From 0edebf0738b8eed1469ce3a68e2c680841b0acaa Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Wed, 26 Mar 2025 13:05:33 +0000 Subject: [PATCH] Add get endpoint for states --- endpoints/states.get.py | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/endpoints/states.get.py b/endpoints/states.get.py index 47f0118..7794ac3 100644 --- a/endpoints/states.get.py +++ b/endpoints/states.get.py @@ -1,21 +1,18 @@ -# Entity: Country +# Entity: State -from fastapi import APIRouter, HTTPException, status +from fastapi import APIRouter, Depends, HTTPException, status +from sqlalchemy.orm import Session from typing import List -from core.models.country import Country -from core.schemas.country import CountrySchema -import httpx +from core.database import get_db +from core.models.state import State +from core.schemas.state import StateSchema router = APIRouter() -@router.get("/states", response_model=List[CountrySchema], status_code=status.HTTP_200_OK) -async def get_countries(): - """Get list of countries from third party API""" - async with httpx.AsyncClient() as client: - response = await client.get("https://restcountries.com/v3.1/all") - if response.status_code != 200: - raise HTTPException( - status_code=status.HTTP_503_SERVICE_UNAVAILABLE, - detail="Unable to fetch countries from external API" - ) - return response.json() \ No newline at end of file +@router.get("/states", response_model=List[StateSchema], status_code=200) +async def get_states( + db: Session = Depends(get_db) +): + """Get all states""" + states = db.query(State).all() + return states \ No newline at end of file