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.
85 lines
3.5 KiB
Go
85 lines
3.5 KiB
Go
package monetization
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/domain/work"
|
|
"tercul/internal/platform/log"
|
|
)
|
|
|
|
// MonetizationQueries contains the query handlers for monetization.
|
|
type MonetizationQueries struct {
|
|
repo domain.MonetizationRepository
|
|
workRepo work.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 work.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")
|
|
}
|
|
log.LogDebug("Getting monetization by ID", log.F("id", id))
|
|
return q.repo.GetByID(ctx, id)
|
|
}
|
|
|
|
// ListMonetizations retrieves all monetizations.
|
|
func (q *MonetizationQueries) ListMonetizations(ctx context.Context) ([]domain.Monetization, error) {
|
|
log.LogDebug("Listing all monetizations")
|
|
return q.repo.ListAll(ctx)
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForWork(ctx context.Context, workID uint) ([]*domain.Monetization, error) {
|
|
log.LogDebug("Getting monetizations for work", log.F("work_id", workID))
|
|
workRecord, err := q.workRepo.GetByIDWithOptions(ctx, workID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return workRecord.Monetizations, nil
|
|
}
|
|
|
|
func (q *MonetizationQueries) GetMonetizationsForAuthor(ctx context.Context, authorID uint) ([]*domain.Monetization, error) {
|
|
log.LogDebug("Getting monetizations for author", log.F("author_id", authorID))
|
|
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) {
|
|
log.LogDebug("Getting monetizations for book", log.F("book_id", bookID))
|
|
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) {
|
|
log.LogDebug("Getting monetizations for publisher", log.F("publisher_id", publisherID))
|
|
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) {
|
|
log.LogDebug("Getting monetizations for source", log.F("source_id", sourceID))
|
|
source, err := q.sourceRepo.GetByIDWithOptions(ctx, sourceID, &domain.QueryOptions{Preloads: []string{"Monetizations"}})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return source.Monetizations, nil
|
|
}
|