tercul-backend/internal/app/translation/queries.go
google-labs-jules[bot] 0a27c84771 This commit introduces a series of significant improvements to bring the codebase closer to a production-ready state.
Key changes include:

- **Architectural Refactoring (CQRS/DTOs):** Refactored the `work` and `translation` application services to use Data Transfer Objects (DTOs) for query responses. This separates the domain layer from the API layer, improving maintainability and performance.

- **Implemented Core Business Logic:** Implemented the `AnalyzeWork` command, which was previously a stub. This command now performs linguistic analysis on works and translations by calling the analytics service.

- **Dependency Injection Improvements:**
    - Refactored the configuration loading in `internal/platform/config/config.go` to use a local `viper` instance, removing the reliance on a global singleton.
    - Injected the `analytics.Service` into the `work.Service` to support the `AnalyzeWork` command.

- **Comprehensive Documentation:**
    - Created a new root `README.md` with a project overview, setup instructions, and architectural principles.
    - Added detailed `README.md` files to key packages (`api`, `analytics`, `auth`, `work`, `db`) to document their purpose and usage.

- **Improved Test Coverage:**
    - Added new unit tests for the refactored `work` and `translation` query handlers.
    - Added a new test suite for the `translation` queries, which were previously untested.
    - Added tests for the new `AnalyzeWork` command.
    - Fixed numerous compilation errors in the test suites caused by the refactoring.
2025-10-08 17:25:02 +00:00

111 lines
3.7 KiB
Go

package translation
import (
"context"
"tercul/internal/domain"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
// TranslationQueries contains the query handlers for the translation aggregate.
type TranslationQueries struct {
repo domain.TranslationRepository
tracer trace.Tracer
}
// NewTranslationQueries creates a new TranslationQueries handler.
func NewTranslationQueries(repo domain.TranslationRepository) *TranslationQueries {
return &TranslationQueries{
repo: repo,
tracer: otel.Tracer("translation.queries"),
}
}
// Translation returns a translation by ID.
func (q *TranslationQueries) Translation(ctx context.Context, id uint) (*TranslationDTO, error) {
ctx, span := q.tracer.Start(ctx, "Translation")
defer span.End()
translation, err := q.repo.GetByID(ctx, id)
if err != nil {
return nil, err
}
if translation == nil {
return nil, nil
}
return &TranslationDTO{
ID: translation.ID,
Title: translation.Title,
Language: translation.Language,
Content: translation.Content,
TranslatableID: translation.TranslatableID,
}, nil
}
// TranslationsByWorkID returns all translations for a work.
func (q *TranslationQueries) TranslationsByWorkID(ctx context.Context, workID uint) ([]domain.Translation, error) {
ctx, span := q.tracer.Start(ctx, "TranslationsByWorkID")
defer span.End()
return q.repo.ListByWorkID(ctx, workID)
}
// TranslationsByEntity returns all translations for an entity.
func (q *TranslationQueries) TranslationsByEntity(ctx context.Context, entityType string, entityID uint) ([]domain.Translation, error) {
ctx, span := q.tracer.Start(ctx, "TranslationsByEntity")
defer span.End()
return q.repo.ListByEntity(ctx, entityType, entityID)
}
// TranslationsByTranslatorID returns all translations for a translator.
func (q *TranslationQueries) TranslationsByTranslatorID(ctx context.Context, translatorID uint) ([]domain.Translation, error) {
ctx, span := q.tracer.Start(ctx, "TranslationsByTranslatorID")
defer span.End()
return q.repo.ListByTranslatorID(ctx, translatorID)
}
// TranslationsByStatus returns all translations for a status.
func (q *TranslationQueries) TranslationsByStatus(ctx context.Context, status domain.TranslationStatus) ([]domain.Translation, error) {
ctx, span := q.tracer.Start(ctx, "TranslationsByStatus")
defer span.End()
return q.repo.ListByStatus(ctx, status)
}
// Translations returns all translations.
func (q *TranslationQueries) Translations(ctx context.Context) ([]domain.Translation, error) {
ctx, span := q.tracer.Start(ctx, "Translations")
defer span.End()
return q.repo.ListAll(ctx)
}
// ListTranslations returns a paginated list of translations for a work, with optional language filtering.
func (q *TranslationQueries) ListTranslations(ctx context.Context, workID uint, language *string, page, pageSize int) (*domain.PaginatedResult[TranslationDTO], error) {
ctx, span := q.tracer.Start(ctx, "ListTranslations")
defer span.End()
paginatedTranslations, err := q.repo.ListByWorkIDPaginated(ctx, workID, language, page, pageSize)
if err != nil {
return nil, err
}
translationDTOs := make([]TranslationDTO, len(paginatedTranslations.Items))
for i, t := range paginatedTranslations.Items {
translationDTOs[i] = TranslationDTO{
ID: t.ID,
Title: t.Title,
Language: t.Language,
Content: t.Content,
TranslatableID: t.TranslatableID,
}
}
return &domain.PaginatedResult[TranslationDTO]{
Items: translationDTOs,
TotalCount: paginatedTranslations.TotalCount,
Page: paginatedTranslations.Page,
PageSize: paginatedTranslations.PageSize,
TotalPages: paginatedTranslations.TotalPages,
}, nil
}