update sum at bottom of page after each change

This commit is contained in:
Khaïs COLIN 2025-10-19 14:18:40 +02:00
parent 12a040fe87
commit 2bd2b95f05
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
5 changed files with 15 additions and 7 deletions

BIN
foods.db

Binary file not shown.

View file

@ -12,9 +12,10 @@ struct IndexTemplate {
} }
#[derive(Template)] #[derive(Template)]
#[template(path = "food.html")] #[template(path = "food-update.html")]
struct FoodTemplate { struct FoodUpdateTemplate {
food: Food, food: Food,
sum: i32,
} }
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
@ -83,7 +84,9 @@ async fn increase(
target_servings: row.get(4).unwrap(), target_servings: row.get(4).unwrap(),
actual_servings: row.get(5).unwrap(), actual_servings: row.get(5).unwrap(),
})).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( Html(
food.render().unwrap() food.render().unwrap()
) )
@ -104,7 +107,9 @@ async fn decrease(
target_servings: row.get(4).unwrap(), target_servings: row.get(4).unwrap(),
actual_servings: row.get(5).unwrap(), actual_servings: row.get(5).unwrap(),
})).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( Html(
food.render().unwrap() food.render().unwrap()
) )

View file

@ -0,0 +1,2 @@
{% include "food.html" %}
{% include "sum.html" %}

View file

@ -15,9 +15,7 @@
{% for food in foods %} {% for food in foods %}
{% include "food.html" %} {% include "food.html" %}
{% endfor %} {% endfor %}
<div style="background-color: lightblue; margin: 1em;"> {% include "sum.html" %}
<b style="font-size: 24px; padding: 1em;">Total: {{ sum }} kc.</b>
</div>
</div> </div>
</main> </main>
</body> </body>

3
templates/sum.html Normal file
View file

@ -0,0 +1,3 @@
<div style="background-color: lightblue; margin: 1em;" id="sum" hx-swap-oob="true">
<b style="font-size: 24px; padding: 1em;">Total: {{ sum }} kc.</b>
</div>