mirror of
https://github.com/zzzzDev4/ias-tea-axum.git
synced 2025-04-21 07:41:21 +02:00
split endpoints for steeping logging
This commit is contained in:
parent
e84af166c1
commit
d63ba6aa3a
2 changed files with 32 additions and 20 deletions
|
@ -33,7 +33,12 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut
|
||||||
.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_meta_by_id))
|
.route("/types-of-tea/:id", post(update_tea_meta_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
|
// trigger log entry of steeping time
|
||||||
|
.route(
|
||||||
|
"/tea-steeping-time-log/:id",
|
||||||
|
post(update_tea_steeping_time_log_by_id),
|
||||||
|
)
|
||||||
|
// update target steeping time for next use (no log entry)
|
||||||
.route(
|
.route(
|
||||||
"/tea-steeping-time/:id",
|
"/tea-steeping-time/:id",
|
||||||
post(update_tea_steeping_time_by_id),
|
post(update_tea_steeping_time_by_id),
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::data::{Config, MyState, Tea, TeaMeta, TeaNew, TeaSteepingTimeChange};
|
use crate::data::{Config, MyState, SteepingLog, Tea, TeaMeta, TeaNew, TeaSteepingTimeChange};
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
|
@ -52,7 +52,6 @@ pub async fn update_tea_meta_by_id(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// used to update steeping time (triggers log entry)
|
|
||||||
pub async fn update_tea_steeping_time_by_id(
|
pub async fn update_tea_steeping_time_by_id(
|
||||||
State(state): State<MyState>,
|
State(state): State<MyState>,
|
||||||
Path(id): Path<i32>,
|
Path(id): Path<i32>,
|
||||||
|
@ -70,23 +69,31 @@ pub async fn update_tea_steeping_time_by_id(
|
||||||
.fetch_one(&state.pool)
|
.fetch_one(&state.pool)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(tea) => {
|
Ok(tea) => Ok((StatusCode::OK, Json(tea))),
|
||||||
// add steeping time change to steeping log
|
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
|
||||||
match sqlx::query(
|
}
|
||||||
"
|
}
|
||||||
INSERT INTO steepinglog (tea_id, steeping_seconds)
|
|
||||||
VALUES ($1, $2)
|
// triggers steeping time log entry
|
||||||
",
|
pub async fn update_tea_steeping_time_log_by_id(
|
||||||
)
|
State(state): State<MyState>,
|
||||||
.bind(id)
|
Path(id): Path<i32>,
|
||||||
.bind(steeping_time_change.new_steeping_seconds)
|
Json(steeping_time_change): Json<TeaSteepingTimeChange>,
|
||||||
.execute(&state.pool)
|
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||||
.await
|
// add steeping time change to steeping log
|
||||||
{
|
match sqlx::query_as::<_, SteepingLog>(
|
||||||
Ok(_) => Ok((StatusCode::OK, Json(tea))),
|
"
|
||||||
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
|
INSERT INTO steepinglog (tea_id, steeping_seconds)
|
||||||
}
|
VALUES ($1, $2)
|
||||||
}
|
RETURNING change_id, tea_id, steeping_seconds, steeping_tested_time
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.bind(id)
|
||||||
|
.bind(steeping_time_change.new_steeping_seconds)
|
||||||
|
.fetch_one(&state.pool)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(log_entry) => Ok((StatusCode::OK, Json(log_entry))),
|
||||||
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
|
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue