tercul-backend/internal/data/sql/city_repository.go

36 lines
894 B
Go

package repositories
import (
"context"
"gorm.io/gorm"
"tercul/internal/models"
)
// CityRepository defines CRUD methods specific to City.
type CityRepository interface {
BaseRepository[models.City]
ListByCountryID(ctx context.Context, countryID uint) ([]models.City, error)
}
type cityRepository struct {
BaseRepository[models.City]
db *gorm.DB
}
// NewCityRepository creates a new CityRepository.
func NewCityRepository(db *gorm.DB) CityRepository {
return &cityRepository{
BaseRepository: NewBaseRepositoryImpl[models.City](db),
db: db,
}
}
// ListByCountryID finds cities by country ID
func (r *cityRepository) ListByCountryID(ctx context.Context, countryID uint) ([]models.City, error) {
var cities []models.City
if err := r.db.WithContext(ctx).Where("country_id = ?", countryID).Find(&cities).Error; err != nil {
return nil, err
}
return cities, nil
}