diff --git a/pkg/database/userSource.go b/pkg/database/userSource.go index bf1879d..6576128 100644 --- a/pkg/database/userSource.go +++ b/pkg/database/userSource.go @@ -3,34 +3,65 @@ 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" + "go.opentelemetry.io/otel/attribute" "gorm.io/gorm" ) func CreateUserSource(ctx context.Context, userSource models.UserSource) (models.UserSource, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": userSource.ID, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(userSource.SourceID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user source creation") + if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } result := client.WithContext(ctx).Create(&userSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return models.UserSource{}, &otterError.Database{Reason: otterError.DuplicateKey} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return models.UserSource{}, result.Error + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source created successfully") return userSource, nil } func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": userSource.ID, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(userSource.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting user source update") + if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } if len(userSource.ID) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.UserSourceIDIsEmpty}) } updatedUserSource := models.UserSource{ @@ -47,62 +78,89 @@ func UpdateUserSource(ctx context.Context, userSource models.UserSource) error { result := client.WithContext(ctx).Updates(&updatedUserSource) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { - return &otterError.Database{Reason: otterError.DuplicateKey} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) } - return result.Error + return utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source updated successfully") return nil } func GetUserSourceByID(ctx context.Context, id models.UserSourceID) (models.UserSource, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserSourceByID") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": id, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get user source by ID") + var user models.UserSource if client == nil { - return models.UserSource{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(id) == 0 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}) } if len(id) != 25 { - return models.UserSource{}, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}) } result := client.WithContext(ctx).First(&user, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return models.UserSource{}, &otterError.Database{Reason: otterError.NoDataFound} + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, otterError.Database{Reason: otterError.NoDataFound}) } - return models.UserSource{}, result.Error + return models.UserSource{}, utils.HandleError(ctx, span, localLogger, result.Error) } + utils.HandleEvent(span, localLogger, "User source retrieved successfully") return user, nil } func DeleteUserSource(ctx context.Context, id models.UserSourceID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUserSource") + defer span.End() + + localLogger.WithFields(log.Fields{ + "user_source_id": id, + }) + + span.SetAttributes( + attribute.String("user_source_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete user source") + var user models.UserSource if client == nil { - return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) } - if len(id) == 0 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDIsEmpty}) } if len(id) != 25 { - return &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort} + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.UserSourceIDToShort}) } result := client.WithContext(ctx).Delete(&user, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { - return &otterError.Database{Reason: otterError.NoDataFound} + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) } return result.Error } + utils.HandleEvent(span, localLogger, "User source deleted successfully") return nil }