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:
parent
576c846ac9
commit
075ab0c3be
2 changed files with 18 additions and 1 deletions
17
src/main.rs
17
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<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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue