stationslauf/src/station.rs

98 lines
3.7 KiB
Rust

use crate::{partials, AppState, Station};
use axum::{extract::State, routing::get, Router};
use maud::{html, Markup};
use sqlx::SqlitePool;
use std::sync::Arc;
use tower_sessions::Session;
//async fn view(
// State(db): State<Arc<SqlitePool>>,
// session: Session,
// jar: CookieJar,
// pjar: PrivateCookieJar,
// axum::extract::Path(id): axum::extract::Path<i64>,
//) -> Result<(CookieJar, PrivateCookieJar, Markup), (CookieJar, PrivateCookieJar, impl IntoResponse)>
//{
// // Station selector
// let (mut jar, current_station_cookie) = get_station_cookie(&db, jar).await;
// if current_station_cookie.is_none() {
// jar = jar.add(Cookie::new("station_id", id.to_string()));
// }
// if let Some(current_station_cookie) = current_station_cookie {
// if current_station_cookie.id != id {
// // user has a cookie, which is a different station than she is trying to access
// if let Some(station) = Station::find_by_id(&db, id).await {
// jar = jar.remove(Cookie::from("station_id"));
// // trying to access valid station id
// err!(session, "Du hast versucht eine neue Station zu öffnen obwohl du bereits eine andere Station offen hattest. Welche möchtest du nun verwenden?");
// return Ok((
// jar,
// pjar,
// decide_between_stations(&current_station_cookie, &station, session).await,
// ));
// } else {
// // user trying to access _in_valid station id -> make her aware + redirect to old
// err!(session, "Du hast versucht eine Station öffnen, die es nicht gibt. Nachdem du vorher schonmal eine andere Station (die es gibt) geöffnet hattest, bist du nun zu dieser weitergeleitet worden. Wenn du das nicht willst, logg dich bitte aus.");
// return Err((
// jar,
// pjar,
// Redirect::to(&format!("/s/{}", current_station_cookie.id)),
// ));
// }
// }
// }
// let station = Station::find_by_id(&db, id).await.unwrap();
//
// let mut pjar = pjar;
//
// // PW Checker
// if let Some(pw) = pjar.get("pw") {
// if pw.value() != station.pw {
// pjar = pjar.remove(Cookie::from("station_id"));
// err!(session, "Du hattest einen falschen Code für Station {} gespeichert. Bitte gibt den richtigen ein:", station.name );
// return Err((jar, pjar, Redirect::to(&format!("/s/code",))));
// }
// } else {
// return Err((jar, pjar, Redirect::to(&format!("/s/code",))));
// }
//
// let content = html! {
// nav {
// ul {
// li { strong { (format!("Station {}", station.name)) } }
// }
// ul {
// li { a href="/s/station-logout" { "Logout" } }
// }
// }
// h1 { "test" }
// };
//
// Ok((jar, pjar, partials::page(content, session, false).await))
//}
async fn view(
State(db): State<Arc<SqlitePool>>,
session: Session,
axum::extract::Path((id, code)): axum::extract::Path<(i64, String)>,
) -> Markup {
let Some(station) = Station::find_by_id_and_code(&db, id, &code).await else {
let content = html! {
article class="error" {
"Falscher Quick-Einlogg-Link. Bitte nochmal scannen oder neu eingeben."
}
};
return partials::page(content, session, false).await;
};
let content = html! {
h1 { (format!("Station {}", station.name)) }
};
partials::page(content, session, false).await
}
pub(super) fn routes() -> Router<AppState> {
Router::new().route("/{id}/{code}", get(view))
}