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.
155 lines
5.4 KiB
Go
155 lines
5.4 KiB
Go
package copyright
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
)
|
|
|
|
// CopyrightCommands contains the command handlers for copyright.
|
|
type CopyrightCommands struct {
|
|
repo domain.CopyrightRepository
|
|
}
|
|
|
|
// NewCopyrightCommands creates a new CopyrightCommands handler.
|
|
func NewCopyrightCommands(repo domain.CopyrightRepository) *CopyrightCommands {
|
|
return &CopyrightCommands{repo: repo}
|
|
}
|
|
|
|
// CreateCopyright creates a new copyright.
|
|
func (c *CopyrightCommands) CreateCopyright(ctx context.Context, copyright *domain.Copyright) error {
|
|
if copyright == nil {
|
|
return errors.New("copyright cannot be nil")
|
|
}
|
|
if copyright.Name == "" {
|
|
return errors.New("copyright name cannot be empty")
|
|
}
|
|
if copyright.Identificator == "" {
|
|
return errors.New("copyright identificator cannot be empty")
|
|
}
|
|
return c.repo.Create(ctx, copyright)
|
|
}
|
|
|
|
// UpdateCopyright updates an existing copyright.
|
|
func (c *CopyrightCommands) UpdateCopyright(ctx context.Context, copyright *domain.Copyright) error {
|
|
if copyright == nil {
|
|
return errors.New("copyright cannot be nil")
|
|
}
|
|
if copyright.ID == 0 {
|
|
return errors.New("copyright ID cannot be zero")
|
|
}
|
|
if copyright.Name == "" {
|
|
return errors.New("copyright name cannot be empty")
|
|
}
|
|
if copyright.Identificator == "" {
|
|
return errors.New("copyright identificator cannot be empty")
|
|
}
|
|
return c.repo.Update(ctx, copyright)
|
|
}
|
|
|
|
// DeleteCopyright deletes a copyright.
|
|
func (c *CopyrightCommands) DeleteCopyright(ctx context.Context, id uint) error {
|
|
if id == 0 {
|
|
return errors.New("invalid copyright ID")
|
|
}
|
|
return c.repo.Delete(ctx, id)
|
|
}
|
|
|
|
|
|
// AddCopyrightToWork adds a copyright to a work.
|
|
func (c *CopyrightCommands) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
if workID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid work ID or copyright ID")
|
|
}
|
|
return c.repo.AddCopyrightToWork(ctx, workID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromWork removes a copyright from a work.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
if workID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid work ID or copyright ID")
|
|
}
|
|
return c.repo.RemoveCopyrightFromWork(ctx, workID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToAuthor adds a copyright to an author.
|
|
func (c *CopyrightCommands) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
if authorID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid author ID or copyright ID")
|
|
}
|
|
return c.repo.AddCopyrightToAuthor(ctx, authorID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromAuthor removes a copyright from an author.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
if authorID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid author ID or copyright ID")
|
|
}
|
|
return c.repo.RemoveCopyrightFromAuthor(ctx, authorID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToBook adds a copyright to a book.
|
|
func (c *CopyrightCommands) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
if bookID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid book ID or copyright ID")
|
|
}
|
|
return c.repo.AddCopyrightToBook(ctx, bookID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromBook removes a copyright from a book.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
if bookID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid book ID or copyright ID")
|
|
}
|
|
return c.repo.RemoveCopyrightFromBook(ctx, bookID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToPublisher adds a copyright to a publisher.
|
|
func (c *CopyrightCommands) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
if publisherID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid publisher ID or copyright ID")
|
|
}
|
|
return c.repo.AddCopyrightToPublisher(ctx, publisherID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromPublisher removes a copyright from a publisher.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
if publisherID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid publisher ID or copyright ID")
|
|
}
|
|
return c.repo.RemoveCopyrightFromPublisher(ctx, publisherID, copyrightID)
|
|
}
|
|
|
|
// AddCopyrightToSource adds a copyright to a source.
|
|
func (c *CopyrightCommands) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
if sourceID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid source ID or copyright ID")
|
|
}
|
|
return c.repo.AddCopyrightToSource(ctx, sourceID, copyrightID)
|
|
}
|
|
|
|
// RemoveCopyrightFromSource removes a copyright from a source.
|
|
func (c *CopyrightCommands) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
if sourceID == 0 || copyrightID == 0 {
|
|
return errors.New("invalid source ID or copyright ID")
|
|
}
|
|
return c.repo.RemoveCopyrightFromSource(ctx, sourceID, copyrightID)
|
|
}
|
|
|
|
// AddTranslation adds a translation to a copyright.
|
|
func (c *CopyrightCommands) AddTranslation(ctx context.Context, translation *domain.CopyrightTranslation) error {
|
|
if translation == nil {
|
|
return errors.New("translation cannot be nil")
|
|
}
|
|
if translation.CopyrightID == 0 {
|
|
return errors.New("copyright ID cannot be zero")
|
|
}
|
|
if translation.LanguageCode == "" {
|
|
return errors.New("language code cannot be empty")
|
|
}
|
|
if translation.Message == "" {
|
|
return errors.New("translation message cannot be empty")
|
|
}
|
|
return c.repo.AddTranslation(ctx, translation)
|
|
}
|