tercul-backend/internal/app/copyright/queries.go
google-labs-jules[bot] fa90dd79da feat: Complete large-scale refactor and prepare for production
This commit marks the completion of a major refactoring effort to stabilize the codebase, improve its structure, and prepare it for production.

The key changes include:

- **Domain Layer Consolidation:** The `Work` entity and its related types, along with all other domain entities and repository interfaces, have been consolidated into the main `internal/domain` package. This eliminates import cycles and provides a single, coherent source of truth for the domain model.

- **Data Access Layer Refactoring:** The repository implementations in `internal/data/sql` have been updated to align with the new domain layer. The `BaseRepositoryImpl` has been corrected to use pointer receivers, and all concrete repositories now correctly embed it, ensuring consistent and correct behavior.

- **Application Layer Stabilization:** All application services in `internal/app` have been updated to use the new domain types and repository interfaces. Dependency injection has been corrected throughout the application, ensuring that all services are initialized with the correct dependencies.

- **GraphQL Adapter Fixes:** The GraphQL resolver implementation in `internal/adapters/graphql` has been updated to correctly handle the new domain types and service methods. The auto-generated GraphQL code has been regenerated to ensure it is in sync with the schema and runtime.

- **Test Suite Overhaul:** All test suites have been fixed to correctly implement their respective interfaces and use the updated domain model. Mock repositories and test suites have been corrected to properly embed the `testify` base types, resolving numerous build and linter errors.

- **Dependency Management:** The Go modules have been tidied, and the module cache has been cleaned to ensure a consistent and correct dependency graph.

- **Code Quality and Verification:** The entire codebase now passes all builds, tests, and linter checks, ensuring a high level of quality and stability.

This comprehensive effort has resulted in a more robust, maintainable, and production-ready application.
2025-10-07 11:09:37 +00:00

112 lines
4.8 KiB
Go

package copyright
import (
"context"
"errors"
"tercul/internal/domain"
"tercul/internal/platform/log"
)
// CopyrightQueries contains the query handlers for copyright.
type CopyrightQueries struct {
repo domain.CopyrightRepository
workRepo domain.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 domain.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.FromContext(ctx).With("id", id).Debug("Getting copyright by ID")
return q.repo.GetByID(ctx, id)
}
// ListCopyrights retrieves all copyrights.
func (q *CopyrightQueries) ListCopyrights(ctx context.Context) ([]domain.Copyright, error) {
log.FromContext(ctx).Debug("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.FromContext(ctx).With("work_id", workID).Debug("Getting copyrights for work")
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.FromContext(ctx).With("author_id", authorID).Debug("Getting copyrights for author")
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.FromContext(ctx).With("book_id", bookID).Debug("Getting copyrights for book")
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.FromContext(ctx).With("publisher_id", publisherID).Debug("Getting copyrights for publisher")
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.FromContext(ctx).With("source_id", sourceID).Debug("Getting copyrights for source")
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.FromContext(ctx).With("copyright_id", copyrightID).Debug("Getting translations for copyright")
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.FromContext(ctx).With("copyright_id", copyrightID).With("language", languageCode).Debug("Getting translation by language for copyright")
return q.repo.GetTranslationByLanguage(ctx, copyrightID, languageCode)
}