tercul-backend/repositories/collection_repository.go
Damir Mukimov 4957117cb6 Initial commit: Tercul Go project with comprehensive architecture
- 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
2025-08-13 07:42:32 +02:00

58 lines
1.8 KiB
Go

package repositories
import (
"context"
"gorm.io/gorm"
"tercul/models"
)
// CollectionRepository defines CRUD methods specific to Collection.
type CollectionRepository interface {
BaseRepository[models.Collection]
ListByUserID(ctx context.Context, userID uint) ([]models.Collection, error)
ListPublic(ctx context.Context) ([]models.Collection, error)
ListByWorkID(ctx context.Context, workID uint) ([]models.Collection, error)
}
type collectionRepository struct {
BaseRepository[models.Collection]
db *gorm.DB
}
// NewCollectionRepository creates a new CollectionRepository.
func NewCollectionRepository(db *gorm.DB) CollectionRepository {
return &collectionRepository{
BaseRepository: NewBaseRepositoryImpl[models.Collection](db),
db: db,
}
}
// ListByUserID finds collections by user ID
func (r *collectionRepository) ListByUserID(ctx context.Context, userID uint) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Where("user_id = ?", userID).Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}
// ListPublic finds public collections
func (r *collectionRepository) ListPublic(ctx context.Context) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Where("is_public = ?", true).Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}
// ListByWorkID finds collections by work ID
func (r *collectionRepository) ListByWorkID(ctx context.Context, workID uint) ([]models.Collection, error) {
var collections []models.Collection
if err := r.db.WithContext(ctx).Joins("JOIN collection_works ON collection_works.collection_id = collections.id").
Where("collection_works.work_id = ?", workID).
Find(&collections).Error; err != nil {
return nil, err
}
return collections, nil
}