File size: 1,473 Bytes
ef6fa9a
a90a6d5
 
 
ef6fa9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from fastapi import APIRouter, HTTPException
from service.population_pandas import get_continents, get_continent_data
from models.population_models import ContinentResponse, ContinentsListResponse, StatResponse
from logger import logger  # Import the logger

router = APIRouter()

@router.get("/continents/", response_model=ContinentsListResponse)
def get_all_continents():
    """Fetch the list of all continents."""
    logger.info("Fetching all continents")
    return {"continents": get_continents()}

@router.get("/continents/{continent}/", response_model=ContinentResponse | StatResponse)
def get_continent_stats(continent: str, stat: str = None):
    """Fetch statistics for a specific continent."""
    logger.info(f"Fetching statistics for continent: {continent}, stat: {stat}")

    continent_data = get_continent_data(continent)

    if not continent_data:
        logger.error(f"Continent not found: {continent}")
        raise HTTPException(status_code=404, detail="Continent not found")

    if stat:
        if stat in continent_data:
            logger.info(f"Returning stat {stat} for {continent}")
            return {"stat": stat, "value": continent_data[stat]}
        else:
            logger.warning(f"Invalid stat requested: {stat}")
            raise HTTPException(status_code=400, detail="Invalid statistic requested")

    logger.info(f"Successfully fetched data for {continent}")
    return {"continent": continent, "statistics": continent_data}