wip
This commit is contained in:
125
app/routers/country.py
Normal file
125
app/routers/country.py
Normal file
@@ -0,0 +1,125 @@
|
||||
import sys
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, Query, Depends
|
||||
from sqlmodel import SQLModel, Field
|
||||
from sqlmodel import select
|
||||
|
||||
import alchemy
|
||||
import utils
|
||||
from dependencies import get_session
|
||||
from utils import update_item, create_item
|
||||
from .status_model import Status
|
||||
from .user import ACTIVE_USER
|
||||
|
||||
PRIMARY_ANNOTATION = utils.make_primary_annotation('Country')
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Models
|
||||
|
||||
class CountryBase(SQLModel):
|
||||
code: str = Field(max_length=2, unique=True)
|
||||
name: str = Field(max_length=80)
|
||||
notes: str | None
|
||||
|
||||
|
||||
class Country(CountryBase):
|
||||
id: int | None = Field(default=None, primary_key=True)
|
||||
|
||||
|
||||
class CountryCreate(CountryBase):
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
class CountryPublic(CountryBase):
|
||||
id: int
|
||||
status: Status
|
||||
|
||||
|
||||
class CountryUpdate(CountryBase):
|
||||
code: str | None = None
|
||||
name: str | None = None
|
||||
notes: str | None = None
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Routes
|
||||
|
||||
router = APIRouter(prefix="/country", tags=["country"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[CountryPublic])
|
||||
async def get_countries(
|
||||
offset: int = 0,
|
||||
limit: Annotated[int, Query] = 100,
|
||||
session=Depends(get_session)):
|
||||
"""Get list of all countries"""
|
||||
if limit < 1:
|
||||
limit = sys.maxsize
|
||||
return session.exec(select(alchemy.Country).offset(offset).limit(limit)).all()
|
||||
|
||||
|
||||
@router.get("/{country_id}",
|
||||
response_model=CountryPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def get_country(
|
||||
country_id: PRIMARY_ANNOTATION,
|
||||
session=Depends(get_session)):
|
||||
return utils.get_single_record(session, alchemy.Country, country_id)
|
||||
|
||||
|
||||
@router.post("/",
|
||||
response_model=CountryPublic)
|
||||
async def create_country(
|
||||
country: CountryCreate,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return create_item(
|
||||
session=session,
|
||||
cls=alchemy.Country,
|
||||
current_user=current_user,
|
||||
data=Country.model_validate(country))
|
||||
|
||||
|
||||
@router.patch("/{country_id}",
|
||||
response_model=CountryPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def update_country(
|
||||
country_id: PRIMARY_ANNOTATION,
|
||||
country: CountryUpdate,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return update_item(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Country, country_id),
|
||||
data=country)
|
||||
|
||||
|
||||
@router.put("/{country_id}/activate",
|
||||
response_model=CountryPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def activate_country(
|
||||
country_id: PRIMARY_ANNOTATION,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return utils.set_item_status(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Country, country_id),
|
||||
status='A')
|
||||
|
||||
|
||||
@router.put("/{country_id}/deactivate",
|
||||
response_model=CountryPublic,
|
||||
responses={404: {"description": "Not found"}})
|
||||
async def deactivate_country(
|
||||
country_id: PRIMARY_ANNOTATION,
|
||||
current_user: ACTIVE_USER,
|
||||
session=Depends(get_session)):
|
||||
return utils.set_item_status(
|
||||
session=session,
|
||||
current_user=current_user,
|
||||
item=utils.get_single_record(session, alchemy.Country, country_id),
|
||||
status='I')
|
||||
Reference in New Issue
Block a user