tercul-backend/internal/app/work/queries.go
google-labs-jules[bot] 06e6e2be85 refactor(domain): Isolate Work aggregate
This commit isolates the `Work` aggregate into its own package at `internal/domain/work`, following the first step of the refactoring plan in `refactor.md`.

- The `Work` struct, related types, and the `WorkRepository` interface have been moved to the new package.
- A circular dependency between `domain` and `work` was resolved by moving the `AnalyticsRepository` to the `app` layer.
- All references to the moved types have been updated across the entire codebase to fix compilation errors.
- Test files, including mocks and integration tests, have been updated to reflect the new structure.
2025-10-03 16:15:09 +00:00

96 lines
2.7 KiB
Go

package work
import (
"context"
"errors"
"tercul/internal/domain"
"tercul/internal/domain/work"
)
// 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
}
// NewWorkQueries creates a new WorkQueries handler.
func NewWorkQueries(repo work.WorkRepository) *WorkQueries {
return &WorkQueries{
repo: repo,
}
}
// GetWorkByID retrieves a work by ID.
func (q *WorkQueries) GetWorkByID(ctx context.Context, id uint) (*work.Work, error) {
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) {
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) {
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) {
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) {
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) {
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) {
if language == "" {
return nil, errors.New("language cannot be empty")
}
return q.repo.FindByLanguage(ctx, language, page, pageSize)
}