apps-l7cal6/endpoints/whatsapp.get.py
2025-03-25 13:33:29 +00:00

31 lines
984 B
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from core.database import get_db
from models.country import Country
import random
router = APIRouter()
@router.get("/whatsapp")
async def get_random_african_country(db: Session = Depends(get_db)):
"""Get a random African country from database"""
# Query all African countries from database
african_countries = db.query(Country).filter(Country.continent == "Africa").all()
if not african_countries:
raise HTTPException(status_code=404, detail="No African countries found in database")
# Select random country
random_country = random.choice(african_countries)
return {
"method": "GET",
"_verb": "get",
"country": {
"name": random_country.name,
"capital": random_country.capital,
"population": random_country.population,
"area": random_country.area
}
}