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>
) -> Redirect{
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();
Redirect::to("/")
}

View file

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