tercul-backend/repositories/country_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

50 lines
1.4 KiB
Go

package repositories
import (
"context"
"errors"
"gorm.io/gorm"
"tercul/models"
)
// CountryRepository defines CRUD methods specific to Country.
type CountryRepository interface {
BaseRepository[models.Country]
GetByCode(ctx context.Context, code string) (*models.Country, error)
ListByContinent(ctx context.Context, continent string) ([]models.Country, error)
}
type countryRepository struct {
BaseRepository[models.Country]
db *gorm.DB
}
// NewCountryRepository creates a new CountryRepository.
func NewCountryRepository(db *gorm.DB) CountryRepository {
return &countryRepository{
BaseRepository: NewBaseRepositoryImpl[models.Country](db),
db: db,
}
}
// GetByCode finds a country by code
func (r *countryRepository) GetByCode(ctx context.Context, code string) (*models.Country, error) {
var country models.Country
if err := r.db.WithContext(ctx).Where("code = ?", code).First(&country).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrEntityNotFound
}
return nil, err
}
return &country, nil
}
// ListByContinent finds countries by continent
func (r *countryRepository) ListByContinent(ctx context.Context, continent string) ([]models.Country, error) {
var countries []models.Country
if err := r.db.WithContext(ctx).Where("continent = ?", continent).Find(&countries).Error; err != nil {
return nil, err
}
return countries, nil
}