mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable. Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API. The following changes were made: - Created application services for most of the domains. - Refactored the GraphQL resolvers to use the new application services. - Implemented dataloaders for the `Author` aggregate. - Updated the `app.Application` struct to hold the application services instead of the repositories. - Fixed a large number of compilation errors in the test files that arose from these changes. There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
108 lines
2.7 KiB
Go
108 lines
2.7 KiB
Go
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)
|
|
}
|