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:
parent
d3363c1481
commit
76fe78bd48
2 changed files with 48 additions and 31 deletions
|
@ -4,4 +4,10 @@ CREATE TABLE IF NOT EXISTS tea (
|
||||||
rfidcode VARCHAR(128) NOT NULL,
|
rfidcode VARCHAR(128) NOT NULL,
|
||||||
watertemp INT,
|
watertemp INT,
|
||||||
steepingseconds INT
|
steepingseconds INT
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS steepinglog (
|
||||||
|
tea_id INT,
|
||||||
|
steeping_seconds INT,
|
||||||
|
steeping_tested_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
73
src/main.rs
73
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'
|
// Invoke-WebRequest -Uri https://ias-tea-axum.shuttleapp.rs/addtea -Method POST -Body '{ "teaname": DemoTea, "rfidcode": "lo1rfId42", "watertemp": 200, "steepingseconds": 10}' -ContentType 'application/json'
|
||||||
// cargo shuttle resource delete database::shared::postgres
|
|
||||||
// cargo shuttle resource list
|
|
||||||
|
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Path, State},
|
extract::{Path, State},
|
||||||
|
routing::{get, post},
|
||||||
http::StatusCode,
|
http::StatusCode,
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
routing::{get, post},
|
|
||||||
Json, Router,
|
Json, Router,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{FromRow, PgPool};
|
use sqlx::{FromRow, PgPool};
|
||||||
use tower_http::services::ServeFile;
|
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>,
|
Path(id): Path<i32>,
|
||||||
State(state): State<MyState>,
|
State(state): State<MyState>,
|
||||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||||
println!("retrieve");
|
|
||||||
match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE id = $1")
|
match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE id = $1")
|
||||||
.bind(id)
|
.bind(id)
|
||||||
.fetch_one(&state.pool)
|
.fetch_one(&state.pool)
|
||||||
|
@ -32,7 +56,6 @@ async fn retrieve_by_rfid(
|
||||||
Path(rfid): Path<String>,
|
Path(rfid): Path<String>,
|
||||||
State(state): State<MyState>,
|
State(state): State<MyState>,
|
||||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||||
println!("retrieve by rfid");
|
|
||||||
match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE rfidcode = $1 LIMIT 1")
|
match sqlx::query_as::<_, Tea>("SELECT * FROM tea WHERE rfidcode = $1 LIMIT 1")
|
||||||
.bind(rfid)
|
.bind(rfid)
|
||||||
.fetch_optional(&state.pool)
|
.fetch_optional(&state.pool)
|
||||||
|
@ -46,7 +69,6 @@ async fn retrieve_by_rfid(
|
||||||
async fn retrieve_all(
|
async fn retrieve_all(
|
||||||
State(state): State<MyState>,
|
State(state): State<MyState>,
|
||||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
) -> Result<impl IntoResponse, impl IntoResponse> {
|
||||||
println!("retrieve all");
|
|
||||||
match sqlx::query_as::<_, Tea>("SELECT * FROM tea LIMIT 100")
|
match sqlx::query_as::<_, Tea>("SELECT * FROM tea LIMIT 100")
|
||||||
.fetch_all(&state.pool)
|
.fetch_all(&state.pool)
|
||||||
.await
|
.await
|
||||||
|
@ -60,7 +82,6 @@ async fn update(
|
||||||
State(state): State<MyState>,
|
State(state): State<MyState>,
|
||||||
Json(data): Json<TeaNew>,
|
Json(data): Json<TeaNew>,
|
||||||
) -> Result<impl IntoResponse, impl IntoResponse> {
|
) -> 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")
|
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.teaname)
|
||||||
.bind(&data.watertemp)
|
.bind(&data.watertemp)
|
||||||
|
@ -92,31 +113,21 @@ async fn add(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
async fn delete_by_id(
|
||||||
struct MyState {
|
Path(id): Path<i32>,
|
||||||
pool: PgPool,
|
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")
|
||||||
#[shuttle_runtime::main]
|
.bind(id)
|
||||||
async fn main(#[shuttle_shared_db::Postgres] pool: PgPool) -> shuttle_axum::ShuttleAxum {
|
.fetch_one(&state.pool)
|
||||||
sqlx::migrate!()
|
|
||||||
.run(&pool)
|
|
||||||
.await
|
.await
|
||||||
.expect("Failed to run migrations");
|
{
|
||||||
|
Ok(tea) => Ok((StatusCode::OK, Json(tea))),
|
||||||
let state = MyState { pool };
|
Err(e) => Err((StatusCode::BAD_REQUEST, e.to_string())),
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TeaNew {
|
struct TeaNew {
|
||||||
pub teaname: String,
|
pub teaname: String,
|
||||||
|
|
Loading…
Add table
Reference in a new issue