tercul-backend/internal/app/work/commands.go
google-labs-jules[bot] 49e2bdd9ac feat: Refactor localization, auth, copyright, and monetization domains
This change introduces a major architectural refactoring of the application, with a focus on improving testability, decoupling, and observability.

The following domains have been successfully refactored:
- `localization`: Wrote a full suite of unit tests and added logging.
- `auth`: Introduced a `JWTManager` interface, wrote comprehensive unit tests, and added logging.
- `copyright`: Separated integration tests, wrote a full suite of unit tests, and added logging.
- `monetization`: Wrote a full suite of unit tests and added logging.
- `search`: Refactored the Weaviate client usage by creating a wrapper to improve testability, and achieved 100% test coverage.

For each of these domains, 100% test coverage has been achieved for the refactored code.

The refactoring of the `work` domain is currently in progress. Unit tests have been written for the commands and queries, but there is a persistent build issue with the query tests that needs to be resolved. The error indicates that the query methods are undefined, despite appearing to be correctly defined and called.
2025-09-06 15:15:10 +00:00

74 lines
1.8 KiB
Go

package work
import (
"context"
"errors"
"tercul/internal/domain"
)
// Analyzer defines the interface for work analysis operations.
type Analyzer interface {
AnalyzeWork(ctx context.Context, workID uint) error
}
// WorkCommands contains the command handlers for the work aggregate.
type WorkCommands struct {
repo domain.WorkRepository
analyzer Analyzer
}
// NewWorkCommands creates a new WorkCommands handler.
func NewWorkCommands(repo domain.WorkRepository, analyzer Analyzer) *WorkCommands {
return &WorkCommands{
repo: repo,
analyzer: analyzer,
}
}
// CreateWork creates a new work.
func (c *WorkCommands) CreateWork(ctx context.Context, work *domain.Work) error {
if work == nil {
return errors.New("work cannot be nil")
}
if work.Title == "" {
return errors.New("work title cannot be empty")
}
if work.Language == "" {
return errors.New("work language cannot be empty")
}
return c.repo.Create(ctx, work)
}
// 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")
}
return c.repo.Update(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 {
if workID == 0 {
return errors.New("invalid work ID")
}
return c.analyzer.AnalyzeWork(ctx, workID)
}