mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This change introduces a service layer to encapsulate the business logic for each domain aggregate. This will make the code more modular, testable, and easier to maintain. The following services have been created: - author - bookmark - category - collection - comment - like - tag - translation - user The main Application struct has been updated to use these new services. The integration test suite has also been updated to use the new Application struct and services. This is a work in progress. The next step is to fix the compilation errors and then refactor the resolvers to use the new services.
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type userRepository struct {
|
|
domain.BaseRepository[domain.User]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewUserRepository creates a new UserRepository.
|
|
func NewUserRepository(db *gorm.DB) domain.UserRepository {
|
|
return &userRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.User](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// FindByUsername finds a user by username
|
|
func (r *userRepository) FindByUsername(ctx context.Context, username string) (*domain.User, error) {
|
|
var user domain.User
|
|
if err := r.db.WithContext(ctx).Where("username = ?", username).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// FindByEmail finds a user by email
|
|
func (r *userRepository) FindByEmail(ctx context.Context, email string) (*domain.User, error) {
|
|
var user domain.User
|
|
if err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error; err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, ErrEntityNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
// ListByRole lists users by role
|
|
func (r *userRepository) ListByRole(ctx context.Context, role domain.UserRole) ([]domain.User, error) {
|
|
var users []domain.User
|
|
if err := r.db.WithContext(ctx).Where("role = ?", role).Find(&users).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return users, nil
|
|
}
|