tercul-backend/internal/app/work/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

81 lines
2.0 KiB
Go

package work
import (
"context"
"errors"
"tercul/internal/domain"
)
// WorkCommands contains the command handlers for the work aggregate.
type WorkCommands struct {
repo domain.WorkRepository
searchClient domain.SearchClient
}
// NewWorkCommands creates a new WorkCommands handler.
func NewWorkCommands(repo domain.WorkRepository, searchClient domain.SearchClient) *WorkCommands {
return &WorkCommands{
repo: repo,
searchClient: searchClient,
}
}
// CreateWork creates a new work.
func (c *WorkCommands) CreateWork(ctx context.Context, work *domain.Work) (*domain.Work, error) {
if work == nil {
return nil, errors.New("work cannot be nil")
}
if work.Title == "" {
return nil, errors.New("work title cannot be empty")
}
if work.Language == "" {
return nil, errors.New("work language cannot be empty")
}
err := c.repo.Create(ctx, work)
if err != nil {
return nil, err
}
// Index the work in the search client
err = c.searchClient.IndexWork(ctx, work, "")
if err != nil {
// Log the error but don't fail the operation
}
return work, nil
}
// UpdateWork updates an existing work.
func (c *WorkCommands) UpdateWork(ctx context.Context, work *domain.Work) error {
if work == nil {
return errors.New("work cannot be nil")
}
if work.ID == 0 {
return errors.New("work ID cannot be zero")
}
if work.Title == "" {
return errors.New("work title cannot be empty")
}
if work.Language == "" {
return errors.New("work language cannot be empty")
}
err := c.repo.Update(ctx, work)
if err != nil {
return err
}
// Index the work in the search client
return c.searchClient.IndexWork(ctx, work, "")
}
// DeleteWork deletes a work by ID.
func (c *WorkCommands) DeleteWork(ctx context.Context, id uint) error {
if id == 0 {
return errors.New("invalid work ID")
}
return c.repo.Delete(ctx, id)
}
// AnalyzeWork performs linguistic analysis on a work.
func (c *WorkCommands) AnalyzeWork(ctx context.Context, workID uint) error {
// TODO: implement this
return nil
}