From ddbd2c330a5f7d1113b027b0ae344e6772852d74 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Tue, 26 Sep 2023 17:00:01 -0700 Subject: [PATCH] bake id into post request uri --- src/api.rs | 30 +++++-------------- .../components/close_shift_button.rs.html | 6 ++-- templates/home.rs.html | 2 +- 3 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/api.rs b/src/api.rs index 21db378..63318fa 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,5 +1,7 @@ +use axum::extract::Path; use axum::{extract::State, response::IntoResponse, routing::post}; use cm_lib::models::Shift; +use cm_lib::schema::shifts; use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper}; use diesel_async::{scoped_futures::ScopedFutureExt, AsyncConnection, RunQueryDsl}; @@ -9,7 +11,7 @@ use crate::AppState; pub(crate) fn router() -> axum::Router { axum::Router::new() .route("/shifts/open", post(open_shift)) - .route("/shifts/close", post(close_shift)) + .route("/shifts/:id/close", post(close_shift)) } async fn open_shift(State(state): State) -> impl IntoResponse { @@ -40,29 +42,13 @@ async fn open_shift(State(state): State) -> impl IntoResponse { render!(crate::templates::home_html, shift) } -async fn close_shift(State(state): State) -> impl IntoResponse { - { - let mut conn = state.connection.get().await.unwrap(); - conn.transaction(|conn| { - use cm_lib::schema::shifts::dsl::*; - - async move { - let open_shift = shifts - .filter(end.is_null()) - .select(Shift::as_select()) - .first(conn) - .await?; - - diesel::update(shifts.filter(id.eq(open_shift.id))) - .set(end.eq(Some(chrono::Utc::now().naive_local()))) - .execute(conn) - .await - } - .scope_boxed() - }) +async fn close_shift(State(state): State, Path(id): Path) -> 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) } diff --git a/templates/components/close_shift_button.rs.html b/templates/components/close_shift_button.rs.html index 8855f74..8293e20 100644 --- a/templates/components/close_shift_button.rs.html +++ b/templates/components/close_shift_button.rs.html @@ -1,5 +1,7 @@ -@() +@use cm_lib::models::Shift; - diff --git a/templates/home.rs.html b/templates/home.rs.html index a171d71..da6e090 100644 --- a/templates/home.rs.html +++ b/templates/home.rs.html @@ -12,7 +12,7 @@ @if open_shift.is_none() { @:open_shift_button_html() } else { - @:close_shift_button_html() + @:close_shift_button_html(open_shift.unwrap()) }