mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
The main changes are: - Refactored the `Copyright` and `Monetization` relationships to use explicit join tables for each owning model, as per the "Option A" strategy. This fixes the GORM migration issues related to polymorphic many-to-many relationships. - Created new join table structs (e.g., `WorkCopyright`, `AuthorCopyright`, `WorkMonetization`, etc.). - Updated the domain models to use standard `gorm:"many2many"` tags with the new join tables. - Refactored the `CopyrightRepository` and `MonetizationRepository` to use the new association-based logic. - Updated the application services (`CopyrightCommands`, `CopyrightQueries`, `MonetizationCommands`, `MonetizationQueries`) to use the new repository methods. - Consolidated all repository interfaces into a single `internal/domain/interfaces.go` file for better code organization. - Added extensive integration tests for the new repository and application layer logic for `Copyrights` and `Monetizations`. - Fixed the deletion logic for `WorkRepository` to correctly handle cascading deletes with SQLite. - Updated the `TODO.md` file to mark the "Stabilize non-linguistics tests and interfaces" task as complete.
109 lines
5.4 KiB
Go
109 lines
5.4 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"tercul/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type copyrightRepository struct {
|
|
domain.BaseRepository[domain.Copyright]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewCopyrightRepository creates a new CopyrightRepository.
|
|
func NewCopyrightRepository(db *gorm.DB) domain.CopyrightRepository {
|
|
return ©rightRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Copyright](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
|
|
// 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
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
work := &domain.Work{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: workID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(work).Association("Copyrights").Append(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromWork(ctx context.Context, workID uint, copyrightID uint) error {
|
|
work := &domain.Work{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: workID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(work).Association("Copyrights").Delete(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
author := &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: authorID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(author).Association("Copyrights").Append(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromAuthor(ctx context.Context, authorID uint, copyrightID uint) error {
|
|
author := &domain.Author{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: authorID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(author).Association("Copyrights").Delete(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
book := &domain.Book{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: bookID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(book).Association("Copyrights").Append(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromBook(ctx context.Context, bookID uint, copyrightID uint) error {
|
|
book := &domain.Book{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: bookID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(book).Association("Copyrights").Delete(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
publisher := &domain.Publisher{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: publisherID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(publisher).Association("Copyrights").Append(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromPublisher(ctx context.Context, publisherID uint, copyrightID uint) error {
|
|
publisher := &domain.Publisher{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: publisherID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(publisher).Association("Copyrights").Delete(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) AddCopyrightToSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
source := &domain.Source{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: sourceID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(source).Association("Copyrights").Append(copyright)
|
|
}
|
|
|
|
func (r *copyrightRepository) RemoveCopyrightFromSource(ctx context.Context, sourceID uint, copyrightID uint) error {
|
|
source := &domain.Source{TranslatableModel: domain.TranslatableModel{BaseModel: domain.BaseModel{ID: sourceID}}}
|
|
copyright := &domain.Copyright{BaseModel: domain.BaseModel{ID: copyrightID}}
|
|
return r.db.WithContext(ctx).Model(source).Association("Copyrights").Delete(copyright)
|
|
}
|