tercul-backend/internal/app/work/commands.go
google-labs-jules[bot] 06e6e2be85 refactor(domain): Isolate Work aggregate
This commit isolates the `Work` aggregate into its own package at `internal/domain/work`, following the first step of the refactoring plan in `refactor.md`.

- The `Work` struct, related types, and the `WorkRepository` interface have been moved to the new package.
- A circular dependency between `domain` and `work` was resolved by moving the `AnalyticsRepository` to the `app` layer.
- All references to the moved types have been updated across the entire codebase to fix compilation errors.
- Test files, including mocks and integration tests, have been updated to reflect the new structure.
2025-10-03 16:15:09 +00:00

82 lines
2.0 KiB
Go

package work
import (
"context"
"errors"
"tercul/internal/domain/search"
"tercul/internal/domain/work"
)
// WorkCommands contains the command handlers for the work aggregate.
type WorkCommands struct {
repo work.WorkRepository
searchClient search.SearchClient
}
// NewWorkCommands creates a new WorkCommands handler.
func NewWorkCommands(repo work.WorkRepository, searchClient search.SearchClient) *WorkCommands {
return &WorkCommands{
repo: repo,
searchClient: searchClient,
}
}
// CreateWork creates a new work.
func (c *WorkCommands) CreateWork(ctx context.Context, work *work.Work) (*work.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 *work.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
}