From f5d758ed2d89c7bc2302561d3573fc398567263b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 24 Oct 2025 22:57:21 +0200 Subject: [PATCH] checkbox behaviour --- foods.db | Bin 12288 -> 12288 bytes src/main.rs | 31 +++++++++++++++++++++++++++++++ src/set.sql | 8 ++++++++ templates/food.html | 8 ++++---- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/set.sql diff --git a/foods.db b/foods.db index 035d690e2105e55baf5b7418a2811462e0fc1d9b..a769939169bc94dd8d7dd8b4ed5755d245622c3f 100644 GIT binary patch delta 140 zcmZojXh@hK&1f`H#+lJ*W5OzV7XG8W-kbRpw0W(0vl)b>Z5bIkr499s6r4*7+~t@# z7)0e6IT@u56T91I{ejFU@r)p*#TI#VnX836UgA3y*A delta 114 zcmZojXh@hK%_uif#+gxWW5OzV7T#?BRh#(~w0SxCbQp}JZ5bIkC)?;L2`HPTSfp5( znlN!NNV_s|a!fv_uQqv>?qtT3llAlr7+E*R>v=P>$TD* PreparedStatements { conn.prepare_cached(include_str!("get_food.sql"))?; conn.prepare_cached(include_str!("get_foods.sql"))?; conn.prepare_cached(include_str!("get_sum.sql"))?; + conn.prepare_cached(include_str!("set.sql"))?; Ok(PreparedStatements {}) } @@ -119,6 +120,11 @@ impl<'conn> PreparedStatements { conn.prepare_cached(include_str!("get_sum.sql")) .expect("cached statement is invalid") } + + fn set(conn: &'conn Connection) -> CachedStatement<'conn> { + conn.prepare_cached(include_str!("set.sql")) + .expect("cached statement is invalid") + } } type ConnState = Arc>; @@ -153,6 +159,7 @@ async fn main() -> Result<(), std::io::Error> { .route("/", get(root)) .route("/increase/{id}", post(increase)) .route("/decrease/{id}", post(decrease)) + .route("/set/{id}/to/{amount}", post(set)) .layer( TraceLayer::new_for_http().make_span_with(|request: &Request<_>| { let matched_path = request @@ -285,3 +292,27 @@ async fn decrease( .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?, )) } + +fn do_set(conn: &Arc>, id: i32, amount: i32) -> rusqlite::Result<()> { + let conn = conn.lock(); + let mut stmt = PreparedStatements::set(&conn); + let new: i32 = stmt.query_one((id, amount), |row| row.get(0))?; + debug!(id, new_serving_count = new, "set"); + Ok(()) +} + +async fn set( + State(conn): State>>, + Path((id, amount)): Path<(i32, i32)>, +) -> Result, StatusCode> { + do_set(&conn, id, amount).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let date = get_date(); + let update = FoodUpdateTemplate { food, sum, date }; + Ok(Html( + update + .render() + .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?, + )) +} diff --git a/src/set.sql b/src/set.sql new file mode 100644 index 0000000..6830998 --- /dev/null +++ b/src/set.sql @@ -0,0 +1,8 @@ +UPDATE + food +SET + actual_servings = MAX(?2, 0) +WHERE + id = ?1 +RETURNING + actual_servings diff --git a/templates/food.html b/templates/food.html index 8472399..3a0fe85 100644 --- a/templates/food.html +++ b/templates/food.html @@ -20,17 +20,17 @@ {% if loop.index as i32 <= food.target_servings %} {% else %} {% endif %}