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:
parent
b77cde24e1
commit
576c846ac9
3 changed files with 29 additions and 7 deletions
|
@ -7,6 +7,9 @@
|
|||
<p>Tea is great.</p>
|
||||
<input type="text" id="myInput">
|
||||
<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>
|
||||
</html>
|
||||
|
||||
|
@ -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));
|
||||
|
|
|
@ -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
|
||||
);
|
17
src/main.rs
17
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 <type>
|
||||
// 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<TeaNew>,
|
||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||
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,
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue