2024-08-10 22:13:46 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-08-12 07:58:14 +00:00
|
|
|
|
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"
|
2024-08-12 07:58:14 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-08-10 22:13:46 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
func CreateTagAlias(ctx context.Context, tagAliasName models.TagAliasName, tagName models.TagName) (models.TagAlias, error) {
|
2024-08-12 09:17:02 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAlias")
|
2024-08-12 07:58:14 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"tag_alias_name": tagAliasName,
|
|
|
|
"tag_name": tagName,
|
|
|
|
})
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("tag_alias_name", string(tagAliasName)),
|
|
|
|
attribute.String("tag_name", string(tagName)),
|
|
|
|
)
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting tag alias creation")
|
2024-08-12 07:58:14 +00:00
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
if client == nil {
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 12:44:27 +00:00
|
|
|
if tagAliasName == "" {
|
|
|
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasNameIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagName == "" {
|
|
|
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
|
|
|
|
}
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
tagAlias := models.TagAlias{
|
2024-08-10 22:13:46 +00:00
|
|
|
Name: tagAliasName,
|
|
|
|
TagID: tagName,
|
|
|
|
}
|
|
|
|
|
2024-08-10 22:24:02 +00:00
|
|
|
result := client.WithContext(ctx).Create(&tagAlias)
|
2024-08-10 22:13:46 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.TagAlias{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Tag alias created successfully")
|
2024-08-10 22:24:02 +00:00
|
|
|
return tagAlias, nil
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTagAliasInBatch(ctx context.Context, tagsAliases []models.TagAlias, batchSize int) error {
|
2024-08-12 09:17:02 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateTagAliasInBatch")
|
2024-08-12 07:58:14 +00:00
|
|
|
defer span.End()
|
2024-08-10 22:13:46 +00:00
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"tag_aliases_count": len(tagsAliases),
|
|
|
|
"batch_size": batchSize,
|
|
|
|
})
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
span.SetAttributes(
|
2024-08-12 09:54:57 +00:00
|
|
|
attribute.Int("batch_size", batchSize),
|
|
|
|
attribute.Int("tag_aliases_count", len(tagsAliases)),
|
2024-08-12 07:58:14 +00:00
|
|
|
)
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting batch tag alias creation")
|
2024-08-12 07:58:14 +00:00
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
if tagsAliases == nil || len(tagsAliases) == 0 {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.TagAliasListIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if batchSize == 0 {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).CreateInBatches(&tagsAliases, batchSize)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-12 07:58:14 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Batch tags aliases created successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteTagAlias(ctx context.Context, tagAliasName models.TagAliasName) error {
|
2024-08-12 09:17:02 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteTagAlias")
|
2024-08-12 07:58:14 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"tag_alias_name": tagAliasName,
|
|
|
|
})
|
|
|
|
|
2024-08-12 07:58:14 +00:00
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("tag_alias_name", string(tagAliasName)),
|
|
|
|
)
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting tag alias deletion")
|
2024-08-12 07:58:14 +00:00
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var tagAlias models.TagAlias
|
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 12:44:27 +00:00
|
|
|
if len(tagAliasName) == 0 {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.TagAliasNameIsEmpty})
|
|
|
|
}
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
result := client.WithContext(ctx).Delete(&tagAlias, tagAliasName)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Tag alias deleted successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|