mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
96 lines
3.0 KiB
Go
96 lines
3.0 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)
|
|
}
|
|
|
|
// AttachCopyrightToEntity attaches a copyright to any entity type.
|
|
func (c *CopyrightCommands) AttachCopyrightToEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error {
|
|
if copyrightID == 0 || entityID == 0 {
|
|
return errors.New("invalid copyright ID or entity ID")
|
|
}
|
|
if entityType == "" {
|
|
return errors.New("entity type cannot be empty")
|
|
}
|
|
return c.repo.AttachToEntity(ctx, copyrightID, entityID, entityType)
|
|
}
|
|
|
|
// DetachCopyrightFromEntity removes a copyright from an entity.
|
|
func (c *CopyrightCommands) DetachCopyrightFromEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error {
|
|
if copyrightID == 0 || entityID == 0 {
|
|
return errors.New("invalid copyright ID or entity ID")
|
|
}
|
|
if entityType == "" {
|
|
return errors.New("entity type cannot be empty")
|
|
}
|
|
return c.repo.DetachFromEntity(ctx, copyrightID, entityID, entityType)
|
|
}
|
|
|
|
// 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)
|
|
}
|