soxx
6f814e5b21
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 3m8s
- unified the migrations - added new IDs for Tag, TagAlias, TagGroup - changed relevant functions to use the given IDs BREAKING-CHANGE: Database needs to be cleared or migrated. Not compatible with Database v5
131 lines
4.0 KiB
Go
131 lines
4.0 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v6/internal/utils"
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v6/pkg/error"
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v6/pkg/models"
|
|
log "github.com/sirupsen/logrus"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagType) (models.Tag, error) {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTag")
|
|
defer span.End()
|
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
"tag_name": tagName,
|
|
"tag_type": tagType,
|
|
})
|
|
|
|
span.SetAttributes(
|
|
attribute.String("tag_name", string(tagName)),
|
|
attribute.String("tag_type", string(tagType)),
|
|
)
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting tag creation")
|
|
|
|
if client == nil {
|
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
|
}
|
|
|
|
if tagName == "" {
|
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
|
|
}
|
|
|
|
if tagType == "" {
|
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagTypeIsEmpty})
|
|
}
|
|
|
|
tag := models.Tag{
|
|
Name: tagName,
|
|
Type: tagType,
|
|
}
|
|
|
|
result := client.WithContext(ctx).Create(&tag)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
|
}
|
|
return models.Tag{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
|
}
|
|
|
|
utils.HandleEvent(span, localLogger, "Tag created successfully")
|
|
return tag, nil
|
|
}
|
|
|
|
func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) ([]models.Tag, error) {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagInBatch")
|
|
defer span.End()
|
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
"tag_count": len(tags),
|
|
"batch_size": batchSize,
|
|
})
|
|
|
|
span.SetAttributes(
|
|
attribute.Int("batch_size", batchSize),
|
|
attribute.Int("tag_count", len(tags)),
|
|
)
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting batch tag creation")
|
|
|
|
if client == nil {
|
|
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
|
}
|
|
|
|
if tags == nil || len(tags) == 0 {
|
|
return nil, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
|
|
}
|
|
|
|
if batchSize == 0 {
|
|
return nil, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
|
}
|
|
|
|
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
|
|
if result.Error != nil {
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
|
return nil, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
|
}
|
|
return nil, utils.HandleError(ctx, span, localLogger, result.Error)
|
|
}
|
|
|
|
utils.HandleEvent(span, localLogger, "Batch tags created successfully")
|
|
return tags, nil
|
|
}
|
|
|
|
func DeleteTag(ctx context.Context, tagID models.TagID) error {
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTag")
|
|
defer span.End()
|
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
"tag_id": tagID,
|
|
})
|
|
|
|
span.SetAttributes(
|
|
attribute.Int64("tag_id", int64(tagID)),
|
|
)
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting tag deletion")
|
|
|
|
var tag models.Tag
|
|
|
|
if client == nil {
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
|
}
|
|
|
|
result := client.WithContext(ctx).Delete(&tag, tagID)
|
|
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 deleted successfully")
|
|
return nil
|
|
}
|