compub/src/compub/crud/geography.py

34 lines
1013 B
Python

from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.sql.expression import select
from compub.models.geography import *
__all__ = [
# 'get_state_by_name_and_country',
'get_states',
'create_state'
]
# async def get_state_by_name_and_country(session: AsyncSession, name: str, country: str) -> db.StateProvince:
# statement = select(db.StateProvince).filter(
# db.StateProvince.name == name,
# db.StateProvince.country == country
# ).limit(1)
# result = await session.execute(statement)
# return result.scalars().first()
async def get_states(session: AsyncSession, skip: int = 0, limit: int = 100) -> list[StateProvince]:
statement = select(StateProvince).offset(skip).limit(limit)
result = await session.execute(statement)
return result.scalars().all()
async def create_state(session: AsyncSession, state: StateProvince) -> StateProvince:
session.add(state)
await session.commit()
await session.refresh(state)
return state