2024-10-15 08:20:37 +00:00
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:50:55 +00:00
|
|
|
if len(poolID) == 0 {
|
2024-10-15 08:20:37 +00:00
|
|
|
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(poolID) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsWrongLength})
|
2024-10-15 08:20:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 08:45:55 +00:00
|
|
|
if len(sourceID) == 0 {
|
2024-10-15 08:20:37 +00:00
|
|
|
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sourceID) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return models.PoolReference{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
|
2024-10-15 08:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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})
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:50:55 +00:00
|
|
|
if len(poolID) == 0 {
|
2024-10-15 08:20:37 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PoolIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
2024-10-15 08:41:39 +00:00
|
|
|
if len(poolID) == 0 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsWrongLength})
|
2024-10-15 08:20:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-15 08:45:55 +00:00
|
|
|
if len(sourceID) == 0 {
|
2024-10-15 08:20:37 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(sourceID) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
|
2024-10-15 08:20:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|