testing-code-gen-nh3hsm/helpers/country_helpers.py

69 lines
2.3 KiB
Python

from typing import List, Optional
from sqlalchemy.orm import Session
from sqlalchemy import or_
from models.country import Country
def get_countries_by_names(db: Session, country_names: List[str]) -> List[Country]:
"""
Retrieves a list of countries by their names.
Args:
db (Session): The database session.
country_names (List[str]): A list of country names.
Returns:
List[Country]: A list of country objects matching the provided names.
"""
return db.query(Country).filter(Country.name.in_(country_names)).all()
def get_countries_by_codes(db: Session, country_codes: List[str]) -> List[Country]:
"""
Retrieves a list of countries by their codes.
Args:
db (Session): The database session.
country_codes (List[str]): A list of country codes.
Returns:
List[Country]: A list of country objects matching the provided codes.
"""
return db.query(Country).filter(Country.code.in_(country_codes)).all()
def get_countries_by_names_or_codes(db: Session, names_or_codes: List[str]) -> List[Country]:
"""
Retrieves a list of countries by their names or codes.
Args:
db (Session): The database session.
names_or_codes (List[str]): A list of country names or codes.
Returns:
List[Country]: A list of country objects matching the provided names or codes.
"""
return db.query(Country).filter(or_(Country.name.in_(names_or_codes), Country.code.in_(names_or_codes))).all()
def get_country_by_name(db: Session, country_name: str) -> Optional[Country]:
"""
Retrieves a country by its name.
Args:
db (Session): The database session.
country_name (str): The name of the country.
Returns:
Optional[Country]: The country object if found, otherwise None.
"""
return db.query(Country).filter(Country.name == country_name).first()
def get_country_by_code(db: Session, country_code: str) -> Optional[Country]:
"""
Retrieves a country by its code.
Args:
db (Session): The database session.
country_code (str): The code of the country.
Returns:
Optional[Country]: The country object if found, otherwise None.
"""
return db.query(Country).filter(Country.code == country_code).first()