package database import ( "context" "errors" "git.anthrove.art/Anthrove/otter-space-sdk/v6/internal/utils" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v6/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v6/pkg/models" log "github.com/sirupsen/logrus" "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagID models.TagID) (models.TagGroup, error) { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "tag_group_name": tagGroupName, "tag_name": tagID, }) span.SetAttributes( attribute.String("tag_group_name", string(tagGroupName)), attribute.Int64("tag_name", int64(tagID)), ) utils.HandleEvent(span, localLogger, "Starting tag group creation") if client == nil { return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tagGroupName == "" { return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty}) } tagGroup := models.TagGroup{ Name: tagGroupName, TagID: tagID, } result := client.WithContext(ctx).Create(&tagGroup) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, result.Error) } utils.HandleEvent(span, localLogger, "Tag group created successfully") return tagGroup, nil } func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch") defer span.End() localLogger = localLogger.WithFields(log.Fields{ "tag_groups_count": len(tagsGroups), "batch_size": batchSize, }) span.SetAttributes( attribute.Int("batch_size", batchSize), attribute.Int("tag_group_count", len(tagsGroups)), ) utils.HandleEvent(span, localLogger, "Starting batch tag group creation") if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if tagsGroups == nil || len(tagsGroups) == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}) } if batchSize == 0 { return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}) } result := client.WithContext(ctx).CreateInBatches(&tagsGroups, 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, "Tag group created successfully") return nil } func DeleteTagGroup(ctx context.Context, tagGroupID models.TagGroupID) error { ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagGroup") defer span.End() span.SetAttributes( attribute.Int64("tag_group_id", int64(tagGroupID)), ) localLogger = localLogger.WithFields(log.Fields{ "tag_group_id": tagGroupID, }) utils.HandleEvent(span, localLogger, "Starting tag group deletion") var tagGroup models.TagGroup if client == nil { return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Delete(&tagGroup, tagGroupID) 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 group deleted successfully") return nil }