Update code in endpoints/whatsapp.get.py

This commit is contained in:
Backend IM Bot 2025-03-25 13:33:29 +00:00
parent f165641c48
commit 5af34de2c9

View File

@ -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
}
}