mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
73 lines
1.8 KiB
Go
73 lines
1.8 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
|
|
analyzer interface { // This will be replaced with a proper interface later
|
|
AnalyzeWork(ctx context.Context, workID uint) error
|
|
}
|
|
}
|
|
|
|
// NewWorkCommands creates a new WorkCommands handler.
|
|
func NewWorkCommands(repo domain.WorkRepository, analyzer interface {
|
|
AnalyzeWork(ctx context.Context, workID uint) error
|
|
}) *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)
|
|
}
|