package author import ( "context" "errors" "tercul/internal/domain" ) // AuthorCommands contains the command handlers for the author aggregate. type AuthorCommands struct { repo domain.AuthorRepository } // NewAuthorCommands creates a new AuthorCommands handler. func NewAuthorCommands(repo domain.AuthorRepository) *AuthorCommands { return &AuthorCommands{ repo: repo, } } // CreateAuthorInput represents the input for creating a new author. type CreateAuthorInput struct { Name string Language string } // CreateAuthor creates a new author. func (c *AuthorCommands) CreateAuthor(ctx context.Context, input CreateAuthorInput) (*domain.Author, error) { if input.Name == "" { return nil, errors.New("author name cannot be empty") } if input.Language == "" { return nil, errors.New("author language cannot be empty") } author := &domain.Author{ Name: input.Name, TranslatableModel: domain.TranslatableModel{ Language: input.Language, }, } err := c.repo.Create(ctx, author) if err != nil { return nil, err } return author, nil } // UpdateAuthorInput represents the input for updating an existing author. type UpdateAuthorInput struct { ID uint Name string Language string } // UpdateAuthor updates an existing author. func (c *AuthorCommands) UpdateAuthor(ctx context.Context, input UpdateAuthorInput) (*domain.Author, error) { if input.ID == 0 { return nil, errors.New("author ID cannot be zero") } if input.Name == "" { return nil, errors.New("author name cannot be empty") } if input.Language == "" { return nil, errors.New("author language cannot be empty") } // Fetch the existing author author, err := c.repo.GetByID(ctx, input.ID) if err != nil { return nil, err } if author == nil { return nil, errors.New("author not found") } // Update fields author.Name = input.Name author.Language = input.Language err = c.repo.Update(ctx, author) if err != nil { return nil, err } return author, nil } // DeleteAuthor deletes an author by ID. func (c *AuthorCommands) DeleteAuthor(ctx context.Context, id uint) error { if id == 0 { return errors.New("invalid author ID") } return c.repo.Delete(ctx, id) }