from fastapi import FastAPI, Depends from typing import List from sqlalchemy.orm import Session from back.schemas import PeopleList, AverageAgeByCountry, CountByCountry, GenderRepartition from back.db import get_db, create_people_from_list, get_average_age_by_country, \ count_people_by_country, get_gender_repartition_by_country app = FastAPI() @app.post("/people") async def create_people(people_list: PeopleList, db: Session = Depends(get_db)) -> dict: count = create_people_from_list(db, people_list.people) return { "message": "People list has been succesfully imported", "records_created": count } @app.get("/average_age", response_model=List[AverageAgeByCountry]) async def read_average_age(db: Session = Depends(get_db)) -> List[AverageAgeByCountry]: return [AverageAgeByCountry(**a) for a in get_average_age_by_country(db)] @app.get("/count_by_country", response_model=List[CountByCountry]) async def read_count_by_country(db: Session = Depends(get_db)) -> List[CountByCountry]: return [CountByCountry(**a) for a in count_people_by_country(db)] @app.get("/gender_repartition/{country}", response_model=GenderRepartition) async def read_count_by_gender(country: str, db: Session = Depends(get_db)) -> GenderRepartition: return GenderRepartition(**get_gender_repartition_by_country(db, country))