clubmanager/src/api.rs

113 lines
3 KiB
Rust
Raw Normal View History

2023-09-27 00:00:01 +00:00
use axum::extract::Path;
2023-09-30 21:51:11 +00:00
use axum::Form;
use axum::{extract::State, response::IntoResponse, routing::post};
2023-09-30 21:51:11 +00:00
use cm_lib::models::{NewDrink, Shift};
2023-09-27 00:00:01 +00:00
use cm_lib::schema::shifts;
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper};
use diesel_async::{scoped_futures::ScopedFutureExt, AsyncConnection, RunQueryDsl};
2023-09-30 21:51:11 +00:00
use serde::Deserialize;
use crate::axum_ructe::render;
use crate::AppState;
pub(crate) fn router() -> axum::Router<AppState> {
axum::Router::new()
2023-09-30 21:51:11 +00:00
.route("/drinks", post(add_drink))
.route("/shifts/open", post(open_shift))
2023-09-27 00:00:01 +00:00
.route("/shifts/:id/close", post(close_shift))
}
async fn open_shift(State(state): State<AppState>) -> impl IntoResponse {
let shift = {
let mut conn = state.connection.get().await.unwrap();
conn.transaction(|conn| {
use cm_lib::schema::shifts::dsl::*;
async move {
diesel::insert_into(shifts)
.default_values()
.execute(conn)
.await?;
shifts
.order(id.desc())
.select(Shift::as_select())
.first(conn)
.await
}
.scope_boxed()
})
.await
.optional()
.unwrap()
};
render!(crate::templates::home_html, shift)
}
2023-09-27 00:00:01 +00:00
async fn close_shift(State(state): State<AppState>, Path(id): Path<u32>) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap();
diesel::update(shifts::table.filter(shifts::id.eq(id)))
.set(shifts::end.eq(Some(chrono::Utc::now().naive_local())))
.execute(&mut conn)
.await
.unwrap();
render!(crate::templates::home_html, None)
}
2023-09-30 21:51:11 +00:00
#[derive(Deserialize, Debug)]
struct DrinkForm {
shift_id: u32,
price: u32,
quantity: u32,
}
impl Into<NewDrink> for DrinkForm {
fn into(self) -> NewDrink {
NewDrink {
price: self.price,
quantity: self.quantity,
shift: self.shift_id,
}
}
}
async fn add_drink(
State(state): State<AppState>,
Form(form): Form<DrinkForm>,
) -> impl IntoResponse {
let mut conn = state.connection.get().await.unwrap();
2023-10-19 10:16:16 +00:00
let open_shift: Option<Shift> = {
use cm_lib::schema::shifts::dsl::*;
shifts
.filter(end.is_null())
.select(Shift::as_select())
.first(&mut conn)
.await
.optional()
.expect("Query failed: No open shifts found")
};
let open_shift = open_shift.unwrap();
2023-09-30 21:51:11 +00:00
async {
use cm_lib::schema::drinks::dsl::*;
diesel::insert_into(drinks)
.values::<NewDrink>(form.into())
.execute(&mut conn)
.await
.unwrap()
}
.await;
2023-10-19 10:16:16 +00:00
let mut headers = axum::http::HeaderMap::new();
headers.insert("HX-Push-Url", "/".parse().unwrap());
(
headers,
render!(crate::templates::home_html, Some(open_shift)),
)
2023-09-30 21:51:11 +00:00
}