Update code in endpoints/whatsapp.get.py
This commit is contained in:
parent
5af34de2c9
commit
f5122459c4
@ -1,31 +1,53 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy import Column, String, Integer
|
||||||
from core.database import get_db
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
from models.country import Country
|
from typing import List
|
||||||
import random
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
class Country(Base):
|
||||||
|
__tablename__ = "countries"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
name = Column(String, unique=True, index=True)
|
||||||
|
code = Column(String, unique=True)
|
||||||
|
phone_code = Column(String)
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.get("/whatsapp")
|
@router.get("/whatsapp")
|
||||||
async def get_random_african_country(db: Session = Depends(get_db)):
|
async def get_countries() -> List[dict]:
|
||||||
"""Get a random African country from database"""
|
"""Get list of countries with phone codes"""
|
||||||
|
countries = [
|
||||||
# Query all African countries from database
|
{"name": "United States", "code": "US", "phone_code": "+1"},
|
||||||
african_countries = db.query(Country).filter(Country.continent == "Africa").all()
|
{"name": "United Kingdom", "code": "GB", "phone_code": "+44"},
|
||||||
|
{"name": "India", "code": "IN", "phone_code": "+91"}
|
||||||
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": {
|
"countries": countries,
|
||||||
"name": random_country.name,
|
"total": len(countries)
|
||||||
"capital": random_country.capital,
|
}
|
||||||
"population": random_country.population,
|
|
||||||
"area": random_country.area
|
# Migration file
|
||||||
}
|
"""
|
||||||
}
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
op.create_table(
|
||||||
|
'countries',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=False),
|
||||||
|
sa.Column('code', sa.String(), nullable=False),
|
||||||
|
sa.Column('phone_code', sa.String(), nullable=False),
|
||||||
|
sa.PrimaryKeyConstraint('id'),
|
||||||
|
sa.UniqueConstraint('name'),
|
||||||
|
sa.UniqueConstraint('code')
|
||||||
|
)
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
op.drop_table('countries')
|
||||||
|
"""
|
Loading…
x
Reference in New Issue
Block a user