tercul-backend/internal/app/copyright/commands.go
Damir Mukimov d50722dad5
Some checks failed
Test / Integration Tests (push) Successful in 4s
Build / Build Binary (push) Failing after 2m9s
Docker Build / Build Docker Image (push) Failing after 2m32s
Test / Unit Tests (push) Failing after 3m12s
Lint / Go Lint (push) Failing after 1m0s
Refactor ID handling to use UUIDs across the application
- Updated database models and repositories to replace uint IDs with UUIDs.
- Modified test fixtures to generate and use UUIDs for authors, translations, users, and works.
- Adjusted mock implementations to align with the new UUID structure.
- Ensured all relevant functions and methods are updated to handle UUIDs correctly.
- Added necessary imports for UUID handling in various files.
2025-12-27 00:33:34 +01:00

171 lines
7.3 KiB
Go

package copyright
import (
"context"
"errors"
"tercul/internal/domain"
"tercul/internal/platform/log"
"github.com/google/uuid"
)
// 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.FromContext(ctx).With("name", copyright.Name).Debug("Creating copyright")
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 == uuid.Nil {
return errors.New("copyright ID 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.FromContext(ctx).With("id", copyright.ID).Debug("Updating copyright")
return c.repo.Update(ctx, copyright)
}
// DeleteCopyright deletes a copyright.
func (c *CopyrightCommands) DeleteCopyright(ctx context.Context, id uuid.UUID) error {
if id == uuid.Nil {
return errors.New("invalid copyright ID")
}
log.FromContext(ctx).With("id", id).Debug("Deleting copyright")
return c.repo.Delete(ctx, id)
}
// AddCopyrightToWork adds a copyright to a work.
func (c *CopyrightCommands) AddCopyrightToWork(ctx context.Context, workID uuid.UUID, copyrightID uuid.UUID) error {
if workID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid work ID or copyright ID")
}
log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Adding copyright to work")
return c.repo.AddCopyrightToWork(ctx, workID, copyrightID)
}
// RemoveCopyrightFromWork removes a copyright from a work.
func (c *CopyrightCommands) RemoveCopyrightFromWork(ctx context.Context, workID uuid.UUID, copyrightID uuid.UUID) error {
if workID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid work ID or copyright ID")
}
log.FromContext(ctx).With("work_id", workID).With("copyright_id", copyrightID).Debug("Removing copyright from work")
return c.repo.RemoveCopyrightFromWork(ctx, workID, copyrightID)
}
// AddCopyrightToAuthor adds a copyright to an author.
func (c *CopyrightCommands) AddCopyrightToAuthor(ctx context.Context, authorID uuid.UUID, copyrightID uuid.UUID) error {
if authorID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid author ID or copyright ID")
}
log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Adding copyright to author")
return c.repo.AddCopyrightToAuthor(ctx, authorID, copyrightID)
}
// RemoveCopyrightFromAuthor removes a copyright from an author.
func (c *CopyrightCommands) RemoveCopyrightFromAuthor(ctx context.Context, authorID uuid.UUID, copyrightID uuid.UUID) error {
if authorID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid author ID or copyright ID")
}
log.FromContext(ctx).With("author_id", authorID).With("copyright_id", copyrightID).Debug("Removing copyright from author")
return c.repo.RemoveCopyrightFromAuthor(ctx, authorID, copyrightID)
}
// AddCopyrightToBook adds a copyright to a book.
func (c *CopyrightCommands) AddCopyrightToBook(ctx context.Context, bookID uuid.UUID, copyrightID uuid.UUID) error {
if bookID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid book ID or copyright ID")
}
log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Adding copyright to book")
return c.repo.AddCopyrightToBook(ctx, bookID, copyrightID)
}
// RemoveCopyrightFromBook removes a copyright from a book.
func (c *CopyrightCommands) RemoveCopyrightFromBook(ctx context.Context, bookID uuid.UUID, copyrightID uuid.UUID) error {
if bookID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid book ID or copyright ID")
}
log.FromContext(ctx).With("book_id", bookID).With("copyright_id", copyrightID).Debug("Removing copyright from book")
return c.repo.RemoveCopyrightFromBook(ctx, bookID, copyrightID)
}
// AddCopyrightToPublisher adds a copyright to a publisher.
func (c *CopyrightCommands) AddCopyrightToPublisher(ctx context.Context, publisherID uuid.UUID, copyrightID uuid.UUID) error {
if publisherID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid publisher ID or copyright ID")
}
log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Adding copyright to publisher")
return c.repo.AddCopyrightToPublisher(ctx, publisherID, copyrightID)
}
// RemoveCopyrightFromPublisher removes a copyright from a publisher.
func (c *CopyrightCommands) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uuid.UUID, copyrightID uuid.UUID) error {
if publisherID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid publisher ID or copyright ID")
}
log.FromContext(ctx).With("publisher_id", publisherID).With("copyright_id", copyrightID).Debug("Removing copyright from publisher")
return c.repo.RemoveCopyrightFromPublisher(ctx, publisherID, copyrightID)
}
// AddCopyrightToSource adds a copyright to a source.
func (c *CopyrightCommands) AddCopyrightToSource(ctx context.Context, sourceID uuid.UUID, copyrightID uuid.UUID) error {
if sourceID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid source ID or copyright ID")
}
log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Adding copyright to source")
return c.repo.AddCopyrightToSource(ctx, sourceID, copyrightID)
}
// RemoveCopyrightFromSource removes a copyright from a source.
func (c *CopyrightCommands) RemoveCopyrightFromSource(ctx context.Context, sourceID uuid.UUID, copyrightID uuid.UUID) error {
if sourceID == uuid.Nil || copyrightID == uuid.Nil {
return errors.New("invalid source ID or copyright ID")
}
log.FromContext(ctx).With("source_id", sourceID).With("copyright_id", copyrightID).Debug("Removing copyright from source")
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 == uuid.Nil {
return errors.New("copyright ID cannot be nil")
}
if translation.LanguageCode == "" {
return errors.New("language code cannot be empty")
}
if translation.Message == "" {
return errors.New("translation message cannot be empty")
}
log.FromContext(ctx).With("copyright_id", translation.CopyrightID).With("language", translation.LanguageCode).Debug("Adding translation to copyright")
return c.repo.AddTranslation(ctx, translation)
}