bake id into post request uri
parent
96dd76e463
commit
ddbd2c330a
28
src/api.rs
28
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<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 {
|
||||
{
|
||||
async fn close_shift(State(state): State<AppState>, Path(id): Path<u32>) -> 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()
|
||||
})
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue