tercul-backend/internal/app/author/commands.go
google-labs-jules[bot] 1c4dcbcf99 Refactor: Introduce service layer for application logic
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.
2025-09-09 02:28:25 +00:00

59 lines
1.4 KiB
Go

package author
import (
"context"
"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
}
// CreateAuthor creates a new author.
func (c *AuthorCommands) CreateAuthor(ctx context.Context, input CreateAuthorInput) (*domain.Author, error) {
author := &domain.Author{
Name: input.Name,
}
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
}
// UpdateAuthor updates an existing author.
func (c *AuthorCommands) UpdateAuthor(ctx context.Context, input UpdateAuthorInput) (*domain.Author, error) {
author, err := c.repo.GetByID(ctx, input.ID)
if err != nil {
return nil, err
}
author.Name = input.Name
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 {
return c.repo.Delete(ctx, id)
}