craete macro
This commit is contained in:
parent
2bac41d641
commit
865760d81a
25
src/lib.rs
25
src/lib.rs
@ -7,6 +7,31 @@ use tower_sessions::{MemoryStore, SessionManagerLayer};
|
||||
mod partials;
|
||||
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 MY_CSS: &str = include_str!("../assets/style.css");
|
||||
const LEAFLET_CSS: &str = include_str!("../assets/leaflet.css");
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{partials::page, station::Station};
|
||||
use crate::{err, partials::page, station::Station, succ};
|
||||
use axum::{
|
||||
extract::State,
|
||||
response::{IntoResponse, Redirect},
|
||||
@ -30,14 +30,7 @@ Ok(_) =>
|
||||
)
|
||||
.await
|
||||
.unwrap(),
|
||||
Err(e) =>
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!("Station '{}' konnte _NICHT_ erstellt werden, da es bereits eine Station mit diesem Namen gibt ({e})!", 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)
|
||||
|
||||
}
|
||||
|
||||
@ -50,28 +43,14 @@ async fn delete(
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
err!(session, "Station mit ID {id} konnte nicht gelöscht werden, da sie nicht existiert");
|
||||
|
||||
return Redirect::to("/station");
|
||||
};
|
||||
|
||||
station.delete(&db).await;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Station '{}' erfolgreich gelöscht!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
succ!(session, "Station '{}' erfolgreich gelöscht!", station.name);
|
||||
|
||||
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>
|
||||
) -> Result<Markup, impl IntoResponse> {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
err!(session,"Station mit ID {id} konnte nicht geöffnet werden, da sie nicht existiert");
|
||||
|
||||
return Err(Redirect::to("/station"));
|
||||
};
|
||||
@ -258,28 +229,14 @@ async fn update_notes(
|
||||
Form(form): Form<UpdateNotesForm>
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
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;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Notizen für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
succ!(session,"Notizen für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
|
||||
|
||||
Redirect::to(&format!("/station/{id}"))
|
||||
}
|
||||
@ -295,28 +252,14 @@ async fn update_amount_people(
|
||||
Form(form): Form<UpdateAmountPeopleForm>
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
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;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
|
||||
|
||||
Redirect::to(&format!("/station/{id}"))
|
||||
}
|
||||
@ -327,28 +270,14 @@ async fn update_amount_people_reset(
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
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;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
succ!(session, "Anzahl an Betreuer für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
|
||||
|
||||
Redirect::to(&format!("/station/{id}"))
|
||||
}
|
||||
@ -365,28 +294,14 @@ async fn update_location(
|
||||
Form(form): Form<UpdateLocationForm>
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
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;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Standort für die Station '{}' wurden erfolgreich bearbeitet!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
succ!(session, "Standort für die Station '{}' wurden erfolgreich bearbeitet!", station.name);
|
||||
|
||||
Redirect::to(&format!("/station/{id}"))
|
||||
}
|
||||
@ -397,28 +312,15 @@ async fn update_location_clear (
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
) -> impl IntoResponse {
|
||||
let Some(station) = Station::find_by_id(&db, id).await else {
|
||||
session
|
||||
.insert(
|
||||
"err",
|
||||
&format!(
|
||||
"Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert"
|
||||
),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
err!(session, "Station mit ID {id} konnte nicht bearbeitet werden, da sie nicht existiert");
|
||||
|
||||
return Redirect::to("/station");
|
||||
};
|
||||
|
||||
station.update_location_clear(&db).await;
|
||||
|
||||
session
|
||||
.insert(
|
||||
"succ",
|
||||
&format!("Standort für die Station '{}' wurden erfolgreich gelöscht!", station.name),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
succ!(session, "Standort für die Station '{}' wurden erfolgreich gelöscht!", station.name);
|
||||
|
||||
Redirect::to(&format!("/station/{id}"))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user