2023-11-03 14:55:24 +00:00
|
|
|
use axum::{
|
|
|
|
extract::{Path, State},
|
|
|
|
response::{
|
|
|
|
sse::{Event, KeepAlive},
|
|
|
|
IntoResponse, Sse,
|
|
|
|
},
|
|
|
|
routing::{get, post},
|
|
|
|
Form,
|
|
|
|
};
|
|
|
|
use cm_lib::{
|
|
|
|
models::{NewDrink, Shift},
|
|
|
|
schema::shifts,
|
|
|
|
};
|
2023-09-26 09:18:12 +00:00
|
|
|
use diesel::{ExpressionMethods, OptionalExtension, QueryDsl, SelectableHelper};
|
|
|
|
use diesel_async::{scoped_futures::ScopedFutureExt, AsyncConnection, RunQueryDsl};
|
2023-11-03 14:55:24 +00:00
|
|
|
use futures_util::Stream;
|
2023-09-30 21:51:11 +00:00
|
|
|
use serde::Deserialize;
|
2023-11-03 14:55:24 +00:00
|
|
|
use tokio_stream::{wrappers::errors::BroadcastStreamRecvError, StreamExt as _};
|
2023-09-26 09:18:12 +00:00
|
|
|
|
|
|
|
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))
|
2023-09-26 09:18:12 +00:00
|
|
|
.route("/shifts/open", post(open_shift))
|
2023-09-27 00:00:01 +00:00
|
|
|
.route("/shifts/:id/close", post(close_shift))
|
2023-11-03 14:55:24 +00:00
|
|
|
.route("/ada/updates", get(ada_subscribe))
|
2023-09-26 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2023-09-26 09:18:12 +00:00
|
|
|
.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,
|
|
|
|
}
|
|
|
|
|
2023-11-02 22:26:47 +00:00
|
|
|
impl From<DrinkForm> for NewDrink {
|
|
|
|
fn from(value: DrinkForm) -> Self {
|
2023-09-30 21:51:11 +00:00
|
|
|
NewDrink {
|
2023-11-02 22:26:47 +00:00
|
|
|
price: value.price,
|
|
|
|
quantity: value.quantity,
|
|
|
|
shift: value.shift_id,
|
2023-09-30 21:51:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2023-11-03 14:55:24 +00:00
|
|
|
|
|
|
|
async fn ada_subscribe(
|
|
|
|
State(state): State<AppState>,
|
|
|
|
) -> Sse<impl Stream<Item = Result<Event, BroadcastStreamRecvError>>> {
|
|
|
|
let stream = tokio_stream::wrappers::BroadcastStream::new(state.ada_sender.subscribe())
|
|
|
|
.map(|r| r.map(|s| Event::default().event("ada").data(s)));
|
|
|
|
|
|
|
|
Sse::new(stream).keep_alive(KeepAlive::default())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_ada_list() -> String {
|
|
|
|
let mut buf = Vec::new();
|
|
|
|
|
|
|
|
crate::templates::components::ada_list_html(&mut buf).unwrap();
|
|
|
|
|
|
|
|
String::from_utf8(buf).unwrap()
|
|
|
|
}
|