Update code in endpoints/whatsapp.get.py
This commit is contained in:
parent
f165641c48
commit
5af34de2c9
@ -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
|
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 = APIRouter()
|
||||||
|
|
||||||
@router.get("/whatsapp")
|
@router.get("/whatsapp")
|
||||||
async def get_random_country():
|
async def get_random_african_country(db: Session = Depends(get_db)):
|
||||||
"""Returns random African country"""
|
"""Get a random African country from database"""
|
||||||
country = random.choice(african_countries)
|
|
||||||
|
# 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 {
|
return {
|
||||||
"method": "GET",
|
"method": "GET",
|
||||||
"_verb": "get",
|
"_verb": "get",
|
||||||
"country": country,
|
"country": {
|
||||||
"message": f"Random African country: {country}",
|
"name": random_country.name,
|
||||||
"features": {
|
"capital": random_country.capital,
|
||||||
"total_countries": len(african_countries),
|
"population": random_country.population,
|
||||||
"region": "Africa"
|
"area": random_country.area
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user