diff --git a/foods.db b/foods.db index 9a624da..356d231 100644 Binary files a/foods.db and b/foods.db differ diff --git a/src/main.rs b/src/main.rs index 33b973e..198ed63 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,9 +12,10 @@ struct IndexTemplate { } #[derive(Template)] -#[template(path = "food.html")] -struct FoodTemplate { +#[template(path = "food-update.html")] +struct FoodUpdateTemplate { food: Food, + sum: i32, } #[derive(Debug, Clone, PartialEq)] @@ -83,7 +84,9 @@ async fn increase( target_servings: row.get(4).unwrap(), actual_servings: row.get(5).unwrap(), })).unwrap(); - let food = FoodTemplate {food}; + let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").unwrap(); + let sum = stmt.query_one((), |row| row.get(0)).unwrap(); + let food = FoodUpdateTemplate{food, sum}; Html( food.render().unwrap() ) @@ -104,7 +107,9 @@ async fn decrease( target_servings: row.get(4).unwrap(), actual_servings: row.get(5).unwrap(), })).unwrap(); - let food = FoodTemplate {food}; + let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").unwrap(); + let sum = stmt.query_one((), |row| row.get(0)).unwrap(); + let food = FoodUpdateTemplate {food,sum}; Html( food.render().unwrap() ) diff --git a/templates/food-update.html b/templates/food-update.html new file mode 100644 index 0000000..2dd7e3d --- /dev/null +++ b/templates/food-update.html @@ -0,0 +1,2 @@ +{% include "food.html" %} +{% include "sum.html" %} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 3c4a953..c4d5354 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,9 +15,7 @@ {% for food in foods %} {% include "food.html" %} {% endfor %} -
- Total: {{ sum }} kc. -
+ {% include "sum.html" %} diff --git a/templates/sum.html b/templates/sum.html new file mode 100644 index 0000000..7f66211 --- /dev/null +++ b/templates/sum.html @@ -0,0 +1,3 @@ +
+ Total: {{ sum }} kc. +
\ No newline at end of file