use cards, prevent negative servings

This commit is contained in:
Khaïs COLIN 2025-10-18 22:45:44 +02:00
parent fc64df2b2a
commit dd0ac1e4c7
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 23 additions and 24 deletions

BIN
foods.db

Binary file not shown.

View file

@ -75,7 +75,7 @@ async fn decrease(
Path(id): Path<i32> Path(id): Path<i32>
) -> Redirect{ ) -> Redirect{
let conn = conn.lock().unwrap(); 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(); 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(); stmt.execute((id,)).unwrap();
Redirect::to("/") Redirect::to("/")
} }

View file

@ -10,37 +10,36 @@
<body> <body>
<main> <main>
<table> <div style="display: flex-root; margin: auto; width: fit-content;">
<tr>
<th>Portion</th>
<th>Lebensmittel</th>
<th>kc.</th>
<th>Servings (target)</th>
</tr>
{% for food in foods %} {% for food in foods %}
<tr> <div style="border: 1px solid black; margin: 1em; padding: 1em;">
<td>{{ food.portion }}</td> <div style="text-align: center;">
<td>{{ food.name }}</td> <p>
<td style="text-align: right;">{{ food.kc_per_serving }}</td> <b>{{ food.name }}</b>
<td> </p>
{{ food.portion }} ({{ food.kc_per_serving }} kc.)
<br>
<progress max="{{food.target_servings}}" value="{{food.actual_servings}}" {% if food.actual_servings> <progress max="{{food.target_servings}}" value="{{food.actual_servings}}" {% if food.actual_servings>
food.target_servings %} food.target_servings %}
style="accent-color: red;" style="accent-color: red;"
{% endif %}> {% endif %}>
</progress> </progress>
{{ food.actual_servings }}/{{ food.target_servings }} {{ food.actual_servings }}/{{ food.target_servings }}
<form method="post" style="display: inline;"> </div>
<button formaction="/increase/{{ food.id }}">+</button> <br>
<button formaction="/decrease/{{ food.id }}">-</button> <form method="post">
<div style="text-align: center;">
<button formaction="/decrease/{{ food.id }}" style="width: 40%; height: 3em;" {% if food.actual_servings <=0
%} disabled {% endif %}>-</button>
<button formaction="/increase/{{ food.id }}" style="width: 40%; height: 3em;">+</button>
</div>
</form> </form>
</td> </div>
</tr>
{% endfor %} {% endfor %}
<tr> <div style="background-color: lightblue; margin: 1em;">
<th colspan=3 style="text-align: right;">kc.:</th> <b style="font-size: 24px; padding: 1em;">Total: {{ sum }} kc.</b>
<th style="text-align: left;">{{ sum }}</th> </div>
</tr> </div>
</table>
</main> </main>
</body> </body>