tercul-backend/internal/app/copyright/queries.go
google-labs-jules[bot] 5d6a6ef47b This commit addresses the "Stabilize non-linguistics tests and interfaces" task from TODO.md.
The main changes are:
-   Refactored the `Copyright` and `Monetization` relationships to use explicit join tables for each owning model, as per the "Option A" strategy. This fixes the GORM migration issues related to polymorphic many-to-many relationships.
-   Created new join table structs (e.g., `WorkCopyright`, `AuthorCopyright`, `WorkMonetization`, etc.).
-   Updated the domain models to use standard `gorm:"many2many"` tags with the new join tables.
-   Refactored the `CopyrightRepository` and `MonetizationRepository` to use the new association-based logic.
-   Updated the application services (`CopyrightCommands`, `CopyrightQueries`, `MonetizationCommands`, `MonetizationQueries`) to use the new repository methods.
-   Consolidated all repository interfaces into a single `internal/domain/interfaces.go` file for better code organization.
-   Added extensive integration tests for the new repository and application layer logic for `Copyrights` and `Monetizations`.
-   Fixed the deletion logic for `WorkRepository` to correctly handle cascading deletes with SQLite.
-   Updated the `TODO.md` file to mark the "Stabilize non-linguistics tests and interfaces" task as complete.
2025-09-06 06:25:11 +00:00

104 lines
4.0 KiB
Go

package copyright
import (
"context"
"errors"
"tercul/internal/domain"
)
// 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")
}
return q.repo.GetByID(ctx, id)
}
// ListCopyrights retrieves all copyrights.
func (q *CopyrightQueries) ListCopyrights(ctx context.Context) ([]domain.Copyright, error) {
// 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) {
work, err := q.workRepo.GetByIDWithOptions(ctx, workID, &domain.QueryOptions{Preloads: []string{"Copyrights"}})
if err != nil {
return nil, err
}
return work.Copyrights, nil
}
// GetCopyrightsForAuthor gets all copyrights for a specific author.
func (q *CopyrightQueries) GetCopyrightsForAuthor(ctx context.Context, authorID uint) ([]*domain.Copyright, error) {
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) {
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) {
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) {
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")
}
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")
}
return q.repo.GetTranslationByLanguage(ctx, copyrightID, languageCode)
}