40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from typing import List
|
|
from app.core.database.db_session import get_db
|
|
from app.repositories.geo_repository import geo_repository
|
|
from app.schemas.Geo import CountrySchema, StateSchema, CitySchema
|
|
from app.models.UserModel import User
|
|
from app.core.permissions.RoleChecker import get_current_user
|
|
|
|
router = APIRouter(prefix="/api/v1/geo", tags=["Geographic Master Data"])
|
|
|
|
@router.get("/countries", response_model=List[CountrySchema])
|
|
def get_countries(
|
|
active_only: bool = True,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
return geo_repository.get_countries(db, active_only)
|
|
|
|
@router.get("/countries/{country_id}/states", response_model=List[StateSchema])
|
|
def get_states(
|
|
country_id: int,
|
|
active_only: bool = True,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
country = geo_repository.get_country_by_id(db, country_id)
|
|
if not country or country.deleted_at is not None:
|
|
raise HTTPException(status_code=404, detail="Country not found")
|
|
return geo_repository.get_states_by_country(db, country_id, active_only)
|
|
|
|
@router.get("/states/{state_id}/cities", response_model=List[CitySchema])
|
|
def get_cities(
|
|
state_id: int,
|
|
active_only: bool = True,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
state = geo_repository.get_state_by_id(db, state_id)
|
|
if not state or state.deleted_at is not None:
|
|
raise HTTPException(status_code=404, detail="State not found")
|
|
return geo_repository.get_cities_by_state(db, state_id, active_only)
|
|
|