package repositories import ( "context" "errors" "gorm.io/gorm" "tercul/models" ) // CopyrightRepository defines CRUD methods specific to Copyright. type CopyrightRepository interface { BaseRepository[models.Copyright] // Polymorphic methods AttachToEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error DetachFromEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error GetByEntity(ctx context.Context, entityID uint, entityType string) ([]models.Copyright, error) GetEntitiesByCopyright(ctx context.Context, copyrightID uint) ([]models.Copyrightable, error) // Translation methods AddTranslation(ctx context.Context, translation *models.CopyrightTranslation) error GetTranslations(ctx context.Context, copyrightID uint) ([]models.CopyrightTranslation, error) GetTranslationByLanguage(ctx context.Context, copyrightID uint, languageCode string) (*models.CopyrightTranslation, error) } type copyrightRepository struct { BaseRepository[models.Copyright] db *gorm.DB } // NewCopyrightRepository creates a new CopyrightRepository. func NewCopyrightRepository(db *gorm.DB) CopyrightRepository { return ©rightRepository{ BaseRepository: NewBaseRepositoryImpl[models.Copyright](db), db: db, } } // AttachToEntity attaches a copyright to any entity type func (r *copyrightRepository) AttachToEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error { copyrightable := models.Copyrightable{ CopyrightID: copyrightID, CopyrightableID: entityID, CopyrightableType: entityType, } return r.db.WithContext(ctx).Create(©rightable).Error } // DetachFromEntity removes a copyright from an entity func (r *copyrightRepository) DetachFromEntity(ctx context.Context, copyrightID uint, entityID uint, entityType string) error { return r.db.WithContext(ctx).Where("copyright_id = ? AND copyrightable_id = ? AND copyrightable_type = ?", copyrightID, entityID, entityType).Delete(&models.Copyrightable{}).Error } // GetByEntity gets all copyrights for a specific entity func (r *copyrightRepository) GetByEntity(ctx context.Context, entityID uint, entityType string) ([]models.Copyright, error) { var copyrights []models.Copyright err := r.db.WithContext(ctx).Joins("JOIN copyrightables ON copyrightables.copyright_id = copyrights.id"). Where("copyrightables.copyrightable_id = ? AND copyrightables.copyrightable_type = ?", entityID, entityType). Preload("Translations"). Find(©rights).Error return copyrights, err } // GetEntitiesByCopyright gets all entities that have a specific copyright func (r *copyrightRepository) GetEntitiesByCopyright(ctx context.Context, copyrightID uint) ([]models.Copyrightable, error) { var copyrightables []models.Copyrightable err := r.db.WithContext(ctx).Where("copyright_id = ?", copyrightID).Find(©rightables).Error return copyrightables, err } // AddTranslation adds a translation to a copyright func (r *copyrightRepository) AddTranslation(ctx context.Context, translation *models.CopyrightTranslation) error { return r.db.WithContext(ctx).Create(translation).Error } // GetTranslations gets all translations for a copyright func (r *copyrightRepository) GetTranslations(ctx context.Context, copyrightID uint) ([]models.CopyrightTranslation, error) { var translations []models.CopyrightTranslation err := r.db.WithContext(ctx).Where("copyright_id = ?", copyrightID).Find(&translations).Error return translations, err } // GetTranslationByLanguage gets a specific translation by language code func (r *copyrightRepository) GetTranslationByLanguage(ctx context.Context, copyrightID uint, languageCode string) (*models.CopyrightTranslation, error) { var translation models.CopyrightTranslation err := r.db.WithContext(ctx).Where("copyright_id = ? AND language_code = ?", copyrightID, languageCode).First(&translation).Error if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { return nil, ErrEntityNotFound } return nil, err } return &translation, nil }