craete macro

This commit is contained in:
Philipp Hofer 2025-04-06 23:11:18 +02:00
parent 2bac41d641
commit 865760d81a
2 changed files with 41 additions and 114 deletions

View File

@ -7,6 +7,31 @@ use tower_sessions::{MemoryStore, SessionManagerLayer};
mod partials; mod partials;
mod station; mod station;
#[macro_export]
macro_rules! err {
($session:expr, $fmt:expr $(, $arg:expr)*) => {
$session
.insert(
"err",
&format!($fmt $(, $arg)*)
)
.await
.unwrap()
};
}
#[macro_export]
macro_rules! succ {
($session:expr, $fmt:expr $(, $arg:expr)*) => {
$session
.insert(
"succ",
&format!($fmt $(, $arg)*)
)
.await
.unwrap()
};
}
const PICO_CSS: &str = include_str!("../assets/pico.classless.min.css"); const PICO_CSS: &str = include_str!("../assets/pico.classless.min.css");
const MY_CSS: &str = include_str!("../assets/style.css"); const MY_CSS: &str = include_str!("../assets/style.css");
const LEAFLET_CSS: &str = include_str!("../assets/leaflet.css"); const LEAFLET_CSS: &str = include_str!("../assets/leaflet.css");

View File

@ -1,4 +1,4 @@
use crate::{partials::page, station::Station}; use crate::{err, partials::page, station::Station, succ};
use axum::{ use axum::{
extract::State, extract::State,
response::{IntoResponse, Redirect}, response::{IntoResponse, Redirect},
@ -30,14 +30,7 @@ Ok(_) =>
) )
.await .await
.unwrap(), .unwrap(),
Err(e) => Err(e) => err!(session, "Station '{}' konnte _NICHT_ erstellt werden, da es bereits eine Station mit diesem Namen gibt ({e})!", form.name)
session
.insert(
"err",
&format!("Station '{}' konnte _NICHT_ erstellt werden, da es bereits eine Station mit diesem Namen gibt ({e})!", form.name),
)
.await
.unwrap(),
} }
@ -50,28 +43,14 @@ async fn delete(
axum::extract::Path(id): axum::extract::Path<i64>, axum::extract::Path(id): axum::extract::Path<i64>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.delete(&db).await; station.delete(&db).await;
session succ!(session, "Station '{}' erfolgreich gelöscht!", station.name);
.insert(
"succ",
&format!("Station '{}' erfolgreich gelöscht!", station.name),
)
.await
.unwrap();
Redirect::to("/station") Redirect::to("/station")
} }
@ -79,15 +58,7 @@ async fn delete(
async fn view(State(db): State<Arc<SqlitePool>>, session: Session, axum::extract::Path(id): axum::extract::Path<i64> async fn view(State(db): State<Arc<SqlitePool>>, session: Session, axum::extract::Path(id): axum::extract::Path<i64>
) -> Result<Markup, impl IntoResponse> { ) -> Result<Markup, impl IntoResponse> {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session,"Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Err(Redirect::to("/station")); return Err(Redirect::to("/station"));
}; };
@ -258,28 +229,14 @@ async fn update_notes(
Form(form): Form<UpdateNotesForm> Form(form): Form<UpdateNotesForm>
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.update_notes(&db, &form.notes).await; station.update_notes(&db, &form.notes).await;
session succ!(session,"Notizen für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
.insert(
"succ",
&format!("Notizen für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
)
.await
.unwrap();
Redirect::to(&format!("/station/{id}")) Redirect::to(&format!("/station/{id}"))
} }
@ -295,28 +252,14 @@ async fn update_amount_people(
Form(form): Form<UpdateAmountPeopleForm> Form(form): Form<UpdateAmountPeopleForm>
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.update_amount_people(&db, form.amount_people).await; station.update_amount_people(&db, form.amount_people).await;
session succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
.insert(
"succ",
&format!("Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
)
.await
.unwrap();
Redirect::to(&format!("/station/{id}")) Redirect::to(&format!("/station/{id}"))
} }
@ -327,28 +270,14 @@ async fn update_amount_people_reset(
axum::extract::Path(id): axum::extract::Path<i64>, axum::extract::Path(id): axum::extract::Path<i64>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.update_amount_people_reset(&db).await; station.update_amount_people_reset(&db).await;
session succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
.insert(
"succ",
&format!("Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
)
.await
.unwrap();
Redirect::to(&format!("/station/{id}")) Redirect::to(&format!("/station/{id}"))
} }
@ -365,28 +294,14 @@ async fn update_location(
Form(form): Form<UpdateLocationForm> Form(form): Form<UpdateLocationForm>
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.update_location(&db, form.lat, form.lng).await; station.update_location(&db, form.lat, form.lng).await;
session succ!(session, "Standort für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
.insert(
"succ",
&format!("Standort für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
)
.await
.unwrap();
Redirect::to(&format!("/station/{id}")) Redirect::to(&format!("/station/{id}"))
} }
@ -397,28 +312,15 @@ async fn update_location_clear (
axum::extract::Path(id): axum::extract::Path<i64>, axum::extract::Path(id): axum::extract::Path<i64>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let Some(station) = Station::find_by_id(&db, id).await else { let Some(station) = Station::find_by_id(&db, id).await else {
session err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
.insert(
"err",
&format!(
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
),
)
.await
.unwrap();
return Redirect::to("/station"); return Redirect::to("/station");
}; };
station.update_location_clear(&db).await; station.update_location_clear(&db).await;
session
.insert( succ!(session, "Standort für die Station '{}' wurden erfolgreich gelöscht!", station.name);
"succ",
&format!("Standort für die Station '{}' wurden erfolgreich gelöscht!", station.name),
)
.await
.unwrap();
Redirect::to(&format!("/station/{id}")) Redirect::to(&format!("/station/{id}"))
} }