From 576c846ac94c6d2d7479ce512fd5582180029dcc Mon Sep 17 00:00:00 2001 From: zzzz Date: Fri, 3 May 2024 01:30:37 +0200 Subject: [PATCH] added more fields to tea --- assets/index.html | 14 ++++++++++++-- migrations/0001_init.sql | 5 ++++- src/main.rs | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/assets/index.html b/assets/index.html index e7496ec..bafca11 100644 --- a/assets/index.html +++ b/assets/index.html @@ -7,6 +7,9 @@

Tea is great.

+ + + @@ -16,7 +19,14 @@ document.getElementById('myButton').addEventListener('click', function() { myHeaders.append('Content-Type', 'application/json'); var inputValue = document.getElementById('myInput').value; - var raw = JSON.stringify({ "teaname": inputValue }); + var raw = JSON.stringify( + { + "teaname": inputValue, + "rfidcode": "b7 6D fo 42", + "watertemp": 90, + "steepingseconds": 120, + } + ); var requestOptions = { method: 'POST', @@ -25,7 +35,7 @@ document.getElementById('myButton').addEventListener('click', function() { redirect: 'follow' }; - fetch('https://ias-tea-axum.shuttleapp.rs/tea', requestOptions) + fetch('https://ias-tea-axum.shuttleapp.rs/addtea', requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error)); diff --git a/migrations/0001_init.sql b/migrations/0001_init.sql index db734fd..c9c627a 100644 --- a/migrations/0001_init.sql +++ b/migrations/0001_init.sql @@ -1,4 +1,7 @@ CREATE TABLE IF NOT EXISTS tea ( id serial PRIMARY KEY, - teaname TEXT NOT NULL + teaname VARCHAR(128) NOT NULL, + rfidcode VARCHAR(128) NOT NULL, + watertemp INT, + steepingseconds INT ); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fd6d200..6b2e1c5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -// Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/todos -Method POST -Body '{"note":"Earl Grey Premium"}' -ContentType 'application/json' -// cargo shuttle resource delete +// 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}, @@ -45,8 +45,11 @@ async fn add( Json(data): Json, ) -> Result { println!("add"); - match sqlx::query_as::<_, Tea>("INSERT INTO tea (teaname) VALUES ($1) RETURNING id, teaname") + match sqlx::query_as::<_, Tea>("INSERT INTO tea (teaname, rfidcode, watertemp, steepingseconds) VALUES ($1, $2, $3, $4) RETURNING id, teaname, rfidcode, watertemp, steepingseconds") .bind(&data.teaname) + .bind(&data.rfidcode) + .bind(&data.watertemp) + .bind(&data.steepingseconds) .fetch_one(&state.pool) .await { @@ -70,7 +73,7 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut let state = MyState { pool }; let router = Router::new() .nest_service("/", ServeFile::new("assets/index.html")) - .route("/tea", post(add)) + .route("/addtea", post(add)) .route("/tea/:id", get(retrieve)) .route("/alltea", get(retrieve_all)) .with_state(state); @@ -81,10 +84,16 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut #[derive(Deserialize)] struct TeaNew { pub teaname: String, + pub rfidcode: String, + pub watertemp: i32, + pub steepingseconds: i32, } #[derive(Serialize, FromRow)] struct Tea { pub id: i32, pub teaname: String, + pub rfidcode: String, + pub watertemp: i32, + pub steepingseconds: i32, }