2024-10-14 12:16:13 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:41:39 +00:00
|
|
|
if len(poolID) == 0 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
2024-10-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if postID == "" {
|
|
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(postID) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
|
2024-10-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:41:39 +00:00
|
|
|
if len(poolID) == 0 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
2024-10-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if postID == "" {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(postID) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
|
2024-10-14 12:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|