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 CreatePool(ctx context.Context, pool models.Pool) (models.Pool, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePool") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": pool.ID, }) span.SetAttributes( attribute.String("pool_id", string(pool.ID)), ) utils.HandleEvent(span, localLogger, "Starting pool creation") if client == nil { return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&pool) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } return models.Pool{}, utils.HandleError(ctx, span, localLogger, result.Error) } utils.HandleEvent(span, localLogger, "pool created successfully") return pool, nil } func GetPoolByID(ctx context.Context, id models.PoolID) (models.Pool, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPool") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": id, }) span.SetAttributes( attribute.String("pool_id", string(id)), ) utils.HandleEvent(span, localLogger, "Starting get pool by ID") var pool models.Pool if client == nil { return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(id) == 0 { return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty}) } result := client.WithContext(ctx).First(&pool, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return models.Pool{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } return models.Pool{}, utils.HandleError(ctx, span, localLogger, result.Error) } utils.HandleEvent(span, localLogger, "pool retrieved successfully") return pool, nil } // UpdatePool updates the pool information in the database. // Only supports changing the Name and the Category of the Pool func UpdatePool(ctx context.Context, pool models.Pool) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePool") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": pool.ID, }) span.SetAttributes( attribute.String("pool_id", string(pool.ID)), ) utils.HandleEvent(span, localLogger, "Starting post update") if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(pool.ID) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty}) } updatePost := models.Pool{ BaseModel: models.BaseModel[models.PoolID]{ ID: pool.ID, }, Name: pool.Name, Category: pool.Category, } 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, "pool updated successfully") return nil } func DeletePool(ctx context.Context, id models.PoolID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePool") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": id, }) span.SetAttributes( attribute.String("pool_id", string(id)), ) utils.HandleEvent(span, localLogger, "Starting delete pool") var pool models.Pool 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.PoolIDIsEmpty}) } if len(id) != 25 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength}) } result := client.WithContext(ctx).Delete(&pool, "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, "pool deleted successfully") return nil }