tercul-backend/internal/app/work/queries.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

95 lines
2.7 KiB
Go

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