4
0
Fork 0
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:
zzzz 2024-06-14 15:00:35 +02:00
parent e84af166c1
commit d63ba6aa3a
2 changed files with 32 additions and 20 deletions

View file

@ -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),

View file

@ -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))),
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
}
}
// triggers steeping time log entry
pub async fn update_tea_steeping_time_log_by_id(
State(state): State<MyState>,
Path(id): Path<i32>,
Json(steeping_time_change): Json<TeaSteepingTimeChange>,
) -> Result<impl IntoResponse, impl IntoResponse> {
// add steeping time change to steeping log // add steeping time change to steeping log
match sqlx::query( match sqlx::query_as::<_, SteepingLog>(
" "
INSERT INTO steepinglog (tea_id, steeping_seconds) INSERT INTO steepinglog (tea_id, steeping_seconds)
VALUES ($1, $2) VALUES ($1, $2)
RETURNING change_id, tea_id, steeping_seconds, steeping_tested_time
", ",
) )
.bind(id) .bind(id)
.bind(steeping_time_change.new_steeping_seconds) .bind(steeping_time_change.new_steeping_seconds)
.execute(&state.pool) .fetch_one(&state.pool)
.await .await
{ {
Ok(_) => Ok((StatusCode::OK, Json(tea))), 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())), Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
} }
} }