otter-space-sdk/pkg/database/pool_reference.go
SoXX 91254adfe3 fix: wrong error message
- fix the error message to be more precise what it is
2024-10-15 10:38:46 +02:00

122 lines
4.1 KiB
Go

package database
import (
"context"
"errors"
"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 CreatePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID, url string) (models.PoolReference, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePoolReference")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"source_id": sourceID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("source_id", string(sourceID)),
)
utils.HandleEvent(span, localLogger, "Starting poolReference creation")
if client == nil {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if poolID == "" {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
}
if sourceID == "" {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(sourceID) != 25 {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
if url == "" {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolURLIsEmpty})
}
poolReference := models.PoolReference{
PoolID: poolID,
SourceID: sourceID,
URL: url,
}
result := client.WithContext(ctx).Create(&poolReference)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "poolReference created successfully")
return poolReference, nil
}
func DeletePoolReference(ctx context.Context, poolID models.PoolID, sourceID models.SourceID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePoolReference")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"pool_id": poolID,
"source_id": sourceID,
})
span.SetAttributes(
attribute.String("pool_id", string(poolID)),
attribute.String("source_id", string(sourceID)),
)
utils.HandleEvent(span, localLogger, "Starting delete poolReference")
var poolReference models.PoolReference
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if poolID == "" {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
}
if len(poolID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
}
if sourceID == "" {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
}
if len(sourceID) != 25 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
}
result := client.WithContext(ctx).Delete(&poolReference, "pool_id = ? AND source_id = ?", poolID, sourceID)
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, "poolReference deleted successfully")
return nil
}