4
0
Fork 0
mirror of https://github.com/zzzzDev4/ias-tea-axum.git synced 2025-04-21 07:41:21 +02:00

cleanup, first wip implementation of steeping log

This commit is contained in:
zzzz 2024-06-11 15:43:56 +02:00
parent 948e191234
commit 4ec74e18ed
3 changed files with 117 additions and 33 deletions

View file

@ -26,11 +26,13 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut
.route("/configuration", get(retrieve_config)) .route("/configuration", get(retrieve_config))
.route("/configuration", post(update_config)) .route("/configuration", post(update_config))
// brewing events // brewing events
//.route("/brewing-events", get(todo!())) .route("/brewing-events", get(retrieve_brewing_events))
// types of tea // types of tea
.route("/types-of-tea", get(retrieve_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", post(update_tea_by_id))
.route("/types-of-tea/:id", get(retrieve_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 // teavail-device
.route("/add-tea", post(add_tea)) .route("/add-tea", post(add_tea))
.route("/update-tea", post(update_tea_by_rfid)) // probably already covered by "update_tea_by_id" .route("/update-tea", post(update_tea_by_rfid)) // probably already covered by "update_tea_by_id"

View file

@ -45,6 +45,19 @@ pub async fn retrieve_tea_by_id(
} }
} }
// TODO: implement this correctly
pub async fn retrieve_brewing_events(
State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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 // used by teavail-device
pub async fn retrieve_tea_by_rfid( pub async fn retrieve_tea_by_rfid(
Path(rfid): Path<String>, Path(rfid): Path<String>,
@ -67,7 +80,10 @@ pub async fn delete_tea_by_id(
State(state): State<MyState>, State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
match sqlx::query_as::<_, Tea>( 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) .bind(id)
.fetch_one(&state.pool) .fetch_one(&state.pool)

View file

@ -1,52 +1,115 @@
use crate::data::{MyState, Tea, TeaNew, Config}; use crate::data::{Config, MyState, Tea, TeaNew};
use axum::{extract::{State, Path}, http::StatusCode, response::IntoResponse, Json}; use axum::{
extract::{Path, State},
http::StatusCode,
response::IntoResponse,
Json,
};
pub async fn update_config( pub async fn update_config(
State(state): State<MyState>, State(state): State<MyState>,
Json(config): Json<Config>, Json(config): Json<Config>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
match sqlx::query_as::<_, Tea>("UPDATE config SET default_steeping_time = $1, feedback_timeout = $2 RETURNING default_steeping_time, feedback_timeout") match sqlx::query_as::<_, Tea>(
.bind(&config.default_steeping_time) "
.bind(&config.feedback_timeout) UPDATE config
.fetch_one(&state.pool) SET default_steeping_time = $1, feedback_timeout = $2
.await 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))), Ok(config) => Ok((StatusCode::OK, Json(config))),
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), 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( pub async fn update_tea_by_id(
State(state): State<MyState>, State(state): State<MyState>,
Path(id): Path<i32>, Path(id): Path<i32>,
Json(data): Json<TeaNew>, Json(data): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
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") match sqlx::query_as::<_, Tea>(
.bind(&data.tea_name) "
.bind(&data.water_temp) UPDATE tea
.bind(&data.steeping_seconds) SET tea_name = $1, water_temp = $2, steeping_seconds = $3
.bind(id) WHERE id = $4
.fetch_one(&state.pool) RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds
.await ",
)
.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))), Ok(tea) => Ok((StatusCode::OK, Json(tea))),
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), 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<MyState>,
Path(id): Path<i32>,
Json(tea_new): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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 // used by teavail-device
pub async fn update_tea_by_rfid( pub async fn update_tea_by_rfid(
State(state): State<MyState>, State(state): State<MyState>,
Json(data): Json<TeaNew>, Json(data): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
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") match sqlx::query_as::<_, Tea>(
.bind(&data.tea_name) "
.bind(&data.water_temp) UPDATE tea SET tea_name = $1, water_temp = $2, steeping_seconds = $3
.bind(&data.steeping_seconds) WHERE rfid_code = $4
.bind(&data.rfid_code) RETURNING id, tea_name, rfid_code, water_temp, steeping_seconds
.fetch_one(&state.pool) ",
.await )
.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))), Ok(tea) => Ok((StatusCode::OK, Json(tea))),
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
@ -58,16 +121,19 @@ pub async fn add_tea(
Json(tea_new): Json<TeaNew>, Json(tea_new): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
println!("add"); println!("add");
match sqlx::query_as::<_, Tea> 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" 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.tea_name)
.bind(&tea_new.rfid_code) .bind(&tea_new.rfid_code)
.bind(&tea_new.water_temp) .bind(tea_new.water_temp)
.bind(&tea_new.steeping_seconds) .bind(tea_new.steeping_seconds)
.fetch_one(&state.pool) .fetch_one(&state.pool)
.await .await
{ {
Ok(tea) => Ok((StatusCode::CREATED, Json(tea))), Ok(tea) => Ok((StatusCode::CREATED, Json(tea))),
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),