SDK v3 #8

Merged
SoXX merged 77 commits from dev/issue-5 into main 2024-08-14 13:27:35 +00:00
Showing only changes of commit c17b78cc48 - Show all commits

View File

@ -3,6 +3,7 @@ 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"
@ -22,9 +23,7 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
span.AddEvent("Starting tag creation")
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return models.Tag{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return models.Tag{}, utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
tag := models.Tag{
@ -41,24 +40,18 @@ func CreateTag(ctx context.Context, tagName models.TagName, tagType models.TagTy
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"name": tagName,
"type": tagType,
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return models.Tag{}, &otterError.Database{Reason: otterError.DuplicateKey}
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"name": tagName,
"type": tagType,
}).Error(result.Error)
span.RecordError(result.Error)
return models.Tag{}, result.Error
}
return models.Tag{}, utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -81,21 +74,15 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err
span.AddEvent("Starting batch tag creation")
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if tags == nil || len(tags) == 0 {
logger.WithContext(ctx).Error(otterError.TagListIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.TagListIsEmpty})
}
if batchSize == 0 {
logger.WithContext(ctx).Error(otterError.BatchSizeIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -105,17 +92,16 @@ func CreateTagInBatch(ctx context.Context, tags []models.Tag, batchSize int) err
result := client.WithContext(ctx).CreateInBatches(&tags, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
logger.WithContext(ctx).WithFields(log.Fields{
loggerFields := log.Fields{
"tag_length": len(tags),
}).Error(otterError.DuplicateKey)
span.RecordError(&otterError.Database{Reason: otterError.DuplicateKey})
return &otterError.Database{Reason: otterError.DuplicateKey}
}
logger.WithContext(ctx).WithFields(log.Fields{
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.DuplicateKey})
}
loggerFields := log.Fields{
"tag_length": len(tags),
}).Error(result.Error)
span.RecordError(result.Error)
return result.Error
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -139,15 +125,11 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
var tag models.Tag
if client == nil {
logger.WithContext(ctx).Error(otterError.DatabaseIsNotConnected)
span.RecordError(&otterError.Database{Reason: otterError.DatabaseIsNotConnected})
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(tagName) == 0 {
logger.WithContext(ctx).Error(otterError.TagNameIsEmpty)
span.RecordError(&otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty})
return &otterError.EntityValidationFailed{Reason: otterError.TagNameIsEmpty}
return utils.HandleError(ctx, span, logger, nil, &otterError.Database{Reason: otterError.TagNameIsEmpty})
}
logger.WithContext(ctx).WithFields(log.Fields{
@ -157,11 +139,17 @@ func DeleteTag(ctx context.Context, tagName models.TagName) error {
result := client.WithContext(ctx).Delete(&tag, tagName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
span.RecordError(&otterError.Database{Reason: otterError.NoDataFound})
return &otterError.Database{Reason: otterError.NoDataFound}
loggerFields := log.Fields{
"tag_name": tagName,
}
span.RecordError(result.Error)
return result.Error
return utils.HandleError(ctx, span, logger, loggerFields, &otterError.Database{Reason: otterError.NoDataFound})
}
loggerFields := log.Fields{
"tag_name": tagName,
}
return utils.HandleError(ctx, span, logger, loggerFields, result.Error)
}
logger.WithContext(ctx).WithFields(log.Fields{