diff --git a/ter/routes.py b/ter/routes.py index 1f2b7e2..1ef3623 100644 --- a/ter/routes.py +++ b/ter/routes.py @@ -1,4 +1,6 @@ -from fastapi import APIRouter, Request +from typing import Annotated + +from fastapi import APIRouter, Request, Header from jinja2_fragments.fastapi import Jinja2Blocks @@ -10,6 +12,38 @@ router = APIRouter() templates = Jinja2Blocks(settings.TEMPLATE_DIR) +@router.get("/station") +async def station( + request: Request, + search: str | None = None, + name: str | None = None, + hx_target: Annotated[str | None, Header()] = None, +): + context = {"request": request} + + if name: + context["name"] = name + cursor = await database.execute( + "SELECT stop_lat, stop_lon FROM stops WHERE location_type=1 and stop_name = ?", + (name,), + ) + context["location"] = await cursor.fetchone() + + elif search: + if len(search) > 2: + cursor = await database.execute( + "SELECT stop_name FROM stops WHERE location_type=1 and stop_name LIKE ?", + (f"%{search}%",), + ) + context["search_results"] = await cursor.fetchall() + context["search"] = search + + if hx_target: + hx_target = hx_target.replace("-", "_") + + return templates.TemplateResponse("station.html", context, block_name=hx_target) + + @router.get("/") async def index(request: Request): """Home page.""" diff --git a/ter/static/img/bars.svg b/ter/static/img/bars.svg new file mode 100644 index 0000000..1c3ec2e --- /dev/null +++ b/ter/static/img/bars.svg @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ter/templates/station.html b/ter/templates/station.html new file mode 100644 index 0000000..c983880 --- /dev/null +++ b/ter/templates/station.html @@ -0,0 +1,43 @@ +{% extends "/_base.html" %} + +{% block content %} +
+{% if name is not defined %} + + +{% block search_results %} +{% if search_results %} + +{% elif search | length < 3 %} +

Il faut au moins 3 caractères

+{% else %} +

Pas de résultats

+{% endif %} +{% endblock %} +{% else %} +

Gare de {{ name }}

+

Lat. {{ location[0] }} Long. {{ location[1] }}

+{% endif %} +
+{% endblock %}