mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
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.
113 lines
4.7 KiB
Go
113 lines
4.7 KiB
Go
package copyright
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/work"
|
|
"tercul/internal/platform/log"
|
|
)
|
|
|
|
// CopyrightQueries contains the query handlers for copyright.
|
|
type CopyrightQueries struct {
|
|
repo domain.CopyrightRepository
|
|
workRepo work.WorkRepository
|
|
authorRepo domain.AuthorRepository
|
|
bookRepo domain.BookRepository
|
|
publisherRepo domain.PublisherRepository
|
|
sourceRepo domain.SourceRepository
|
|
}
|
|
|
|
// NewCopyrightQueries creates a new CopyrightQueries handler.
|
|
func NewCopyrightQueries(repo domain.CopyrightRepository, workRepo work.WorkRepository, authorRepo domain.AuthorRepository, bookRepo domain.BookRepository, publisherRepo domain.PublisherRepository, sourceRepo domain.SourceRepository) *CopyrightQueries {
|
|
return &CopyrightQueries{repo: repo, workRepo: workRepo, authorRepo: authorRepo, bookRepo: bookRepo, publisherRepo: publisherRepo, sourceRepo: sourceRepo}
|
|
}
|
|
|
|
// GetCopyrightByID retrieves a copyright by ID.
|
|
func (q *CopyrightQueries) GetCopyrightByID(ctx context.Context, id uint) (*domain.Copyright, error) {
|
|
if id == 0 {
|
|
return nil, errors.New("invalid copyright ID")
|
|
}
|
|
log.LogDebug("Getting copyright by ID", log.F("id", id))
|
|
return q.repo.GetByID(ctx, id)
|
|
}
|
|
|
|
// ListCopyrights retrieves all copyrights.
|
|
func (q *CopyrightQueries) ListCopyrights(ctx context.Context) ([]domain.Copyright, error) {
|
|
log.LogDebug("Listing all copyrights")
|
|
// Note: This might need pagination in the future.
|
|
// For now, it mirrors the old service's behavior.
|
|
return q.repo.ListAll(ctx)
|
|
}
|
|
|
|
// GetCopyrightsForWork gets all copyrights for a specific work.
|
|
func (q *CopyrightQueries) GetCopyrightsForWork(ctx context.Context, workID uint) ([]*domain.Copyright, error) {
|
|
log.LogDebug("Getting copyrights for work", log.F("work_id", workID))
|
|
workRecord, err := q.workRepo.GetByIDWithOptions(ctx, workID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return workRecord.Copyrights, nil
|
|
}
|
|
|
|
// GetCopyrightsForAuthor gets all copyrights for a specific author.
|
|
func (q *CopyrightQueries) GetCopyrightsForAuthor(ctx context.Context, authorID uint) ([]*domain.Copyright, error) {
|
|
log.LogDebug("Getting copyrights for author", log.F("author_id", authorID))
|
|
author, err := q.authorRepo.GetByIDWithOptions(ctx, authorID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return author.Copyrights, nil
|
|
}
|
|
|
|
// GetCopyrightsForBook gets all copyrights for a specific book.
|
|
func (q *CopyrightQueries) GetCopyrightsForBook(ctx context.Context, bookID uint) ([]*domain.Copyright, error) {
|
|
log.LogDebug("Getting copyrights for book", log.F("book_id", bookID))
|
|
book, err := q.bookRepo.GetByIDWithOptions(ctx, bookID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return book.Copyrights, nil
|
|
}
|
|
|
|
// GetCopyrightsForPublisher gets all copyrights for a specific publisher.
|
|
func (q *CopyrightQueries) GetCopyrightsForPublisher(ctx context.Context, publisherID uint) ([]*domain.Copyright, error) {
|
|
log.LogDebug("Getting copyrights for publisher", log.F("publisher_id", publisherID))
|
|
publisher, err := q.publisherRepo.GetByIDWithOptions(ctx, publisherID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return publisher.Copyrights, nil
|
|
}
|
|
|
|
// GetCopyrightsForSource gets all copyrights for a specific source.
|
|
func (q *CopyrightQueries) GetCopyrightsForSource(ctx context.Context, sourceID uint) ([]*domain.Copyright, error) {
|
|
log.LogDebug("Getting copyrights for source", log.F("source_id", sourceID))
|
|
source, err := q.sourceRepo.GetByIDWithOptions(ctx, sourceID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return source.Copyrights, nil
|
|
}
|
|
|
|
// GetTranslations gets all translations for a copyright.
|
|
func (q *CopyrightQueries) GetTranslations(ctx context.Context, copyrightID uint) ([]domain.CopyrightTranslation, error) {
|
|
if copyrightID == 0 {
|
|
return nil, errors.New("invalid copyright ID")
|
|
}
|
|
log.LogDebug("Getting translations for copyright", log.F("copyright_id", copyrightID))
|
|
return q.repo.GetTranslations(ctx, copyrightID)
|
|
}
|
|
|
|
// GetTranslationByLanguage gets a specific translation by language code.
|
|
func (q *CopyrightQueries) GetTranslationByLanguage(ctx context.Context, copyrightID uint, languageCode string) (*domain.CopyrightTranslation, error) {
|
|
if copyrightID == 0 {
|
|
return nil, errors.New("invalid copyright ID")
|
|
}
|
|
if languageCode == "" {
|
|
return nil, errors.New("language code cannot be empty")
|
|
}
|
|
log.LogDebug("Getting translation by language for copyright", log.F("copyright_id", copyrightID), log.F("language", languageCode))
|
|
return q.repo.GetTranslationByLanguage(ctx, copyrightID, languageCode)
|
|
}
|