tercul-backend/internal/repositories/country_repository.go
Damir Mukimov fa336cacf3
wip
2025-09-01 00:43:59 +02:00

50 lines
1.4 KiB
Go

package repositories
import (
"context"
"errors"
"gorm.io/gorm"
"tercul/internal/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
}