mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 05:11:34 +00:00
- Core Go application with GraphQL API using gqlgen - Comprehensive data models for literary works, authors, translations - Repository pattern with caching layer - Authentication and authorization system - Linguistics analysis capabilities with multiple adapters - Vector search integration with Weaviate - Docker containerization support - Python data migration and analysis scripts - Clean architecture with proper separation of concerns - Production-ready configuration and middleware - Proper .gitignore excluding vendor/, database files, and build artifacts
164 lines
4.4 KiB
Go
164 lines
4.4 KiB
Go
package repositories
|
|
|
|
import (
|
|
"context"
|
|
"gorm.io/gorm"
|
|
"tercul/models"
|
|
)
|
|
|
|
// WorkRepository defines methods specific to Work.
|
|
type WorkRepository interface {
|
|
BaseRepository[models.Work]
|
|
FindByTitle(ctx context.Context, title string) ([]models.Work, error)
|
|
FindByAuthor(ctx context.Context, authorID uint) ([]models.Work, error)
|
|
FindByCategory(ctx context.Context, categoryID uint) ([]models.Work, error)
|
|
FindByLanguage(ctx context.Context, language string, page, pageSize int) (*PaginatedResult[models.Work], error)
|
|
GetWithTranslations(ctx context.Context, id uint) (*models.Work, error)
|
|
ListWithTranslations(ctx context.Context, page, pageSize int) (*PaginatedResult[models.Work], error)
|
|
}
|
|
|
|
type workRepository struct {
|
|
BaseRepository[models.Work]
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewWorkRepository creates a new WorkRepository.
|
|
func NewWorkRepository(db *gorm.DB) WorkRepository {
|
|
return &workRepository{
|
|
BaseRepository: NewBaseRepositoryImpl[models.Work](db),
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// FindByTitle finds works by title (partial match)
|
|
func (r *workRepository) FindByTitle(ctx context.Context, title string) ([]models.Work, error) {
|
|
var works []models.Work
|
|
if err := r.db.WithContext(ctx).Where("title LIKE ?", "%"+title+"%").Find(&works).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return works, nil
|
|
}
|
|
|
|
// FindByAuthor finds works by author ID
|
|
func (r *workRepository) FindByAuthor(ctx context.Context, authorID uint) ([]models.Work, error) {
|
|
var works []models.Work
|
|
if err := r.db.WithContext(ctx).Joins("JOIN work_authors ON work_authors.work_id = works.id").
|
|
Where("work_authors.author_id = ?", authorID).
|
|
Find(&works).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return works, nil
|
|
}
|
|
|
|
// FindByCategory finds works by category ID
|
|
func (r *workRepository) FindByCategory(ctx context.Context, categoryID uint) ([]models.Work, error) {
|
|
var works []models.Work
|
|
if err := r.db.WithContext(ctx).Joins("JOIN work_categories ON work_categories.work_id = works.id").
|
|
Where("work_categories.category_id = ?", categoryID).
|
|
Find(&works).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return works, nil
|
|
}
|
|
|
|
// FindByLanguage finds works by language with pagination
|
|
func (r *workRepository) FindByLanguage(ctx context.Context, language string, page, pageSize int) (*PaginatedResult[models.Work], error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
|
|
var works []models.Work
|
|
var totalCount int64
|
|
|
|
// Get total count
|
|
if err := r.db.WithContext(ctx).Model(&models.Work{}).Where("language = ?", language).Count(&totalCount).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Calculate offset
|
|
offset := (page - 1) * pageSize
|
|
|
|
// Get paginated data
|
|
if err := r.db.WithContext(ctx).Where("language = ?", language).
|
|
Offset(offset).Limit(pageSize).
|
|
Find(&works).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Calculate total pages
|
|
totalPages := int(totalCount) / pageSize
|
|
if int(totalCount)%pageSize > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
hasNext := page < totalPages
|
|
hasPrev := page > 1
|
|
|
|
return &PaginatedResult[models.Work]{
|
|
Items: works,
|
|
TotalCount: totalCount,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
TotalPages: totalPages,
|
|
HasNext: hasNext,
|
|
HasPrev: hasPrev,
|
|
}, nil
|
|
}
|
|
|
|
// GetWithTranslations gets a work with its translations
|
|
func (r *workRepository) GetWithTranslations(ctx context.Context, id uint) (*models.Work, error) {
|
|
return r.FindWithPreload(ctx, []string{"Translations"}, id)
|
|
}
|
|
|
|
// ListWithTranslations lists works with their translations
|
|
func (r *workRepository) ListWithTranslations(ctx context.Context, page, pageSize int) (*PaginatedResult[models.Work], error) {
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
|
|
var works []models.Work
|
|
var totalCount int64
|
|
|
|
// Get total count
|
|
if err := r.db.WithContext(ctx).Model(&models.Work{}).Count(&totalCount).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Calculate offset
|
|
offset := (page - 1) * pageSize
|
|
|
|
// Get paginated data with preloaded translations
|
|
if err := r.db.WithContext(ctx).Preload("Translations").
|
|
Offset(offset).Limit(pageSize).
|
|
Find(&works).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Calculate total pages
|
|
totalPages := int(totalCount) / pageSize
|
|
if int(totalCount)%pageSize > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
hasNext := page < totalPages
|
|
hasPrev := page > 1
|
|
|
|
return &PaginatedResult[models.Work]{
|
|
Items: works,
|
|
TotalCount: totalCount,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
TotalPages: totalPages,
|
|
HasNext: hasNext,
|
|
HasPrev: hasPrev,
|
|
}, nil
|
|
}
|