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.
90 lines
3.5 KiB
Go
90 lines
3.5 KiB
Go
package monetization
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// MonetizationCommands contains the command handlers for monetization.
|
|
type MonetizationCommands struct {
|
|
repo domain.MonetizationRepository
|
|
}
|
|
|
|
// NewMonetizationCommands creates a new MonetizationCommands handler.
|
|
func NewMonetizationCommands(repo domain.MonetizationRepository) *MonetizationCommands {
|
|
return &MonetizationCommands{repo: repo}
|
|
}
|
|
|
|
// AddMonetizationToWork adds a monetization to a work.
|
|
func (c *MonetizationCommands) AddMonetizationToWork(ctx context.Context, workID uint, monetizationID uint) error {
|
|
if workID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid work ID or monetization ID")
|
|
}
|
|
return c.repo.AddMonetizationToWork(ctx, workID, monetizationID)
|
|
}
|
|
|
|
// RemoveMonetizationFromWork removes a monetization from a work.
|
|
func (c *MonetizationCommands) RemoveMonetizationFromWork(ctx context.Context, workID uint, monetizationID uint) error {
|
|
if workID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid work ID or monetization ID")
|
|
}
|
|
return c.repo.RemoveMonetizationFromWork(ctx, workID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) AddMonetizationToAuthor(ctx context.Context, authorID uint, monetizationID uint) error {
|
|
if authorID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid author ID or monetization ID")
|
|
}
|
|
return c.repo.AddMonetizationToAuthor(ctx, authorID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) RemoveMonetizationFromAuthor(ctx context.Context, authorID uint, monetizationID uint) error {
|
|
if authorID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid author ID or monetization ID")
|
|
}
|
|
return c.repo.RemoveMonetizationFromAuthor(ctx, authorID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) AddMonetizationToBook(ctx context.Context, bookID uint, monetizationID uint) error {
|
|
if bookID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid book ID or monetization ID")
|
|
}
|
|
return c.repo.AddMonetizationToBook(ctx, bookID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) RemoveMonetizationFromBook(ctx context.Context, bookID uint, monetizationID uint) error {
|
|
if bookID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid book ID or monetization ID")
|
|
}
|
|
return c.repo.RemoveMonetizationFromBook(ctx, bookID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) AddMonetizationToPublisher(ctx context.Context, publisherID uint, monetizationID uint) error {
|
|
if publisherID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid publisher ID or monetization ID")
|
|
}
|
|
return c.repo.AddMonetizationToPublisher(ctx, publisherID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) RemoveMonetizationFromPublisher(ctx context.Context, publisherID uint, monetizationID uint) error {
|
|
if publisherID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid publisher ID or monetization ID")
|
|
}
|
|
return c.repo.RemoveMonetizationFromPublisher(ctx, publisherID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) AddMonetizationToSource(ctx context.Context, sourceID uint, monetizationID uint) error {
|
|
if sourceID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid source ID or monetization ID")
|
|
}
|
|
return c.repo.AddMonetizationToSource(ctx, sourceID, monetizationID)
|
|
}
|
|
|
|
func (c *MonetizationCommands) RemoveMonetizationFromSource(ctx context.Context, sourceID uint, monetizationID uint) error {
|
|
if sourceID == 0 || monetizationID == 0 {
|
|
return errors.New("invalid source ID or monetization ID")
|
|
}
|
|
return c.repo.RemoveMonetizationFromSource(ctx, sourceID, monetizationID)
|
|
}
|