tercul-backend/internal/data/sql/user_repository.go
google-labs-jules[bot] bb5e18d162 refactor: Introduce application layer and dataloaders
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable.

Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API.

The following changes were made:
- Created application services for most of the domains.
- Refactored the GraphQL resolvers to use the new application services.
- Implemented dataloaders for the `Author` aggregate.
- Updated the `app.Application` struct to hold the application services instead of the repositories.
- Fixed a large number of compilation errors in the test files that arose from these changes.

There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
2025-09-08 10:19:43 +00:00

65 lines
1.7 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
}
// GetByIDs finds users by a list of IDs
func (r *userRepository) GetByIDs(ctx context.Context, ids []uint) ([]domain.User, error) {
var users []domain.User
if err := r.db.WithContext(ctx).Where("id IN (?)", ids).Find(&users).Error; err != nil {
return nil, err
}
return users, nil
}