mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
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.
76 lines
2.9 KiB
Go
76 lines
2.9 KiB
Go
package monetization
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// MonetizationQueries contains the query handlers for monetization.
|
|
type MonetizationQueries struct {
|
|
repo domain.MonetizationRepository
|
|
workRepo domain.WorkRepository
|
|
authorRepo domain.AuthorRepository
|
|
bookRepo domain.BookRepository
|
|
publisherRepo domain.PublisherRepository
|
|
sourceRepo domain.SourceRepository
|
|
}
|
|
|
|
// NewMonetizationQueries creates a new MonetizationQueries handler.
|
|
func NewMonetizationQueries(repo domain.MonetizationRepository, workRepo domain.WorkRepository, authorRepo domain.AuthorRepository, bookRepo domain.BookRepository, publisherRepo domain.PublisherRepository, sourceRepo domain.SourceRepository) *MonetizationQueries {
|
|
return &MonetizationQueries{repo: repo, workRepo: workRepo, authorRepo: authorRepo, bookRepo: bookRepo, publisherRepo: publisherRepo, sourceRepo: sourceRepo}
|
|
}
|
|
|
|
// GetMonetizationByID retrieves a monetization by ID.
|
|
func (q *MonetizationQueries) GetMonetizationByID(ctx context.Context, id uint) (*domain.Monetization, error) {
|
|
if id == 0 {
|
|
return nil, errors.New("invalid monetization ID")
|
|
}
|
|
return q.repo.GetByID(ctx, id)
|
|
}
|
|
|
|
// ListMonetizations retrieves all monetizations.
|
|
func (q *MonetizationQueries) ListMonetizations(ctx context.Context) ([]domain.Monetization, error) {
|
|
return q.repo.ListAll(ctx)
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForWork(ctx context.Context, workID uint) ([]*domain.Monetization, error) {
|
|
work, err := q.workRepo.GetByIDWithOptions(ctx, workID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return work.Monetizations, nil
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForAuthor(ctx context.Context, authorID uint) ([]*domain.Monetization, error) {
|
|
author, err := q.authorRepo.GetByIDWithOptions(ctx, authorID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return author.Monetizations, nil
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForBook(ctx context.Context, bookID uint) ([]*domain.Monetization, error) {
|
|
book, err := q.bookRepo.GetByIDWithOptions(ctx, bookID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return book.Monetizations, nil
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForPublisher(ctx context.Context, publisherID uint) ([]*domain.Monetization, error) {
|
|
publisher, err := q.publisherRepo.GetByIDWithOptions(ctx, publisherID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return publisher.Monetizations, nil
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForSource(ctx context.Context, sourceID uint) ([]*domain.Monetization, error) {
|
|
source, err := q.sourceRepo.GetByIDWithOptions(ctx, sourceID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return source.Monetizations, nil
|
|
}
|