diff --git a/src/api.rs b/src/api.rs index 8201e7f..eeab646 100644 --- a/src/api.rs +++ b/src/api.rs @@ -2,9 +2,10 @@ use std::time::Duration; use axum::{ extract::{Path, State}, + http::StatusCode, response::{ sse::{Event, KeepAlive}, - IntoResponse, Sse, + IntoResponse, Response, Sse, }, routing::{get, post}, Form, @@ -99,7 +100,7 @@ async fn add_drink( State(state): State, HxRequest(hx): HxRequest, Form(form): Form, -) -> impl IntoResponse { +) -> Response { let mut conn = state.connection.get().await.unwrap(); let open_shift: Option = { @@ -113,7 +114,16 @@ async fn add_drink( .optional() .expect("Query failed: No open shifts found") }; - let open_shift = open_shift.unwrap(); + + if open_shift.is_none() { + return ( + StatusCode::CONFLICT, + "Unable to to add a drink when no shift is open", + ) + .into_response(); + } + + let open_shift = unsafe { open_shift.unwrap_unchecked() }; { use cm_lib::schema::drinks::dsl::*; @@ -131,6 +141,7 @@ async fn add_drink( headers, render!(crate::templates::home_html, Some(open_shift), !hx), ) + .into_response() } #[derive(Deserialize, Debug)]