From 5af34de2c92d0ec2ce999f9160bec2725f21f5a8 Mon Sep 17 00:00:00 2001 From: Backend IM Bot Date: Tue, 25 Mar 2025 13:33:29 +0000 Subject: [PATCH] Update code in endpoints/whatsapp.get.py --- endpoints/whatsapp.get.py | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/endpoints/whatsapp.get.py b/endpoints/whatsapp.get.py index 65ae9d5..c704b85 100644 --- a/endpoints/whatsapp.get.py +++ b/endpoints/whatsapp.get.py @@ -1,27 +1,31 @@ -from fastapi import APIRouter +from fastapi import APIRouter, Depends, HTTPException +from sqlalchemy.orm import Session +from core.database import get_db +from models.country import Country import random -african_countries = [ - "Nigeria", "Egypt", "South Africa", "Kenya", "Ethiopia", - "Ghana", "Tanzania", "Uganda", "Algeria", "Morocco", - "Zimbabwe", "Zambia", "Rwanda", "Senegal", "Mali", - "Angola", "Mozambique", "Cameroon", "Niger", "Malawi" -] - router = APIRouter() @router.get("/whatsapp") -async def get_random_country(): - """Returns random African country""" - country = random.choice(african_countries) +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": country, - "message": f"Random African country: {country}", - "features": { - "total_countries": len(african_countries), - "region": "Africa" + "country": { + "name": random_country.name, + "capital": random_country.capital, + "population": random_country.population, + "area": random_country.area } } \ No newline at end of file