This commit is contained in:
Philipp Hofer 2025-04-06 23:12:50 +02:00
parent 865760d81a
commit af2e6d72a5
4 changed files with 86 additions and 46 deletions

View File

@ -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;

View File

@ -1,5 +1,5 @@
use dotenv::dotenv;
use sqlx::{pool::PoolOptions, SqlitePool};
use sqlx::{SqlitePool, pool::PoolOptions};
use std::env;
#[tokio::main]

View File

@ -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 {

View File

@ -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,16 +60,20 @@ 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 {
@ -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 {