From af2e6d72a565189044781f57783cf6f64a158151 Mon Sep 17 00:00:00 2001 From: Philipp Hofer Date: Sun, 6 Apr 2025 23:12:50 +0200 Subject: [PATCH] format --- src/lib.rs | 2 +- src/main.rs | 2 +- src/partials.rs | 2 +- src/station/routes.rs | 126 ++++++++++++++++++++++++++++-------------- 4 files changed, 86 insertions(+), 46 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9bbeceb..2aad419 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use axum::{body::Body, response::Response, routing::get, Router}; +use axum::{Router, body::Body, response::Response, routing::get}; use sqlx::SqlitePool; use std::sync::Arc; use tokio::net::TcpListener; diff --git a/src/main.rs b/src/main.rs index 031741c..90e2894 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use dotenv::dotenv; -use sqlx::{pool::PoolOptions, SqlitePool}; +use sqlx::{SqlitePool, pool::PoolOptions}; use std::env; #[tokio::main] diff --git a/src/partials.rs b/src/partials.rs index 53941c1..0337cb4 100644 --- a/src/partials.rs +++ b/src/partials.rs @@ -1,4 +1,4 @@ -use maud::{html, Markup, DOCTYPE}; +use maud::{DOCTYPE, Markup, html}; use tower_sessions::Session; pub(crate) async fn page(content: Markup, session: Session, leaflet: bool) -> Markup { diff --git a/src/station/routes.rs b/src/station/routes.rs index 2897111..0f8423e 100644 --- a/src/station/routes.rs +++ b/src/station/routes.rs @@ -1,11 +1,11 @@ use crate::{err, partials::page, station::Station, succ}; use axum::{ + Form, Router, extract::State, response::{IntoResponse, Redirect}, routing::{get, post}, - Form, Router, }; -use maud::{html, Markup}; +use maud::{Markup, html}; use serde::Deserialize; use sqlx::SqlitePool; use std::sync::Arc; @@ -21,17 +21,19 @@ async fn create( session: Session, Form(form): Form, ) -> impl IntoResponse { - match Station::create(&db, &form.name).await{ -Ok(_) => - session - .insert( - "succ", - &format!("Station '{}' erfolgreich erstellt!", form.name), - ) - .await - .unwrap(), - Err(e) => err!(session, "Station '{}' konnte _NICHT_ erstellt werden, da es bereits eine Station mit diesem Namen gibt ({e})!", form.name) - + match Station::create(&db, &form.name).await { + Ok(_) => session + .insert( + "succ", + &format!("Station '{}' erfolgreich erstellt!", form.name), + ) + .await + .unwrap(), + Err(e) => err!( + session, + "Station '{}' konnte _NICHT_ erstellt werden, da es bereits eine Station mit diesem Namen gibt ({e})!", + form.name + ), } Redirect::to("/station") @@ -43,7 +45,10 @@ async fn delete( axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; @@ -55,27 +60,31 @@ async fn delete( Redirect::to("/station") } -async fn view(State(db): State>, session: Session, axum::extract::Path(id): axum::extract::Path - ) -> Result { +async fn view( + State(db): State>, + session: Session, + axum::extract::Path(id): axum::extract::Path, +) -> Result { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session,"Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert" + ); return Err(Redirect::to("/station")); }; - - // maybe switch to maud-display impl of station let content = html! { - h1 { + h1 { a href="/station" { "↩️" } - "Station " (station.name) + "Station " (station.name) } table { tbody { tr { th scope="row" { "Notizen" }; - td { + td { @match station.notes { Some(notes) => { (notes) @@ -99,7 +108,7 @@ async fn view(State(db): State>, session: Session, axum::extract } tr { th scope="row" { "Stations-Code" }; - td { + td { (station.pw) article class="warning" { (format!("Diesen Code nur Betreuern der Station {} geben! Mit diesem Code erhält man die Berechtigung, Gruppen zu bewerten.", station.name)) @@ -108,7 +117,7 @@ async fn view(State(db): State>, session: Session, axum::extract } tr { th scope="row" { "Anzahl Stationsbetreuer" }; - td { + td { @match station.amount_people { Some(amount) => (amount), None => "?", @@ -131,7 +140,7 @@ async fn view(State(db): State>, session: Session, axum::extract } tr { th scope="row" { "Letzter Login eines Stationsbetreuers" }; - td { + td { @match station.last_login { Some(last_login) => (last_login), None => "noch nicht eingeloggt :-(", @@ -217,7 +226,6 @@ async fn view(State(db): State>, session: Session, axum::extract Ok(page(content, session, true).await) } - #[derive(Deserialize)] struct UpdateNotesForm { notes: String, @@ -226,17 +234,24 @@ async fn update_notes( State(db): State>, session: Session, axum::extract::Path(id): axum::extract::Path, - Form(form): Form + Form(form): Form, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; station.update_notes(&db, &form.notes).await; - succ!(session,"Notizen für die Station '{}' wurden erfolgreich bearbeitet!", station.name); + succ!( + session, + "Notizen für die Station '{}' wurden erfolgreich bearbeitet!", + station.name + ); Redirect::to(&format!("/station/{id}")) } @@ -249,17 +264,24 @@ async fn update_amount_people( State(db): State>, session: Session, axum::extract::Path(id): axum::extract::Path, - Form(form): Form + Form(form): Form, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; station.update_amount_people(&db, form.amount_people).await; - succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name); + succ!( + session, + "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", + station.name + ); Redirect::to(&format!("/station/{id}")) } @@ -270,14 +292,21 @@ async fn update_amount_people_reset( axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; station.update_amount_people_reset(&db).await; - succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name); + succ!( + session, + "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", + station.name + ); Redirect::to(&format!("/station/{id}")) } @@ -291,45 +320,56 @@ async fn update_location( State(db): State>, session: Session, axum::extract::Path(id): axum::extract::Path, - Form(form): Form + Form(form): Form, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; station.update_location(&db, form.lat, form.lng).await; - succ!(session, "Standort für die Station '{}' wurden erfolgreich bearbeitet!", station.name); + succ!( + session, + "Standort für die Station '{}' wurden erfolgreich bearbeitet!", + station.name + ); Redirect::to(&format!("/station/{id}")) } -async fn update_location_clear ( +async fn update_location_clear( State(db): State>, session: Session, axum::extract::Path(id): axum::extract::Path, ) -> impl IntoResponse { let Some(station) = Station::find_by_id(&db, id).await else { - err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"); + err!( + session, + "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert" + ); return Redirect::to("/station"); }; station.update_location_clear(&db).await; - - succ!(session, "Standort für die Station '{}' wurden erfolgreich gelöscht!", station.name); + succ!( + session, + "Standort für die Station '{}' wurden erfolgreich gelöscht!", + station.name + ); Redirect::to(&format!("/station/{id}")) } - async fn index(State(db): State>, session: Session) -> Markup { let stations = Station::all(&db).await; - let content = html! { h1 { "Stationen" } ol {