otter-space-sdk/pkg/database/tagAlias.go

135 lines
4.4 KiB
Go
Raw Permalink Normal View History

package database
import (
"context"
"errors"
2024-08-12 07:58:14 +00:00
2024-08-29 13:02:39 +00:00
"git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
2024-08-12 07:58:14 +00:00
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")
2024-08-12 07:58:14 +00:00
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
"tag_name": tagName,
})
2024-08-12 07:58:14 +00:00
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias creation")
2024-08-12 07:58:14 +00:00
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")
2024-08-12 07:58:14 +00:00
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_aliases_count": len(tagsAliases),
"batch_size": batchSize,
})
2024-08-12 07:58:14 +00:00
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_aliases_count", len(tagsAliases)),
2024-08-12 07:58:14 +00:00
)
utils.HandleEvent(span, localLogger, "Starting batch tag alias creation")
2024-08-12 07:58:14 +00:00
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
2024-08-12 07:58:14 +00:00
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})
2024-08-12 07:58:14 +00:00
}
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")
2024-08-12 07:58:14 +00:00
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_alias_name": tagAliasName,
})
2024-08-12 07:58:14 +00:00
span.SetAttributes(
attribute.String("tag_alias_name", string(tagAliasName)),
)
utils.HandleEvent(span, localLogger, "Starting tag alias deletion")
2024-08-12 07:58:14 +00:00
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
}