SoXX
755aea20aa
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 3m15s
- use consistent methode to check if the id is 0
118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils"
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v4/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 poolID == "" {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
}
|
|
|
|
if len(poolID) == 0 {
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
|
}
|
|
|
|
if postID == "" {
|
|
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 poolID == "" {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
}
|
|
|
|
if len(poolID) == 0 {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
|
}
|
|
|
|
if postID == "" {
|
|
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
|
|
}
|