package database import ( "context" "errors" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "gorm.io/gorm" ) func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) { if client == nil { return models.TagAlias{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } tagAlias := models.TagAlias{ Name: tagAliasName, TagID: tagName, } result := client.WithContext(ctx).Create(&tagAlias) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.TagAlias{}, &otterError.Database{Reason: otterError.DuplicateKey} } return models.TagAlias{}, result.Error } return tagAlias, nil } func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error { if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if tagsAliases == nil { return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} } if len(tagsAliases) == 0 { return &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty} } if batchSize == 0 { return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} } result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return &otterError.Database{Reason: otterError.DuplicateKey} } return result.Error } return nil } func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error { var tagAlias models.TagAlias if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(tagAliasName) == 0 { return &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty} } result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return &otterError.Database{Reason: otterError.NoDataFound} } return result.Error } return nil }