format
This commit is contained in:
parent
865760d81a
commit
af2e6d72a5
@ -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;
|
||||
|
@ -1,5 +1,5 @@
|
||||
use dotenv::dotenv;
|
||||
use sqlx::{pool::PoolOptions, SqlitePool};
|
||||
use sqlx::{SqlitePool, pool::PoolOptions};
|
||||
use std::env;
|
||||
|
||||
#[tokio::main]
|
||||
|
@ -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 {
|
||||
|
@ -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<CreateForm>,
|
||||
) -> 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<i64>,
|
||||
) -> 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<Arc<SqlitePool>>, session: Session, axum::extract::Path(id): axum::extract::Path<i64>
|
||||
) -> Result<Markup, impl IntoResponse> {
|
||||
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 {
|
||||
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<Arc<SqlitePool>>, 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<Arc<SqlitePool>>, 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<Arc<SqlitePool>>, 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<Arc<SqlitePool>>, 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<Arc<SqlitePool>>,
|
||||
session: Session,
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
Form(form): Form<UpdateNotesForm>
|
||||
Form(form): Form<UpdateNotesForm>,
|
||||
) -> 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<Arc<SqlitePool>>,
|
||||
session: Session,
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
Form(form): Form<UpdateAmountPeopleForm>
|
||||
Form(form): Form<UpdateAmountPeopleForm>,
|
||||
) -> 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<i64>,
|
||||
) -> 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<Arc<SqlitePool>>,
|
||||
session: Session,
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
Form(form): Form<UpdateLocationForm>
|
||||
Form(form): Form<UpdateLocationForm>,
|
||||
) -> 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<Arc<SqlitePool>>,
|
||||
session: Session,
|
||||
axum::extract::Path(id): axum::extract::Path<i64>,
|
||||
) -> 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<Arc<SqlitePool>>, session: Session) -> Markup {
|
||||
let stations = Station::all(&db).await;
|
||||
|
||||
|
||||
let content = html! {
|
||||
h1 { "Stationen" }
|
||||
ol {
|
||||
|
Loading…
x
Reference in New Issue
Block a user