from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from geoalchemy2.types import Geometry
from sqlmodel import Session, cast, select

from .database import engine
from .models import Country

app = FastAPI()
templates = Jinja2Templates(directory="geo_info_svg/templates")


@app.get("/", response_class=HTMLResponse)
async def test(request: Request):
    with Session(engine) as session:
        countries = session.exec(
            select(  # type: ignore
                Country.name,
                Country.population,
                cast(Country.geometry, Geometry).ST_XMin(),
                cast(Country.geometry, Geometry).ST_YMin(),
                cast(Country.geometry, Geometry).ST_XMax(),
                cast(Country.geometry, Geometry).ST_YMax(),
                Country.geometry.ST_AsSVG(),
            )
            .order_by(Country.population.desc())  # type: ignore
            .limit(10)
        ).all()

    return templates.TemplateResponse(
        "countries.html", {"request": request, "countries": countries}
    )

sqlmodel.cast sqlmodel.Session fastapi.templating.Jinja2Templates fastapi.FastAPI
[((274, 283), 'fastapi.FastAPI', 'FastAPI', ([], {}), '()\n', (281, 283), False, 'from fastapi import FastAPI, Request\n'), ((296, 347), 'fastapi.templating.Jinja2Templates', 'Jinja2Templates', ([], {'directory': '"""geo_info_svg/templates"""'}), "(directory='geo_info_svg/templates')\n", (311, 347), False, 'from fastapi.templating import Jinja2Templates\n'), ((436, 451), 'sqlmodel.Session', 'Session', (['engine'], {}), '(engine)\n', (443, 451), False, 'from sqlmodel import Session, cast, select\n'), ((616, 648), 'sqlmodel.cast', 'cast', (['Country.geometry', 'Geometry'], {}), '(Country.geometry, Geometry)\n', (620, 648), False, 'from sqlmodel import Session, cast, select\n'), ((676, 708), 'sqlmodel.cast', 'cast', (['Country.geometry', 'Geometry'], {}), '(Country.geometry, Geometry)\n', (680, 708), False, 'from sqlmodel import Session, cast, select\n'), ((736, 768), 'sqlmodel.cast', 'cast', (['Country.geometry', 'Geometry'], {}), '(Country.geometry, Geometry)\n', (740, 768), False, 'from sqlmodel import Session, cast, select\n'), ((796, 828), 'sqlmodel.cast', 'cast', (['Country.geometry', 'Geometry'], {}), '(Country.geometry, Geometry)\n', (800, 828), False, 'from sqlmodel import Session, cast, select\n')]