2024-08-10 22:13:46 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-08-12 09:39:22 +00:00
|
|
|
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
|
2024-08-10 22:13:46 +00:00
|
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
|
2024-08-12 09:39:22 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-08-10 22:13:46 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
|
2024-08-12 09:39:22 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePost")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
localLogger.WithFields(log.Fields{
|
|
|
|
"post_id": post.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("post_id", string(post.ID)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting post creation")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
if client == nil {
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).Create(&post)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Post created successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return post, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
|
2024-08-12 09:39:22 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
localLogger.WithFields(log.Fields{
|
|
|
|
"post_count": len(post),
|
|
|
|
"batch_size": batchSize,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
2024-08-12 09:53:17 +00:00
|
|
|
attribute.Int("batch_size", batchSize),
|
|
|
|
attribute.Int("post_count", len(post)),
|
2024-08-12 09:39:22 +00:00
|
|
|
)
|
2024-08-10 22:13:46 +00:00
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting batch post creation")
|
|
|
|
|
|
|
|
if client == nil {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
if post == nil || len(post) == 0 {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if batchSize == 0 {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).CreateInBatches(&post, batchSize)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Batch posts created successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
|
2024-08-12 09:39:22 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostByID")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
localLogger.WithFields(log.Fields{
|
|
|
|
"post_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("post_id", string(id)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting get post by ID")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var post models.Post
|
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).First(&post, "id = ?", id)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:39:22 +00:00
|
|
|
return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Post retrieved successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return post, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
|
2024-08-12 09:39:22 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePost")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
localLogger.WithFields(log.Fields{
|
|
|
|
"post_id": anthrovePost.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("post_id", string(anthrovePost.ID)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting post update")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
if client == nil {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(anthrovePost.ID) == 0 {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updatePost := models.Post{
|
|
|
|
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) {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Post updated successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeletePost(ctx context.Context, id models.PostID) error {
|
2024-08-12 09:39:22 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePost")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
localLogger.WithFields(log.Fields{
|
|
|
|
"post_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("post_id", string(id)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting delete post")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var userFavorite models.UserFavorite
|
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) != 25 {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:39:22 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:39:22 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Post deleted successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|