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 }