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

164 lines
4.8 KiB
Go
Raw Normal View History

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 CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) (models.TagGroup, error) {
ctx, span := tracer.Start(ctx, "CreateTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
attribute.String("tag_name", string(tagName)),
)
span.AddEvent("Starting tag group creation")
if client == nil {
return models.TagGroup{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
tagGroup := models.TagGroup{
Name: tagGroupName,
TagID: tagName,
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}).Debug("attempting to create tag group")
result := client.WithContext(ctx).Create(&tagGroup)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}
return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}
return models.TagGroup{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
}).Debug("tag group created")
span.AddEvent("Tag group created successfully")
return tagGroup, nil
}
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
ctx, span := tracer.Start(ctx, "CreateTagAliasInBatch")
defer span.End()
span.SetAttributes(
attribute.Int64("batch_size", int64(batchSize)),
attribute.Int64("tag_group_count", int64(len(tagsGroups))),
)
span.AddEvent("Starting batch tag group creation")
if client == nil {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagsGroups == nil || len(tagsGroups) == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_groups_count": len(tagsGroups),
}).Debug("attempting to create tags groups")
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
loggerFields := log.Fields{
"tag_group_count": len(tagsGroups),
}
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_group_count": len(tagsGroups),
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_count": len(tagsGroups),
}).Debug("batch tags groups created")
span.AddEvent("Batch tags groups created successfully")
return nil
}
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
ctx, span := tracer.Start(ctx, "DeleteTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
)
span.AddEvent("Starting tag group deletion")
var tagGroup models.TagGroup
if client == nil {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagGroupName) == 0 {
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
}).Debug("attempting to delete tag group")
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
}
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
}
loggerFields := log.Fields{
"tag_group_name": tagGroupName,
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
"tag_group_name": tagGroupName,
}).Debug("tag group deleted")
span.AddEvent("Tag group deleted successfully")
return nil
}