mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
- Fixes a potential panic in the background job queue by changing `log.Fatalf` to `log.Printf`, allowing for more graceful error handling.
- Implements all `panic("not implemented")` methods in the mock repositories for `Like`, `Work`, and `User`, enabling more robust testing.
- Consolidates duplicated `WorkAnalytics` and `TranslationAnalytics` structs into a central `internal/domain/analytics` package to reduce code duplication and improve maintainability.
- Corrects build errors that arose during testing, including an unused import and an incorrect struct field name in a mock repository.
93 lines
2.8 KiB
Go
93 lines
2.8 KiB
Go
package work
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/work"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// 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)
|
|
}
|