118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreatePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID, orderPosition int) (models.PoolPost, error) {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolPost")
|
|
defer span.End()
|
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
"pool_id": poolID,
|
|
"post_id": postID,
|
|
})
|
|
|
|
span.SetAttributes(
|
|
attribute.String("pool_id", string(poolID)),
|
|
attribute.String("post_id", string(postID)),
|
|
)
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting poolPost creation")
|
|
|
|
if client == nil {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
|
}
|
|
|
|
if len(poolID) == 0 {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
}
|
|
|
|
if len(poolID) != 25 {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
|
}
|
|
|
|
if len(postID) == 0 {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
|
}
|
|
|
|
if len(postID) != 25 {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
|
|
}
|
|
|
|
poolPost := models.PoolPost{
|
|
PoolID: poolID,
|
|
PostID: postID,
|
|
OrderPosition: orderPosition,
|
|
}
|
|
|
|
result := client.WithContext(ctx).Create(&poolPost)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
|
}
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
|
}
|
|
|
|
utils.HandleEvent(span, localLogger, "poolPost created successfully")
|
|
return poolPost, nil
|
|
}
|
|
|
|
func DeletePoolPost(ctx context.Context, poolID models.PoolID, postID models.PostID) error {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolPost")
|
|
defer span.End()
|
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
"pool_id": poolID,
|
|
"post_id": postID,
|
|
})
|
|
|
|
span.SetAttributes(
|
|
attribute.String("pool_id", string(poolID)),
|
|
attribute.String("post_id", string(postID)),
|
|
)
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting delete poolPost")
|
|
|
|
var poolPost models.PoolPost
|
|
|
|
if client == nil {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
|
}
|
|
|
|
if len(poolID) == 0 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
}
|
|
|
|
if len(poolID) != 25 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
|
}
|
|
|
|
if len(postID) == 0 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
|
}
|
|
|
|
if len(postID) != 25 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
|
|
}
|
|
|
|
result := client.WithContext(ctx).Delete(&poolPost, "pool_id = ? AND post_id = ?", poolID, postID)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
|
}
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
|
}
|
|
|
|
utils.HandleEvent(span, localLogger, "poolPost deleted successfully")
|
|
return nil
|
|
}
|