otter-space-sdk/pkg/database/favorite.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

200 lines
6.7 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 CreateUserFavorite(ctx context.Context, userFav models.UserFavorite) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite creation")
if client == nil {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &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{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite created successfully")
return userFav, nil
}
func CreateUserFavoriteInBatch(ctx context.Context, userFav []models.UserFavorite, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserFavoriteInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_count": len(userFav),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("user_favorite_count", len(userFav)),
)
utils.HandleEvent(span, localLogger, "Starting batch user favorite creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if userFav == nil || len(userFav) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&userFav, 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 user favorites created successfully")
return nil
}
// UpdateUserFavorite updates the user post information in the database.
// Only supports the undulation of userFavorites, for this set the DeletedAt.Valid to false
func UpdateUserFavorite(ctx context.Context, userFav models.UserFavorite) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": userFav.ID,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(userFav.ID)),
)
utils.HandleEvent(span, localLogger, "Starting user favorite update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(userFav.ID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIDIsEmpty})
}
if userFav.DeletedAt.Valid == true {
return nil
}
result := client.WithContext(ctx).Unscoped().Model(&models.UserFavorite{}).Where("id", userFav.ID).Update("deleted_at", nil)
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, "User favorite updated successfully")
return nil
}
func GetUserFavoritesByID(ctx context.Context, id models.UserFavoriteID) (models.UserFavorite, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserFavoritesByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get user favorite by ID")
var userFavorites models.UserFavorite
if client == nil {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, &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{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.UserFavorite{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "User favorite retrieved successfully")
return userFavorites, nil
}
func DeleteUserFavorite(ctx context.Context, id models.UserFavoriteID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserFavorite")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_favorite_id": id,
})
span.SetAttributes(
attribute.String("user_favorite_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete user favorite")
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.UserFavoriteIDIsEmpty})
}
if len(id) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserFavoriteIsWrongLength})
}
result := client.WithContext(ctx).Delete(&userFavorite, 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, "User favorite deleted successfully")
return nil
}