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 CreatePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID, url string) (models.PoolReference, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolReference") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": poolID, "source_id": sourceID, }) span.SetAttributes( attribute.String("pool_id", string(poolID)), attribute.String("source_id", string(sourceID)), ) utils.HandleEvent(span, localLogger, "Starting poolReference creation") if client == nil { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(poolID) == 0 { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty}) } if len(poolID) != 25 { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength}) } if len(sourceID) == 0 { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}) } if len(sourceID) != 25 { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength}) } if url == "" { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolURLIsEmpty}) } poolReference := models.PoolReference{ PoolID: poolID, SourceID: sourceID, URL: url, } result := client.WithContext(ctx).Create(&poolReference) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, result.Error) } utils.HandleEvent(span, localLogger, "poolReference created successfully") return poolReference, nil } func DeletePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolReference") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "pool_id": poolID, "source_id": sourceID, }) span.SetAttributes( attribute.String("pool_id", string(poolID)), attribute.String("source_id", string(sourceID)), ) utils.HandleEvent(span, localLogger, "Starting delete poolReference") var poolReference models.PoolReference if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(poolID) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty}) } if len(poolID) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength}) } if len(sourceID) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty}) } if len(sourceID) != 25 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength}) } result := client.WithContext(ctx).Delete(&poolReference, "pool_id = ? AND source_id = ?", poolID, sourceID) 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, "poolReference deleted successfully") return nil }