tercul-backend/internal/app/translation/commands.go
google-labs-jules[bot] 1c4dcbcf99 Refactor: Introduce service layer for application logic
This change introduces a service layer to encapsulate the business logic
for each domain aggregate. This will make the code more modular,
testable, and easier to maintain.

The following services have been created:
- author
- bookmark
- category
- collection
- comment
- like
- tag
- translation
- user

The main Application struct has been updated to use these new services.
The integration test suite has also been updated to use the new
Application struct and services.

This is a work in progress. The next step is to fix the compilation
errors and then refactor the resolvers to use the new services.
2025-09-09 02:28:25 +00:00

81 lines
2.3 KiB
Go

package translation
import (
"context"
"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
Content string
Description string
Language string
Status domain.TranslationStatus
TranslatableID uint
TranslatableType string
TranslatorID *uint
}
// CreateTranslation creates a new translation.
func (c *TranslationCommands) CreateTranslation(ctx context.Context, input CreateTranslationInput) (*domain.Translation, error) {
translation := &domain.Translation{
Title: input.Title,
Content: input.Content,
Description: input.Description,
Language: input.Language,
Status: input.Status,
TranslatableID: input.TranslatableID,
TranslatableType: input.TranslatableType,
TranslatorID: input.TranslatorID,
}
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
Content string
Description string
Language string
Status domain.TranslationStatus
}
// UpdateTranslation updates an existing translation.
func (c *TranslationCommands) UpdateTranslation(ctx context.Context, input UpdateTranslationInput) (*domain.Translation, error) {
translation, err := c.repo.GetByID(ctx, input.ID)
if err != nil {
return nil, err
}
translation.Title = input.Title
translation.Content = input.Content
translation.Description = input.Description
translation.Language = input.Language
translation.Status = input.Status
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 {
return c.repo.Delete(ctx, id)
}