package database import ( "context" "errors" "git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag") defer span.End() localLogger.WithFields(log.Fields{ "tag_name": tagName, "tag_type": tagType, }) span.SetAttributes( attribute.String("tag_name", string(tagName)), attribute.String("tag_type", string(tagType)), ) utils.HandleEvent(span, localLogger, "Starting tag creation") if client == nil { return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } tag := models.Tag{ Name: tagName, Type: tagType, } result := client.WithContext(ctx).Create(&tag) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } return models.Tag{}, utils.HandleError(ctx, span, localLogger, result.Error) } utils.HandleEvent(span, localLogger, "Tag created successfully") return tag, nil } func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch") defer span.End() localLogger.WithFields(log.Fields{ "tag_count": len(tags), "batch_size": batchSize, }) span.SetAttributes( attribute.Int64("batch_size", int64(batchSize)), attribute.Int64("tag_count", int64(len(tags))), ) utils.HandleEvent(span, localLogger, "Starting batch tag creation") if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tags == nil || len(tags) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}) } if batchSize == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } result := client.WithContext(ctx).CreateInBatches(&tags, 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 tags created successfully") return nil } func DeleteTag(ctx context.Context, tagName models.TagName) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag") defer span.End() localLogger.WithFields(log.Fields{ "tag_name": tagName, }) span.SetAttributes( attribute.String("tag_name", string(tagName)), ) utils.HandleEvent(span, localLogger, "Starting tag deletion") var tag models.Tag if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Delete(&tag, tagName) 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, "Tag deleted successfully") return nil }