clubmanager/src/api.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

2023-09-27 00:00:01 +00:00
use axum::extract::Path;
use axum::{extract::State, response::IntoResponse, routing::post};
use cm_lib::models::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};
use crate::axum_ructe::render;
use crate::AppState;
pub(crate) fn router() -> axum::Router<AppState> {
axum::Router::new()
.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)
}