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

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("/")
}