otter-space-sdk/pkg/database/pool_post.go
SoXX a5420cc8c2
All checks were successful
Gitea Build Check / Build (push) Successful in 3m5s
feat(pool): added pool pots
- added basic CRUD for PoolsPosts
2024-10-14 14:16:13 +02:00

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) != 25 {
return models.PoolPost{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDToShort})
}
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.PostIDToShort})
}
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) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDToShort})
}
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.PostIDToShort})
}
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
}