otter-space-sdk/pkg/database/user.go

109 lines
3.1 KiB
Go
Raw Normal View History

2024-08-10 22:23:47 +00:00
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v2/internal/utils"
2024-08-10 22:23:47 +00:00
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"
2024-08-10 22:23:47 +00:00
"gorm.io/gorm"
)
func CreateUser(ctx context.Context, user models.User) (models.User, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateUser")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"user_id": user.ID,
})
span.SetAttributes(
attribute.String("user_id", string(user.ID)),
)
2024-08-10 22:23:47 +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) {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
2024-08-10 22:23:47 +00:00
}
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
2024-08-10 22:23:47 +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) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetUserByID")
defer span.End()
2024-08-10 22:23:47 +00:00
span.SetAttributes(
attribute.String("user_id", string(id)),
)
2024-08-10 22:23:47 +00:00
localLogger = localLogger.WithFields(log.Fields{
"user_id": id,
})
utils.HandleEvent(span, localLogger, "Starting user retrieval")
var user models.User
2024-08-10 22:23:47 +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) {
return models.User{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
2024-08-10 22:23:47 +00:00
}
return models.User{}, utils.HandleError(ctx, span, localLogger, result.Error)
2024-08-10 22:23:47 +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 {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteUser")
defer span.End()
span.SetAttributes(
attribute.String("user_id", string(id)),
)
localLogger = localLogger.WithFields(log.Fields{
"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 {
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) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
2024-08-10 22:23:47 +00:00
}
return utils.HandleError(ctx, span, localLogger, result.Error)
2024-08-10 22:23:47 +00:00
}
utils.HandleEvent(span, localLogger, "User deleted successfully")
2024-08-10 22:23:47 +00:00
return nil
}