tercul-backend/internal/data/sql/copyright_repository.go
2025-09-05 21:37:42 +00:00

81 lines
3.2 KiB
Go

package sql
import (
"context"
"errors"
"tercul/internal/domain"
"tercul/internal/domain/copyright"
"gorm.io/gorm"
)
type copyrightRepository struct {
domain.BaseRepository[domain.Copyright]
db *gorm.DB
}
// NewCopyrightRepository creates a new CopyrightRepository.
func NewCopyrightRepository(db *gorm.DB) copyright.CopyrightRepository {
return &copyrightRepository{
BaseRepository: NewBaseRepositoryImpl[domain.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 := domain.Copyrightable{
CopyrightID: copyrightID,
CopyrightableID: entityID,
CopyrightableType: entityType,
}
return r.db.WithContext(ctx).Create(&copyrightable).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(&domain.Copyrightable{}).Error
}
// GetByEntity gets all copyrights for a specific entity
func (r *copyrightRepository) GetByEntity(ctx context.Context, entityID uint, entityType string) ([]domain.Copyright, error) {
var copyrights []domain.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(&copyrights).Error
return copyrights, err
}
// GetEntitiesByCopyright gets all entities that have a specific copyright
func (r *copyrightRepository) GetEntitiesByCopyright(ctx context.Context, copyrightID uint) ([]domain.Copyrightable, error) {
var copyrightables []domain.Copyrightable
err := r.db.WithContext(ctx).Where("copyright_id = ?", copyrightID).Find(&copyrightables).Error
return copyrightables, err
}
// AddTranslation adds a translation to a copyright
func (r *copyrightRepository) AddTranslation(ctx context.Context, translation *domain.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) ([]domain.CopyrightTranslation, error) {
var translations []domain.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) (*domain.CopyrightTranslation, error) {
var translation domain.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
}