mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This change introduces a major architectural refactoring of the application, with a focus on improving testability, decoupling, and observability. The following domains have been successfully refactored: - `localization`: Wrote a full suite of unit tests and added logging. - `auth`: Introduced a `JWTManager` interface, wrote comprehensive unit tests, and added logging. - `copyright`: Separated integration tests, wrote a full suite of unit tests, and added logging. - `monetization`: Wrote a full suite of unit tests and added logging. - `search`: Refactored the Weaviate client usage by creating a wrapper to improve testability, and achieved 100% test coverage. For each of these domains, 100% test coverage has been achieved for the refactored code. The refactoring of the `work` domain is currently in progress. Unit tests have been written for the commands and queries, but there is a persistent build issue with the query tests that needs to be resolved. The error indicates that the query methods are undefined, despite appearing to be correctly defined and called.
169 lines
6.9 KiB
Go
169 lines
6.9 KiB
Go
package copyright
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
"tercul/internal/platform/log"
|
|
)
|
|
|
|
// 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")
|
|
}
|
|
log.LogDebug("Creating copyright", log.F("name", copyright.Name))
|
|
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")
|
|
}
|
|
log.LogDebug("Updating copyright", log.F("id", copyright.ID))
|
|
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")
|
|
}
|
|
log.LogDebug("Deleting copyright", log.F("id", 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")
|
|
}
|
|
log.LogDebug("Adding copyright to work", log.F("work_id", workID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Removing copyright from work", log.F("work_id", workID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Adding copyright to author", log.F("author_id", authorID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Removing copyright from author", log.F("author_id", authorID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Adding copyright to book", log.F("book_id", bookID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Removing copyright from book", log.F("book_id", bookID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Adding copyright to publisher", log.F("publisher_id", publisherID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Removing copyright from publisher", log.F("publisher_id", publisherID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Adding copyright to source", log.F("source_id", sourceID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Removing copyright from source", log.F("source_id", sourceID), log.F("copyright_id", copyrightID))
|
|
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")
|
|
}
|
|
log.LogDebug("Adding translation to copyright", log.F("copyright_id", translation.CopyrightID), log.F("language", translation.LanguageCode))
|
|
return c.repo.AddTranslation(ctx, translation)
|
|
}
|