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

cleanup and WIP steeping log

This commit is contained in:
zzzz 2024-06-02 23:50:34 +02:00
parent d3363c1481
commit 76fe78bd48
2 changed files with 48 additions and 31 deletions

View file

@ -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
);

View file

@ -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<i32>,
State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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<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)
@ -46,7 +69,6 @@ async fn retrieve_by_rfid(
async fn retrieve_all(
State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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<MyState>,
Json(data): Json<TeaNew>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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<i32>,
State(state): State<MyState>,
) -> Result<impl IntoResponse, impl IntoResponse> {
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,