package translation import ( "context" "errors" "tercul/internal/domain" ) // TranslationCommands contains the command handlers for the translation aggregate. type TranslationCommands struct { repo domain.TranslationRepository } // NewTranslationCommands creates a new TranslationCommands handler. func NewTranslationCommands(repo domain.TranslationRepository) *TranslationCommands { return &TranslationCommands{ repo: repo, } } // CreateTranslationInput represents the input for creating a new translation. type CreateTranslationInput struct { Title string Language string Content string WorkID uint IsOriginalLanguage bool } // CreateTranslation creates a new translation. func (c *TranslationCommands) CreateTranslation(ctx context.Context, input CreateTranslationInput) (*domain.Translation, error) { if input.Title == "" { return nil, errors.New("translation title cannot be empty") } if input.Language == "" { return nil, errors.New("translation language cannot be empty") } if input.WorkID == 0 { return nil, errors.New("work ID cannot be zero") } translation := &domain.Translation{ Title: input.Title, Language: input.Language, Content: input.Content, TranslatableID: input.WorkID, TranslatableType: "Work", IsOriginalLanguage: input.IsOriginalLanguage, } err := c.repo.Create(ctx, translation) if err != nil { return nil, err } return translation, nil } // UpdateTranslationInput represents the input for updating an existing translation. type UpdateTranslationInput struct { ID uint Title string Language string Content string } // UpdateTranslation updates an existing translation. func (c *TranslationCommands) UpdateTranslation(ctx context.Context, input UpdateTranslationInput) (*domain.Translation, error) { if input.ID == 0 { return nil, errors.New("translation ID cannot be zero") } if input.Title == "" { return nil, errors.New("translation title cannot be empty") } if input.Language == "" { return nil, errors.New("translation language cannot be empty") } // Fetch the existing translation translation, err := c.repo.GetByID(ctx, input.ID) if err != nil { return nil, err } if translation == nil { return nil, errors.New("translation not found") } // Update fields translation.Title = input.Title translation.Language = input.Language translation.Content = input.Content err = c.repo.Update(ctx, translation) if err != nil { return nil, err } return translation, nil } // DeleteTranslation deletes a translation by ID. func (c *TranslationCommands) DeleteTranslation(ctx context.Context, id uint) error { if id == 0 { return errors.New("invalid translation ID") } return c.repo.Delete(ctx, id) }