SDK v3 #8

Merged
SoXX merged 77 commits from dev/issue-5 into main 2024-08-14 13:27:35 +00:00
Showing only changes of commit 24d51a3751 - Show all commits

View File

@ -3,84 +3,141 @@ package database
import ( import (
"context" "context"
"errors" "errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm" "gorm.io/gorm"
) )
func CreatePost(ctx context.Context, post models.Post) (models.Post, error) { func CreatePost(ctx context.Context, post models.Post) (models.Post, error) {
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")
if client == nil { if client == nil {
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
result := client.WithContext(ctx).Create(&post) result := client.WithContext(ctx).Create(&post)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.Post{}, &otterError.Database{Reason: otterError.DuplicateKey} return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
} }
return models.Post{}, result.Error return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Post created successfully")
return post, nil return post, nil
} }
func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error { func CreatePostInBatch(ctx context.Context, post []models.Post, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostInBatch")
defer span.End()
localLogger.WithFields(log.Fields{
"post_count": len(post),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int64("batch_size", int64(batchSize)),
attribute.Int64("post_count", int64(len(post))),
)
utils.HandleEvent(span, localLogger, "Starting batch post creation")
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if post == nil { if post == nil || len(post) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty})
}
if len(post) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostListIsEmpty}
} }
if batchSize == 0 { if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
} }
result := client.WithContext(ctx).CreateInBatches(&post, batchSize) result := client.WithContext(ctx).CreateInBatches(&post, batchSize)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) { if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Batch posts created successfully")
return nil return nil
} }
func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) { func GetPostByID(ctx context.Context, id models.PostID) (models.Post, error) {
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")
var post models.Post var post models.Post
if client == nil { if client == nil {
return models.Post{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(id) == 0 { if len(id) == 0 {
return models.Post{}, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
} }
result := client.WithContext(ctx).First(&post, "id = ?", id) result := client.WithContext(ctx).First(&post, "id = ?", id)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.Post{}, &otterError.Database{Reason: otterError.NoDataFound} return models.Post{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return models.Post{}, result.Error return models.Post{}, utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Post retrieved successfully")
return post, nil return post, nil
} }
func UpdatePost(ctx context.Context, anthrovePost models.Post) error { func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
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")
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(anthrovePost.ID) == 0 { if len(anthrovePost.ID) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
} }
updatePost := models.Post{ updatePost := models.Post{
@ -92,36 +149,51 @@ func UpdatePost(ctx context.Context, anthrovePost models.Post) error {
result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{}) result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{})
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Post updated successfully")
return nil return nil
} }
func DeletePost(ctx context.Context, id models.PostID) error { func DeletePost(ctx context.Context, id models.PostID) error {
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")
var userFavorite models.UserFavorite var userFavorite models.UserFavorite
if client == nil { if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
} }
if len(id) == 0 { if len(id) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty})
} }
if len(id) != 25 { if len(id) != 25 {
return &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort} return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDToShort})
} }
result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id)
if result.Error != nil { if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) { if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound} return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
} }
return result.Error return utils.HandleError(ctx, span, localLogger, result.Error)
} }
utils.HandleEvent(span, localLogger, "Post deleted successfully")
return nil return nil
} }