44 lines
1.0 KiB
Rust
44 lines
1.0 KiB
Rust
use axum::{extract::State, routing::get, Router};
|
|
use maud::{html, Markup};
|
|
use serde::{Deserialize, Serialize};
|
|
use sqlx::{FromRow, SqlitePool};
|
|
use std::sync::Arc;
|
|
|
|
#[derive(FromRow, Debug, Serialize, Deserialize)]
|
|
struct Station {
|
|
id: u64,
|
|
name: String,
|
|
amount_people: u8,
|
|
last_login: Option<String>, // TODO use proper timestamp (NaiveDateTime?)
|
|
pw: String,
|
|
lat: Option<f64>,
|
|
lng: Option<f64>,
|
|
}
|
|
|
|
impl Station {
|
|
async fn all(db: &SqlitePool) -> Vec<Self> {
|
|
sqlx::query_as::<_, Self>(
|
|
"SELECT id, name, amount_people, last_login, pw, lat, lng FROM station;",
|
|
)
|
|
.fetch_all(db)
|
|
.await
|
|
.unwrap()
|
|
}
|
|
}
|
|
|
|
async fn get_stations(State(db): State<Arc<SqlitePool>>) -> Markup {
|
|
let all = Station::all(&db).await;
|
|
let mut ret = String::new();
|
|
for a in all {
|
|
ret.push_str(&a.name);
|
|
}
|
|
|
|
html! {
|
|
div { (ret) }
|
|
}
|
|
}
|
|
|
|
pub(super) fn routes() -> Router<Arc<SqlitePool>> {
|
|
Router::new().route("/", get(get_stations))
|
|
}
|