mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
This commit includes the following changes: - Refactored all data repositories in `internal/data/sql/` to use a consistent `sql` package and to align with the new `domain` models. - Fixed the GraphQL structure by moving the server creation logic from `internal/app` to `cmd/api`, which resolved an import cycle. - Corrected numerous incorrect import paths for packages like `graph`, `linguistics`, `syncjob`, and the legacy `models` package. - Resolved several package and function redeclaration errors. - Removed legacy migration code.
79 lines
3.1 KiB
Go
79 lines
3.1 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"gorm.io/gorm"
|
|
"tercul/internal/domain/copyright"
|
|
)
|
|
|
|
type copyrightRepository struct {
|
|
domain.BaseRepository[domain.Copyright]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCopyrightRepository creates a new CopyrightRepository.
|
|
func NewCopyrightRepository(db *gorm.DB) copyright.CopyrightRepository {
|
|
return ©rightRepository{
|
|
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(©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(&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(©rights).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(©rightables).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
|
|
}
|