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

added more fields to tea

This commit is contained in:
zzzz 2024-05-03 01:30:37 +02:00
parent b77cde24e1
commit 576c846ac9
3 changed files with 29 additions and 7 deletions

View file

@ -7,6 +7,9 @@
<p>Tea is great.</p> <p>Tea is great.</p>
<input type="text" id="myInput"> <input type="text" id="myInput">
<button id="myButton">Click me if you want tea!</button> <button id="myButton">Click me if you want tea!</button>
<a href="https://ias-tea-axum.shuttleapp.rs/alltea">
<button id="myAllteaButton">ALL THE TEA!</button>
</a>
</body> </body>
</html> </html>
@ -16,7 +19,14 @@ document.getElementById('myButton').addEventListener('click', function() {
myHeaders.append('Content-Type', 'application/json'); myHeaders.append('Content-Type', 'application/json');
var inputValue = document.getElementById('myInput').value; 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 = { var requestOptions = {
method: 'POST', method: 'POST',
@ -25,7 +35,7 @@ document.getElementById('myButton').addEventListener('click', function() {
redirect: 'follow' 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(response => response.text())
.then(result => console.log(result)) .then(result => console.log(result))
.catch(error => console.log('error', error)); .catch(error => console.log('error', error));

View file

@ -1,4 +1,7 @@
CREATE TABLE IF NOT EXISTS tea ( CREATE TABLE IF NOT EXISTS tea (
id serial PRIMARY KEY, id serial PRIMARY KEY,
teaname TEXT NOT NULL teaname VARCHAR(128) NOT NULL,
rfidcode VARCHAR(128) NOT NULL,
watertemp INT,
steepingseconds INT
); );

View file

@ -1,5 +1,5 @@
// Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/todos -Method POST -Body '{"note":"Earl Grey Premium"}' -ContentType 'application/json' // Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/alltea -Method POST -Body '{"note":"Earl Grey Premium"}' -ContentType 'application/json'
// cargo shuttle resource delete <type> // cargo shuttle resource delete database::shared::postgres
// cargo shuttle resource list // cargo shuttle resource list
use axum::{ use axum::{
extract::{Path, State}, extract::{Path, State},
@ -45,8 +45,11 @@ async fn add(
Json(data): Json<TeaNew>, Json(data): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> { ) -> Result<impl IntoResponse, impl IntoResponse> {
println!("add"); 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.teaname)
.bind(&data.rfidcode)
.bind(&data.watertemp)
.bind(&data.steepingseconds)
.fetch_one(&state.pool) .fetch_one(&state.pool)
.await .await
{ {
@ -70,7 +73,7 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut
let state = MyState { pool }; let state = MyState { pool };
let router = Router::new() let router = Router::new()
.nest_service("/", ServeFile::new("assets/index.html")) .nest_service("/", ServeFile::new("assets/index.html"))
.route("/tea", post(add)) .route("/addtea", post(add))
.route("/tea/:id", get(retrieve)) .route("/tea/:id", get(retrieve))
.route("/alltea", get(retrieve_all)) .route("/alltea", get(retrieve_all))
.with_state(state); .with_state(state);
@ -81,10 +84,16 @@ async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::Shut
#[derive(Deserialize)] #[derive(Deserialize)]
struct TeaNew { struct TeaNew {
pub teaname: String, pub teaname: String,
pub rfidcode: String,
pub watertemp: i32,
pub steepingseconds: i32,
} }
#[derive(Serialize, FromRow)] #[derive(Serialize, FromRow)]
struct Tea { struct Tea {
pub id: i32, pub id: i32,
pub teaname: String, pub teaname: String,
pub rfidcode: String,
pub watertemp: i32,
pub steepingseconds: i32,
} }