otter-space-sdk/pkg/database/tagAlias.go
SoXX da76e6215d
All checks were successful
Gitea Build Check / Build (push) Successful in 3m12s
fix(module): updated to version
2024-11-06 13:21:21 +01:00

135 lines
4.4 KiB
Go

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 CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
})
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias creation")
if client == nil {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagAliasName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty})
}
if tagName == "" {
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
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{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag alias created successfully")
return tagAlias, nil
}
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_aliases_count": len(tagsAliases),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_aliases_count", len(tagsAliases)),
)
utils.HandleEvent(span, localLogger, "Starting batch tag alias creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagsAliases == nil || len(tagsAliases) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, 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 aliases created successfully")
return nil
}
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
})
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias deletion")
var tagAlias models.TagAlias
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagAliasName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
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 alias deleted successfully")
return nil
}