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

138 lines
4.4 KiB
Go
Raw Permalink Normal View History

package database
import (
"context"
"errors"
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"
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, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagGroup")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_group_name": tagGroupName,
"tag_name": tagName,
})
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
attribute.String("tag_name", string(tagName)),
)
utils.HandleEvent(span, localLogger, "Starting tag group creation")
if client == nil {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagGroupName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty})
}
if tagName == "" {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
}
tagGroup := models.TagGroup{
Name: tagGroupName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tagGroup)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.TagGroup{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "Tag group created successfully")
return tagGroup, nil
}
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"tag_groups_count": len(tagsGroups),
"batch_size": batchSize,
})
span.SetAttributes(
attribute.Int("batch_size", batchSize),
attribute.Int("tag_group_count", len(tagsGroups)),
)
utils.HandleEvent(span, localLogger, "Starting batch tag group creation")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tagsGroups == nil || len(tagsGroups) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty})
}
if batchSize == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, 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, "Tag group created successfully")
return nil
}
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagGroup")
defer span.End()
span.SetAttributes(
attribute.String("tag_group_name", string(tagGroupName)),
)
localLogger = localLogger.WithFields(log.Fields{
"tag_group_name": tagGroupName,
})
utils.HandleEvent(span, localLogger, "Starting tag group deletion")
var tagGroup models.TagGroup
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagGroupName) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagGroupNameIsEmpty})
}
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
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 group deleted successfully")
return nil
}