diff --git a/migrations/0001_init.sql b/migrations/0001_init.sql index c9c627a..43c78e6 100644 --- a/migrations/0001_init.sql +++ b/migrations/0001_init.sql @@ -4,4 +4,10 @@ CREATE TABLE IF NOT EXISTS tea ( rfidcode VARCHAR(128) NOT NULL, watertemp INT, steepingseconds INT +); + +CREATE TABLE IF NOT EXISTS steepinglog ( + tea_id INT, + steeping_seconds INT, + steeping_tested_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 94c99b5..f51b424 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,47 @@ -// 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 +// Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/addtea -Method POST -Body '{ "teaname": DemoTea, "rfidcode": "lo1rfId42", "watertemp": 200, "steepingseconds": 10}' -ContentType 'application/json' use axum::{ extract::{Path, State}, + routing::{get, post}, http::StatusCode, response::IntoResponse, - routing::{get, post}, Json, Router, }; use serde::{Deserialize, Serialize}; use sqlx::{FromRow, PgPool}; use tower_http::services::ServeFile; -async fn retrieve( + +#[derive(Clone)] +struct MyState { + pool: PgPool, +} + +#[shuttle_runtime::main] +async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::ShuttleAxum { + sqlx::migrate!() + .run(&pool) + .await + .expect("Failed to run migrations"); + + let state = MyState { pool }; + let router = Router::new() + .nest_service("/", ServeFile::new("assets/index.html")) + .route("/addtea", post(add)) + .route("/deletetea", get(delete_by_id)) + .route("/updatetea", post(update)) + .route("/teabyid/:id", get(retrieve_by_id)) + .route("/teabyrfid/:rfid", get(retrieve_by_rfid)) + .route("/alltea", get(retrieve_all)) + .with_state(state); + + Ok(router.into()) +} + +async fn retrieve_by_id( Path(id): Path, State(state): State, ) -> Result { - println!("retrieve"); match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE id = $1") .bind(id) .fetch_one(&state.pool) @@ -32,7 +56,6 @@ 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) @@ -46,7 +69,6 @@ async fn retrieve_by_rfid( async fn retrieve_all( State(state): State, ) -> Result { - println!("retrieve all"); match sqlx::query_as::<_, Tea>("SELECT * FROM tea LIMIT 100") .fetch_all(&state.pool) .await @@ -60,7 +82,6 @@ async fn update( State(state): State, Json(data): Json, ) -> Result { - println!("update"); match sqlx::query_as::<_, Tea>("UPDATE tea SET teaname = $1, watertemp = $2, steepingseconds = $3 WHERE rfidcode = $4 RETURNING id, teaname, rfidcode, watertemp, steepingseconds") .bind(&data.teaname) .bind(&data.watertemp) @@ -92,31 +113,21 @@ async fn add( } } -#[derive(Clone)] -struct MyState { - pool: PgPool, -} - -#[shuttle_runtime::main] -async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::ShuttleAxum { - sqlx::migrate!() - .run(&pool) +async fn delete_by_id( + Path(id): Path, + State(state): State, +) -> Result { + match sqlx::query_as::<_, Tea>("DELETE FROM tea WHERE id = $1 RETURNING id, teaname, rfidcode, watertemp, steepingseconds") + .bind(id) + .fetch_one(&state.pool) .await - .expect("Failed to run migrations"); - - let state = MyState { pool }; - let router = Router::new() - .nest_service("/", ServeFile::new("assets/index.html")) - .route("/addtea", post(add)) - .route("/updatetea", post(update)) - .route("/tea/:id", get(retrieve)) - .route("/teabyrfid/:rfid", get(retrieve_by_rfid)) - .route("/alltea", get(retrieve_all)) - .with_state(state); - - Ok(router.into()) + { + Ok(tea) => Ok((StatusCode::OK, Json(tea))), + Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())), + } } + #[derive(Deserialize)] struct TeaNew { pub teaname: String,