From 1dc9b1fb7c8e49eaaacfcbab239116ac8df4b139 Mon Sep 17 00:00:00 2001 From: SoXX Date: Fri, 9 Aug 2024 22:47:02 +0200 Subject: [PATCH] feat(database): favorites - Added functionality --- pkg/database/favorite.go | 125 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 pkg/database/favorite.go diff --git a/pkg/database/favorite.go b/pkg/database/favorite.go new file mode 100644 index 0000000..b2194b5 --- /dev/null +++ b/pkg/database/favorite.go @@ -0,0 +1,125 @@ +package database + +import ( + "context" + "errors" + otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" + "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" + "gorm.io/gorm" +) + +func CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) { + if client == nil { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + result := client.WithContext(ctx).Create(&userFav) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DuplicateKey} + } + return models.UserFavorite{}, result.Error + } + + return userFav, nil +} + +func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if userFav == nil { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} + } + + if len(userFav) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty} + } + + if batchSize == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} + } + + result := client.WithContext(ctx).CreateInBatches(&userFav, batchSize) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return &otterError.Database{Reason: otterError.DuplicateKey} + } + return result.Error + } + + return nil +} + +func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error { + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(userFav.ID) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + if !userFav.DeletedAt.Valid { + return nil + } + + result := client.WithContext(ctx).Model(&userFav).Update("deleted_at", gorm.DeletedAt{}) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +} + +func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) { + var userFavorites models.UserFavorite + + if client == nil { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return models.UserFavorite{}, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + result := client.WithContext(ctx).First(&userFavorites, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.UserFavorite{}, &otterError.Database{Reason: otterError.NoDataFound} + } + return models.UserFavorite{}, result.Error + } + + return userFavorites, nil +} + +func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error { + var userFavorite models.UserFavorite + + if client == nil { + return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + } + + if len(id) == 0 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty} + } + + if len(id) != 25 { + return &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDToShort} + } + + result := client.WithContext(ctx).Delete(&userFavorite, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return &otterError.Database{Reason: otterError.NoDataFound} + } + return result.Error + } + + return nil +}