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:
parent
948e191234
commit
4ec74e18ed
3 changed files with 117 additions and 33 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
pub async fn retrieve_tea_by_rfid(
|
||||
Path(rfid): Path<String>,
|
||||
|
@ -67,7 +80,10 @@ pub async fn delete_tea_by_id(
|
|||
State(state): State<MyState>,
|
||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
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)
|
||||
|
|
|
@ -1,13 +1,24 @@
|
|||
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<MyState>,
|
||||
Json(config): Json<Config>,
|
||||
) -> 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")
|
||||
.bind(&config.default_steeping_time)
|
||||
.bind(&config.feedback_timeout)
|
||||
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
|
||||
{
|
||||
|
@ -16,16 +27,23 @@ pub async fn update_config(
|
|||
}
|
||||
}
|
||||
|
||||
// used by web-interface
|
||||
// TODO: should be used by web-interface to update metadata
|
||||
pub async fn update_tea_by_id(
|
||||
State(state): State<MyState>,
|
||||
Path(id): Path<i32>,
|
||||
Json(data): Json<TeaNew>,
|
||||
) -> 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>(
|
||||
"
|
||||
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(data.water_temp)
|
||||
.bind(data.steeping_seconds)
|
||||
.bind(id)
|
||||
.fetch_one(&state.pool)
|
||||
.await
|
||||
|
@ -35,15 +53,60 @@ pub async fn update_tea_by_id(
|
|||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
pub async fn update_tea_by_rfid(
|
||||
State(state): State<MyState>,
|
||||
Json(data): Json<TeaNew>,
|
||||
) -> 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>(
|
||||
"
|
||||
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.water_temp)
|
||||
.bind(data.steeping_seconds)
|
||||
.bind(&data.rfid_code)
|
||||
.fetch_one(&state.pool)
|
||||
.await
|
||||
|
@ -58,14 +121,17 @@ pub async fn add_tea(
|
|||
Json(tea_new): Json<TeaNew>,
|
||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
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)
|
||||
.bind(tea_new.water_temp)
|
||||
.bind(tea_new.steeping_seconds)
|
||||
.fetch_one(&state.pool)
|
||||
.await
|
||||
{
|
||||
|
|
Loading…
Add table
Reference in a new issue