tercul-backend/internal/app/work/queries.go
google-labs-jules[bot] ac29aaa1d5 This is a work-in-progress commit for the core architectural refactoring of configuration handling.
The goal of this refactoring is to eliminate the global configuration singleton (`config.Cfg`) and replace it with explicit dependency injection of a `Config` struct.

This commit includes the following partial changes:
- The `Config` struct in `internal/platform/config/config.go` has been updated with all necessary fields.
- Several platform packages (`db`, `cache`, `auth`, `http`, `jobs/sync`) have been modified to accept the `*config.Config` struct.
- The API server entry point (`cmd/api/main.go`) has been updated to load and provide the configuration.
- A new worker entry point (`cmd/worker/main.go`) has been created to house the background job runner, as per the architecture defined in `refactor.md`.

NOTE: The build is currently broken as this refactoring is incomplete. This commit is for saving progress as requested.
2025-10-05 15:16:22 +00:00

115 lines
3.3 KiB
Go

package work
import (
"context"
"errors"
"tercul/internal/domain"
"tercul/internal/domain/work"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/trace"
)
// WorkAnalytics contains analytics data for a work
type WorkAnalytics struct {
WorkID uint
ViewCount int64
LikeCount int64
CommentCount int64
BookmarkCount int64
TranslationCount int64
ReadabilityScore float64
SentimentScore float64
TopKeywords []string
PopularTranslations []TranslationAnalytics
}
// TranslationAnalytics contains analytics data for a translation
type TranslationAnalytics struct {
TranslationID uint
Language string
ViewCount int64
LikeCount int64
}
// WorkQueries contains the query handlers for the work aggregate.
type WorkQueries struct {
repo work.WorkRepository
tracer trace.Tracer
}
// NewWorkQueries creates a new WorkQueries handler.
func NewWorkQueries(repo work.WorkRepository) *WorkQueries {
return &WorkQueries{
repo: repo,
tracer: otel.Tracer("work.queries"),
}
}
// GetWorkByID retrieves a work by ID.
func (q *WorkQueries) GetWorkByID(ctx context.Context, id uint) (*work.Work, error) {
ctx, span := q.tracer.Start(ctx, "GetWorkByID")
defer span.End()
if id == 0 {
return nil, errors.New("invalid work ID")
}
return q.repo.GetByID(ctx, id)
}
// ListWorks returns a paginated list of works.
func (q *WorkQueries) ListWorks(ctx context.Context, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
ctx, span := q.tracer.Start(ctx, "ListWorks")
defer span.End()
return q.repo.List(ctx, page, pageSize)
}
// GetWorkWithTranslations retrieves a work with its translations.
func (q *WorkQueries) GetWorkWithTranslations(ctx context.Context, id uint) (*work.Work, error) {
ctx, span := q.tracer.Start(ctx, "GetWorkWithTranslations")
defer span.End()
if id == 0 {
return nil, errors.New("invalid work ID")
}
return q.repo.GetWithTranslations(ctx, id)
}
// FindWorksByTitle finds works by title.
func (q *WorkQueries) FindWorksByTitle(ctx context.Context, title string) ([]work.Work, error) {
ctx, span := q.tracer.Start(ctx, "FindWorksByTitle")
defer span.End()
if title == "" {
return nil, errors.New("title cannot be empty")
}
return q.repo.FindByTitle(ctx, title)
}
// FindWorksByAuthor finds works by author ID.
func (q *WorkQueries) FindWorksByAuthor(ctx context.Context, authorID uint) ([]work.Work, error) {
ctx, span := q.tracer.Start(ctx, "FindWorksByAuthor")
defer span.End()
if authorID == 0 {
return nil, errors.New("invalid author ID")
}
return q.repo.FindByAuthor(ctx, authorID)
}
// FindWorksByCategory finds works by category ID.
func (q *WorkQueries) FindWorksByCategory(ctx context.Context, categoryID uint) ([]work.Work, error) {
ctx, span := q.tracer.Start(ctx, "FindWorksByCategory")
defer span.End()
if categoryID == 0 {
return nil, errors.New("invalid category ID")
}
return q.repo.FindByCategory(ctx, categoryID)
}
// FindWorksByLanguage finds works by language.
func (q *WorkQueries) FindWorksByLanguage(ctx context.Context, language string, page, pageSize int) (*domain.PaginatedResult[work.Work], error) {
ctx, span := q.tracer.Start(ctx, "FindWorksByLanguage")
defer span.End()
if language == "" {
return nil, errors.New("language cannot be empty")
}
return q.repo.FindByLanguage(ctx, language, page, pageSize)
}