diff --git a/foods.db b/foods.db index 218fe6a..9a624da 100644 Binary files a/foods.db and b/foods.db differ diff --git a/src/main.rs b/src/main.rs index fe30185..33b973e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::sync::{Arc, Mutex}; -use axum::{extract::{Path, State}, response::{Html, Redirect}, routing::{get, post}, Router}; +use axum::{extract::{Path, State}, response::Html, routing::{get, post}, Router}; use rusqlite::Connection; use askama::Template; @@ -11,6 +11,12 @@ struct IndexTemplate { sum: i32, } +#[derive(Template)] +#[template(path = "food.html")] +struct FoodTemplate { + food: Food, +} + #[derive(Debug, Clone, PartialEq)] struct Food { id: i32, @@ -64,18 +70,42 @@ async fn root( async fn increase( State(conn): State>>, Path(id): Path -) -> Redirect{ +) -> Html{ let conn = conn.lock().unwrap(); let mut stmt = conn.prepare("UPDATE food SET actual_servings = (SELECT actual_servings FROM food WHERE id = ?1) + 1 WHERE id = ?1").unwrap(); stmt.execute((id,)).unwrap(); - Redirect::to("/") + let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food WHERE id = ?1").unwrap(); + let food = stmt.query_one((id,), |row| Ok(Food { + id: row.get(0).unwrap(), + portion: row.get(1).unwrap(), + name: row.get(2).unwrap(), + kc_per_serving: row.get(3).unwrap(), + target_servings: row.get(4).unwrap(), + actual_servings: row.get(5).unwrap(), + })).unwrap(); + let food = FoodTemplate {food}; + Html( + food.render().unwrap() + ) } async fn decrease( State(conn): State>>, Path(id): Path -) -> Redirect{ +) -> Html{ let conn = conn.lock().unwrap(); let mut stmt = conn.prepare("UPDATE food SET actual_servings = MAX((SELECT actual_servings FROM food WHERE id = ?1) - 1, 0) WHERE id = ?1").unwrap(); stmt.execute((id,)).unwrap(); - Redirect::to("/") + let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food WHERE id = ?1").unwrap(); + let food = stmt.query_one((id,), |row| Ok(Food { + id: row.get(0).unwrap(), + portion: row.get(1).unwrap(), + name: row.get(2).unwrap(), + kc_per_serving: row.get(3).unwrap(), + target_servings: row.get(4).unwrap(), + actual_servings: row.get(5).unwrap(), + })).unwrap(); + let food = FoodTemplate {food}; + Html( + food.render().unwrap() + ) } diff --git a/templates/food.html b/templates/food.html index 348ac28..2f4ed33 100644 --- a/templates/food.html +++ b/templates/food.html @@ -1,4 +1,4 @@ -
+

{{ food.name }} @@ -15,9 +15,14 @@

- - + +
\ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 435e279..3c4a953 100644 --- a/templates/index.html +++ b/templates/index.html @@ -2,6 +2,7 @@ +