2024-07-19 08:03:35 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-09 20:16:03 +00:00
|
|
|
"errors"
|
2024-08-12 09:36:34 +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 09:36:34 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-08-09 20:16:03 +00:00
|
|
|
"gorm.io/gorm"
|
2024-07-19 08:03:35 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
func CreateSource(ctx context.Context, source models.Source) (models.Source, error) {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSource")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting source creation")
|
|
|
|
|
2024-08-09 20:16:03 +00:00
|
|
|
if client == nil {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
result := client.WithContext(ctx).Create(&source)
|
2024-08-09 20:16:03 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-13 13:43:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
|
|
|
"source_id": source.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("source_id", string(source.ID)),
|
|
|
|
)
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Source created successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return source, nil
|
2024-08-09 19:37:54 +00:00
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateSourceInBatch")
|
|
|
|
defer span.End()
|
2024-08-09 20:16:03 +00:00
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:36:34 +00:00
|
|
|
"source_count": len(source),
|
|
|
|
"batch_size": batchSize,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
2024-08-12 09:53:48 +00:00
|
|
|
attribute.Int("batch_size", batchSize),
|
|
|
|
attribute.Int("source_count", len(source)),
|
2024-08-12 09:36:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting batch source creation")
|
|
|
|
|
|
|
|
if client == nil {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
if source == nil || len(source) == 0 {
|
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
if batchSize == 0 {
|
2024-08-12 09:36:34 +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(&source, batchSize)
|
2024-08-09 20:16:03 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Batch sources created successfully")
|
2024-08-09 19:37:54 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-13 13:43:45 +00:00
|
|
|
// UpdateSource updates th source information in the database.
|
|
|
|
// Only a few parameter can be updated:
|
|
|
|
// - DisplayName
|
|
|
|
// - Domain
|
|
|
|
// - Icon
|
2024-08-10 22:13:46 +00:00
|
|
|
func UpdateSource(ctx context.Context, source models.Source) error {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateSource")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:36:34 +00:00
|
|
|
"source_id": source.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("source_id", string(source.ID)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting source update")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
if client == nil {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(source.ID) == 0 {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updateSource := models.Source{
|
2024-08-13 13:43:45 +00:00
|
|
|
BaseModel: models.BaseModel[models.SourceID]{
|
|
|
|
ID: source.ID,
|
|
|
|
},
|
2024-08-10 22:13:46 +00:00
|
|
|
DisplayName: source.DisplayName,
|
|
|
|
Domain: source.Domain,
|
|
|
|
Icon: source.Icon,
|
|
|
|
}
|
|
|
|
|
2024-08-13 13:43:45 +00:00
|
|
|
result := client.WithContext(ctx).Updates(&updateSource)
|
2024-08-10 22:13:46 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:36:34 +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:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Source updated successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByID")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:36:34 +00:00
|
|
|
"source_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("source_id", string(id)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting get source by ID")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var source models.Source
|
2024-08-09 20:16:03 +00:00
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).First(&source, "id = ?", id)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Source retrieved successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return source, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetSourceByDomain")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:36:34 +00:00
|
|
|
"source_domain": sourceDomain,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("source_domain", string(sourceDomain)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting get source by domain")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var source models.Source
|
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(sourceDomain) == 0 {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain)
|
2024-08-09 20:16:03 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
2024-08-12 09:36:34 +00:00
|
|
|
return models.Source{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Source retrieved successfully")
|
2024-08-10 22:13:46 +00:00
|
|
|
return source, nil
|
2024-08-09 19:37:54 +00:00
|
|
|
}
|
2024-07-19 08:03:35 +00:00
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
func DeleteSource(ctx context.Context, id models.SourceID) error {
|
2024-08-12 09:36:34 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteSource")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:36:34 +00:00
|
|
|
"source_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("source_id", string(id)),
|
|
|
|
)
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting delete source")
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
var source models.Source
|
2024-08-09 20:16:03 +00:00
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) == 0 {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(id) != 25 {
|
2024-10-15 08:38:46 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsWrongLength})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 22:13:46 +00:00
|
|
|
result := client.WithContext(ctx).Delete(&source, id)
|
2024-08-09 20:16:03 +00:00
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
2024-08-12 09:36:34 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-09 20:16:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:36:34 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Source deleted successfully")
|
2024-08-09 19:37:54 +00:00
|
|
|
return nil
|
2024-07-19 08:03:35 +00:00
|
|
|
}
|