bake id into post request uri

main
Zynh0722 2023-09-26 17:00:01 -07:00
parent 96dd76e463
commit ddbd2c330a
3 changed files with 13 additions and 25 deletions

View File

@ -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<AppState> {
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<AppState>) -> impl IntoResponse {
@ -40,29 +42,13 @@ async fn open_shift(State(state): State<AppState>) -> impl IntoResponse {
render!(crate::templates::home_html, shift)
}
async fn close_shift(State(state): State<AppState>) -> 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<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)
}

View File

@ -1,5 +1,7 @@
@()
@use cm_lib::models::Shift;
<button hx-post="/api/shifts/close" hx-swap="outerHTML" type="button">
@(open_shift: Shift)
<button hx-post="/api/shifts/@open_shift.id/close" hx-swap="outerHTML" type="button">
Close Shift
</button>

View File

@ -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())
}
</div>
</main>