otter-space-sdk/pkg/database/post.go
SoXX da76e6215d
All checks were successful
Gitea Build Check / Build (push) Successful in 3m12s
fix(module): updated to version
2024-11-06 13:21:21 +01:00

208 lines
6.4 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 CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": post.ID,
})
span.SetAttributes(
attribute.String("post_id", string(post.ID)),
)
utils.HandleEvent(span, localLogger, "Starting post creation")
if client == nil {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&post)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post created successfully")
return post, nil
}
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_count": len(post),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("post_count", len(post)),
)
utils.HandleEvent(span, localLogger, "Starting batch post creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if post == nil || len(post) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&post, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Batch posts created successfully")
return nil
}
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": id,
})
span.SetAttributes(
attribute.String("post_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get post by ID")
var post models.Post
if client == nil {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
result := client.WithContext(ctx).First(&post, "id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Post retrieved successfully")
return post, nil
}
// UpdatePost updates the user post information in the database.
// Only a few parameter can be updated:
// - Rating
// - Tags
// - References
func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": anthrovePost.ID,
})
span.SetAttributes(
attribute.String("post_id", string(anthrovePost.ID)),
)
utils.HandleEvent(span, localLogger, "Starting post update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(anthrovePost.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
updatePost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: anthrovePost.ID,
},
Rating: anthrovePost.Rating,
Tags: anthrovePost.Tags,
References: anthrovePost.References,
}
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
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, "Post updated successfully")
return nil
}
func DeletePost(ctx context.Context, id models.PostID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"post_id": id,
})
span.SetAttributes(
attribute.String("post_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete post")
var userFavorite models.UserFavorite
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
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, "Post deleted successfully")
return nil
}