mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 04:01:34 +00:00
This commit addresses a broken build state caused by a mid-stream architectural refactoring. The changes align the existing code with the new Domain-Driven Design (DDD-lite) structure outlined in `refactor.md`. Key changes include: - Defined missing domain interfaces for `Auth`, `Localization`, and `Search`. - Refactored application services to use a `Commands` and `Queries` pattern. - Updated GraphQL resolvers to call application services instead of accessing repositories directly. - Fixed dependency injection in `cmd/api/main.go` by removing the non-existent `ApplicationBuilder` and manually instantiating services. - Corrected numerous test files (`integration`, `unit`, and `repository` tests) to reflect the new architecture, including fixing mock objects and test suite setups. - Added missing database migrations for test schemas to resolve "no such table" errors. This effort successfully gets the application to a compilable state and passes a significant portion of the test suite, laying the groundwork for further development and fixing the remaining test failures.
62 lines
1.8 KiB
Go
62 lines
1.8 KiB
Go
package sql
|
|
|
|
import (
|
|
"context"
|
|
"tercul/internal/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type authorRepository struct {
|
|
domain.BaseRepository[domain.Author]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAuthorRepository creates a new AuthorRepository.
|
|
func NewAuthorRepository(db *gorm.DB) domain.AuthorRepository {
|
|
return &authorRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[domain.Author](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// ListByWorkID finds authors by work ID
|
|
func (r *authorRepository) ListByWorkID(ctx context.Context, workID uint) ([]domain.Author, error) {
|
|
var authors []domain.Author
|
|
if err := r.db.WithContext(ctx).Joins("JOIN work_authors ON work_authors.author_id = authors.id").
|
|
Where("work_authors.work_id = ?", workID).
|
|
Find(&authors).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return authors, nil
|
|
}
|
|
|
|
// GetWithTranslations finds an author by ID and preloads their translations.
|
|
func (r *authorRepository) GetWithTranslations(ctx context.Context, id uint) (*domain.Author, error) {
|
|
var author domain.Author
|
|
if err := r.db.WithContext(ctx).Preload("Translations").First(&author, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &author, nil
|
|
}
|
|
|
|
// ListByBookID finds authors by book ID
|
|
func (r *authorRepository) ListByBookID(ctx context.Context, bookID uint) ([]domain.Author, error) {
|
|
var authors []domain.Author
|
|
if err := r.db.WithContext(ctx).Joins("JOIN book_authors ON book_authors.author_id = authors.id").
|
|
Where("book_authors.book_id = ?", bookID).
|
|
Find(&authors).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return authors, nil
|
|
}
|
|
|
|
// ListByCountryID finds authors by country ID
|
|
func (r *authorRepository) ListByCountryID(ctx context.Context, countryID uint) ([]domain.Author, error) {
|
|
var authors []domain.Author
|
|
if err := r.db.WithContext(ctx).Where("country_id = ?", countryID).Find(&authors).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return authors, nil
|
|
}
|