update sum at bottom of page after each change
This commit is contained in:
parent
12a040fe87
commit
2bd2b95f05
5 changed files with 15 additions and 7 deletions
BIN
foods.db
BIN
foods.db
Binary file not shown.
13
src/main.rs
13
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()
|
||||
)
|
||||
|
|
|
|||
2
templates/food-update.html
Normal file
2
templates/food-update.html
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
{% include "food.html" %}
|
||||
{% include "sum.html" %}
|
||||
|
|
@ -15,9 +15,7 @@
|
|||
{% for food in foods %}
|
||||
{% include "food.html" %}
|
||||
{% endfor %}
|
||||
<div style="background-color: lightblue; margin: 1em;">
|
||||
<b style="font-size: 24px; padding: 1em;">Total: {{ sum }} kc.</b>
|
||||
</div>
|
||||
{% include "sum.html" %}
|
||||
</div>
|
||||
</main>
|
||||
</body>
|
||||
|
|
|
|||
3
templates/sum.html
Normal file
3
templates/sum.html
Normal 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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue