diff --git a/src/main.rs b/src/main.rs index 98a4676..7ef20b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,11 +26,13 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut .route("/configuration", get(retrieve_config)) .route("/configuration", post(update_config)) // brewing events - //.route("/brewing-events", get(todo!())) + .route("/brewing-events", get(retrieve_brewing_events)) // types of tea .route("/types-of-tea", get(retrieve_types_of_tea)) .route("/types-of-tea/:id", post(update_tea_by_id)) .route("/types-of-tea/:id", get(retrieve_tea_by_id)) + // update steeping time and trigger log entry + .route("/tea-steeping-time/:id", post(update_tea_steeping_time_by_id)) // teavail-device .route("/add-tea", post(add_tea)) .route("/update-tea", post(update_tea_by_rfid)) // probably already covered by "update_tea_by_id" diff --git a/src/requests/get_requests.rs b/src/requests/get_requests.rs index d7e025b..10f3e8a 100644 --- a/src/requests/get_requests.rs +++ b/src/requests/get_requests.rs @@ -45,6 +45,19 @@ pub async fn retrieve_tea_by_id( } } +// TODO: implement this correctly +pub async fn retrieve_brewing_events( + State(state): State, +) -> Result { + match sqlx::query_as::<_, Tea>("SELECT * FROM steepinglog GROUPBY tea_id") + .fetch_optional(&state.pool) + .await + { + Ok(tea) => Ok((StatusCode::OK, Json(tea))), + Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), + } +} + // used by teavail-device pub async fn retrieve_tea_by_rfid( Path(rfid): Path, @@ -67,7 +80,10 @@ pub async fn delete_tea_by_id( State(state): State, ) -> Result { match sqlx::query_as::<_, Tea>( - "DELETE FROM tea WHERE id = $1 RETURNING id, teaname, rfidcode, watertemp, steepingseconds", + " + DELETE FROM tea WHERE id = $1 + RETURNING id, teaname, rfidcode, watertemp, steepingseconds + ", ) .bind(id) .fetch_one(&state.pool) diff --git a/src/requests/post_requests.rs b/src/requests/post_requests.rs index ebf160e..580af77 100644 --- a/src/requests/post_requests.rs +++ b/src/requests/post_requests.rs @@ -1,52 +1,115 @@ -use crate::data::{MyState, Tea, TeaNew, Config}; -use axum::{extract::{State, Path}, http::StatusCode, response::IntoResponse, Json}; +use crate::data::{Config, MyState, Tea, TeaNew}; +use axum::{ + extract::{Path, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; pub async fn update_config( State(state): State, Json(config): Json, ) -> Result { - match sqlx::query_as::<_, Tea>("UPDATE config SET default_steeping_time = $1, feedback_timeout = $2 RETURNING default_steeping_time, feedback_timeout") - .bind(&config.default_steeping_time) - .bind(&config.feedback_timeout) - .fetch_one(&state.pool) - .await + match sqlx::query_as::<_, Tea>( + " + UPDATE config + SET default_steeping_time = $1, feedback_timeout = $2 + RETURNING default_steeping_time, feedback_timeout + ", + ) + .bind(config.default_steeping_time) + .bind(config.feedback_timeout) + .fetch_one(&state.pool) + .await { Ok(config) => Ok((StatusCode::OK, Json(config))), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), } } -// used by web-interface +// TODO: should be used by web-interface to update metadata pub async fn update_tea_by_id( State(state): State, Path(id): Path, Json(data): Json, ) -> Result { - match sqlx::query_as::<_, Tea>("UPDATE tea SET tea_name = $1, water_temp = $2, steeping_seconds = $3 WHERE id = $4 RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds") - .bind(&data.tea_name) - .bind(&data.water_temp) - .bind(&data.steeping_seconds) - .bind(id) - .fetch_one(&state.pool) - .await + match sqlx::query_as::<_, Tea>( + " + UPDATE tea + SET tea_name = $1, water_temp = $2, steeping_seconds = $3 + WHERE id = $4 + RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds + ", + ) + .bind(&data.tea_name) + .bind(data.water_temp) + .bind(data.steeping_seconds) + .bind(id) + .fetch_one(&state.pool) + .await { Ok(tea) => Ok((StatusCode::OK, Json(tea))), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), } } +// used to update steeping time (triggers log entry) +pub async fn update_tea_steeping_time_by_id( + State(state): State, + Path(id): Path, + Json(tea_new): Json, +) -> Result { + match sqlx::query_as::<_, Tea>( + " + UPDATE tea SET steeping_seconds = $1 + WHERE id = $2 + RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds + ", + ) + .bind(tea_new.steeping_seconds) + .bind(id) + .fetch_one(&state.pool) + .await + { + Ok(tea) => { + // add steeping time change to steeping log + match sqlx::query( + " + INSERT INTO steepinglog (tea_id, steeping_seconds) + VALUES ($1, $2) + ", + ) + .bind(id) + .bind(tea_new.steeping_seconds) + .execute(&state.pool) + .await + { + Ok(_) => Ok((StatusCode::OK, Json(tea))), + Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), + } + } + Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), + } +} + // used by teavail-device pub async fn update_tea_by_rfid( State(state): State, Json(data): Json, ) -> Result { - match sqlx::query_as::<_, Tea>("UPDATE tea SET tea_name = $1, water_temp = $2, steeping_seconds = $3 WHERE rfid_code = $4 RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds") - .bind(&data.tea_name) - .bind(&data.water_temp) - .bind(&data.steeping_seconds) - .bind(&data.rfid_code) - .fetch_one(&state.pool) - .await + match sqlx::query_as::<_, Tea>( + " + UPDATE tea SET tea_name = $1, water_temp = $2, steeping_seconds = $3 + WHERE rfid_code = $4 + RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds + ", + ) + .bind(&data.tea_name) + .bind(data.water_temp) + .bind(data.steeping_seconds) + .bind(&data.rfid_code) + .fetch_one(&state.pool) + .await { Ok(tea) => Ok((StatusCode::OK, Json(tea))), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), @@ -58,16 +121,19 @@ pub async fn add_tea( Json(tea_new): Json, ) -> Result { println!("add"); - match sqlx::query_as::<_, Tea> - ( - "INSERT INTO tea (tea_name, rfid_code, water_temp, steeping_seconds) VALUES ($1, $2, $3, $4) RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds" + match sqlx::query_as::<_, Tea>( + " + INSERT INTO tea (tea_name, rfid_code, water_temp, steeping_seconds) + VALUES ($1, $2, $3, $4) + RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds + ", ) - .bind(&tea_new.tea_name) - .bind(&tea_new.rfid_code) - .bind(&tea_new.water_temp) - .bind(&tea_new.steeping_seconds) - .fetch_one(&state.pool) - .await + .bind(&tea_new.tea_name) + .bind(&tea_new.rfid_code) + .bind(tea_new.water_temp) + .bind(tea_new.steeping_seconds) + .fetch_one(&state.pool) + .await { Ok(tea) => Ok((StatusCode::CREATED, Json(tea))), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),