123 lines
3.9 KiB
Go
123 lines
3.9 KiB
Go
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 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})
|
|
}
|
|
|
|
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})
|
|
}
|
|
|
|
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
|
|
}
|