diff --git a/assets/index.html b/assets/index.html index bafca11..bc0b9c0 100644 --- a/assets/index.html +++ b/assets/index.html @@ -22,7 +22,7 @@ document.getElementById('myButton').addEventListener('click', function() { var raw = JSON.stringify( { "teaname": inputValue, - "rfidcode": "b7 6D fo 42", + "rfidcode": "b76Dfo42", "watertemp": 90, "steepingseconds": 120, } diff --git a/src/main.rs b/src/main.rs index 6b2e1c5..789ed4a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ // Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/alltea -Method POST -Body '{"note":"Earl Grey Premium"}' -ContentType 'application/json' // cargo shuttle resource delete database::shared::postgres // cargo shuttle resource list + use axum::{ extract::{Path, State}, http::StatusCode, @@ -27,6 +28,21 @@ async fn retrieve( } } +async fn retrieve_by_rfid( + Path(rfid): Path, + State(state): State, +) -> Result { + println!("retrieve by rfid"); + match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE rfidcode = $1 LIMIT 1") + .bind(rfid) + .fetch_optional(&state.pool) + .await + { + Ok(tea) => Ok((StatusCode::OK, Json(tea))), + Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), + } +} + async fn retrieve_all( State(state): State, ) -> Result { @@ -75,6 +91,7 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut .nest_service("/", ServeFile::new("assets/index.html")) .route("/addtea", post(add)) .route("/tea/:id", get(retrieve)) + .route("/teabyrfid/:rfid", get(retrieve_by_rfid)) .route("/alltea", get(retrieve_all)) .with_state(state);