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.
77 lines
1.8 KiB
Go
77 lines
1.8 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// UserCommands contains the command handlers for the user aggregate.
|
|
type UserCommands struct {
|
|
repo domain.UserRepository
|
|
}
|
|
|
|
// NewUserCommands creates a new UserCommands handler.
|
|
func NewUserCommands(repo domain.UserRepository) *UserCommands {
|
|
return &UserCommands{repo: repo}
|
|
}
|
|
|
|
// CreateUserInput represents the input for creating a new user.
|
|
type CreateUserInput struct {
|
|
Username string
|
|
Email string
|
|
Password string
|
|
FirstName string
|
|
LastName string
|
|
Role domain.UserRole
|
|
}
|
|
|
|
// CreateUser creates a new user.
|
|
func (c *UserCommands) CreateUser(ctx context.Context, input CreateUserInput) (*domain.User, error) {
|
|
user := &domain.User{
|
|
Username: input.Username,
|
|
Email: input.Email,
|
|
Password: input.Password,
|
|
FirstName: input.FirstName,
|
|
LastName: input.LastName,
|
|
Role: input.Role,
|
|
}
|
|
err := c.repo.Create(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
// UpdateUserInput represents the input for updating an existing user.
|
|
type UpdateUserInput struct {
|
|
ID uint
|
|
Username string
|
|
Email string
|
|
FirstName string
|
|
LastName string
|
|
Role domain.UserRole
|
|
}
|
|
|
|
// UpdateUser updates an existing user.
|
|
func (c *UserCommands) UpdateUser(ctx context.Context, input UpdateUserInput) (*domain.User, error) {
|
|
user, err := c.repo.GetByID(ctx, input.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
user.Username = input.Username
|
|
user.Email = input.Email
|
|
user.FirstName = input.FirstName
|
|
user.LastName = input.LastName
|
|
user.Role = input.Role
|
|
err = c.repo.Update(ctx, user)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
// DeleteUser deletes a user by ID.
|
|
func (c *UserCommands) DeleteUser(ctx context.Context, id uint) error {
|
|
return c.repo.Delete(ctx, id)
|
|
}
|