use crate::data::{Config, MyState, Tea, TeaMeta, TeaNew, TeaSteepingTimeChange}; 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::<_, Config>( " 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())), } } // TODO: should be used by web-interface to update metadata pub async fn update_tea_meta_by_id( State(state): State, Path(id): Path, Json(tea_meta): Json, ) -> Result { match sqlx::query_as::<_, Tea>( " UPDATE tea SET tea_name = $1, water_temp = $2, tea_notes = $3 WHERE id = $4 RETURNING id, tea_name, rfid_code, tea_notes, water_temp, steeping_seconds, registration_timestamp ", ) .bind(&tea_meta.tea_name) .bind(tea_meta.water_temp) .bind(&tea_meta.tea_notes) .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(steeping_time_change): 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(steeping_time_change.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(steeping_time_change.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())), } } // registers new tea and triggers log entry for initial steeping time pub async fn add_tea( State(state): State, Json(tea_new): Json, ) -> Result { 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 { Ok(tea) => { // add steeping time change to steeping log match sqlx::query( " INSERT INTO steepinglog (tea_id, steeping_seconds) VALUES ($1, $2) ", ) .bind(tea.id) .bind(tea.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 - deprecated 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 { Ok(tea) => Ok((StatusCode::OK, Json(tea))), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), } } */