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

added retrieve_by_rfid

This commit is contained in:
zzzz 2024-05-05 20:52:25 +02:00
parent 576c846ac9
commit 075ab0c3be
2 changed files with 18 additions and 1 deletions

View file

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

View file

@ -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<String>,
State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
@ -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);