2024-08-10 22:23:47 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
2024-08-12 09:17:02 +00:00
|
|
|
|
2024-11-06 12:21:21 +00:00
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
|
|
|
|
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
|
|
|
|
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
|
2024-08-12 09:17:02 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
2024-08-10 22:23:47 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
func CreateUser(ctx context.Context, user models.User) (models.User, error) {
|
|
|
|
|
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"user_id": user.ID,
|
|
|
|
})
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("user_id", string(user.ID)),
|
|
|
|
)
|
2024-08-10 22:23:47 +00:00
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "Starting user creation")
|
|
|
|
|
|
|
|
if client == nil {
|
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).Create(&user)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "User created successfully")
|
2024-08-10 22:23:47 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUserByID(ctx context.Context, id models.UserID) (models.User, error) {
|
2024-08-12 09:17:02 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserByID")
|
|
|
|
defer span.End()
|
2024-08-10 22:23:47 +00:00
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("user_id", string(id)),
|
|
|
|
)
|
2024-08-10 22:23:47 +00:00
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"user_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting user retrieval")
|
|
|
|
|
|
|
|
var user models.User
|
2024-08-10 22:23:47 +00:00
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
if client == nil {
|
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).First(&user, "id = ?", id)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "User retrieved successfully")
|
2024-08-10 22:23:47 +00:00
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(ctx context.Context, id models.UserID) error {
|
2024-08-12 09:17:02 +00:00
|
|
|
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
span.SetAttributes(
|
|
|
|
attribute.String("user_id", string(id)),
|
|
|
|
)
|
|
|
|
|
2024-08-12 13:53:45 +00:00
|
|
|
localLogger = localLogger.WithFields(log.Fields{
|
2024-08-12 09:17:02 +00:00
|
|
|
"user_id": id,
|
|
|
|
})
|
|
|
|
|
|
|
|
utils.HandleEvent(span, localLogger, "Starting user deletion")
|
|
|
|
|
2024-08-10 22:23:47 +00:00
|
|
|
var user models.User
|
|
|
|
|
|
|
|
if client == nil {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result := client.WithContext(ctx).Delete(&user, id)
|
|
|
|
if result.Error != nil {
|
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
2024-08-12 09:17:02 +00:00
|
|
|
return utils.HandleError(ctx, span, localLogger, result.Error)
|
2024-08-10 22:23:47 +00:00
|
|
|
}
|
|
|
|
|
2024-08-12 09:17:02 +00:00
|
|
|
utils.HandleEvent(span, localLogger, "User deleted successfully")
|
2024-08-10 22:23:47 +00:00
|
|
|
return nil
|
|
|
|
}
|