SDK v3 #8
@ -3,14 +3,28 @@ 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 := tracer.Start(ctx, "CreateTagAlias")
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("tag_alias_name", string(tagAliasName)),
|
||||
attribute.String("tag_name", string(tagName)),
|
||||
)
|
||||
|
||||
span.AddEvent("Starting tag alias creation")
|
||||
|
||||
if client == nil {
|
||||
return models.TagAlias{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
tagAlias := models.TagAlias{
|
||||
@ -18,63 +32,131 @@ func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagNa
|
||||
TagID: tagName,
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
"tag_name": tagName,
|
||||
}).Debug("attempting to create tag alias")
|
||||
|
||||
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}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
"tag_name": tagName,
|
||||
}
|
||||
return models.TagAlias{}, result.Error
|
||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||
}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
"tag_name": tagName,
|
||||
}
|
||||
return models.TagAlias{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
"tag_name": tagName,
|
||||
}).Debug("tag alias created")
|
||||
span.AddEvent("Tag alias created successfully")
|
||||
return tagAlias, nil
|
||||
}
|
||||
|
||||
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
|
||||
ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch")
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.Int64("batch_size", int64(batchSize)),
|
||||
attribute.Int64("tag_count", int64(len(tagsAliases))),
|
||||
)
|
||||
|
||||
span.AddEvent("Starting batch tag creation")
|
||||
|
||||
if client == nil {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, logger, nil, &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 tagsAliases == nil || len(tagsAliases) == 0 {
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
|
||||
}
|
||||
|
||||
if batchSize == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_length": len(tagsAliases),
|
||||
}).Debug("attempting to create tags aliases")
|
||||
|
||||
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
||||
return &otterError.Database{Reason: otterError.DuplicateKey}
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_length": len(tagsAliases),
|
||||
}
|
||||
return result.Error
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
|
||||
}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_length": len(tagsAliases),
|
||||
}
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_length": len(tagsAliases),
|
||||
}).Debug("batch tags aliases created")
|
||||
|
||||
span.AddEvent("Batch tags aliases created successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
|
||||
ctx, span := tracer.Start(ctx, "DeleteTagAlias")
|
||||
defer span.End()
|
||||
|
||||
span.SetAttributes(
|
||||
attribute.String("tag_alias_name", string(tagAliasName)),
|
||||
)
|
||||
|
||||
span.AddEvent("Starting tag alias deletion")
|
||||
|
||||
var tagAlias models.TagAlias
|
||||
|
||||
if client == nil {
|
||||
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
||||
}
|
||||
|
||||
if len(tagAliasName) == 0 {
|
||||
return &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty}
|
||||
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
}).Debug("attempting to delete tag alias")
|
||||
|
||||
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
|
||||
if result.Error != nil {
|
||||
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||
return &otterError.Database{Reason: otterError.NoDataFound}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
}
|
||||
return result.Error
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
|
||||
}
|
||||
|
||||
loggerFields := log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
}
|
||||
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
|
||||
}
|
||||
|
||||
logger.WithContext(ctx).WithFields(log.Fields{
|
||||
"tag_alias_name": tagAliasName,
|
||||
}).Debug("tag alias deleted")
|
||||
|
||||
span.AddEvent("Tag alias deleted successfully")
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user