checkbox behaviour

This commit is contained in:
Khaïs COLIN 2025-10-24 22:57:21 +02:00
parent d1585409a2
commit f5d758ed2d
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
4 changed files with 43 additions and 4 deletions

BIN
foods.db

Binary file not shown.

View file

@ -87,6 +87,7 @@ impl<'conn> PreparedStatements {
conn.prepare_cached(include_str!("get_food.sql"))?;
conn.prepare_cached(include_str!("get_foods.sql"))?;
conn.prepare_cached(include_str!("get_sum.sql"))?;
conn.prepare_cached(include_str!("set.sql"))?;
Ok(PreparedStatements {})
}
@ -119,6 +120,11 @@ impl<'conn> PreparedStatements {
conn.prepare_cached(include_str!("get_sum.sql"))
.expect("cached statement is invalid")
}
fn set(conn: &'conn Connection) -> CachedStatement<'conn> {
conn.prepare_cached(include_str!("set.sql"))
.expect("cached statement is invalid")
}
}
type ConnState = Arc<Mutex<Connection>>;
@ -153,6 +159,7 @@ async fn main() -> Result<(), std::io::Error> {
.route("/", get(root))
.route("/increase/{id}", post(increase))
.route("/decrease/{id}", post(decrease))
.route("/set/{id}/to/{amount}", post(set))
.layer(
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
let matched_path = request
@ -285,3 +292,27 @@ async fn decrease(
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
))
}
fn do_set(conn: &Arc<Mutex<Connection>>, id: i32, amount: i32) -> rusqlite::Result<()> {
let conn = conn.lock();
let mut stmt = PreparedStatements::set(&conn);
let new: i32 = stmt.query_one((id, amount), |row| row.get(0))?;
debug!(id, new_serving_count = new, "set");
Ok(())
}
async fn set(
State(conn): State<Arc<Mutex<Connection>>>,
Path((id, amount)): Path<(i32, i32)>,
) -> Result<Html<String>, StatusCode> {
do_set(&conn, id, amount).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let food = get_food(&conn, id).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let date = get_date();
let update = FoodUpdateTemplate { food, sum, date };
Ok(Html(
update
.render()
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
))
}

8
src/set.sql Normal file
View file

@ -0,0 +1,8 @@
UPDATE
food
SET
actual_servings = MAX(?2, 0)
WHERE
id = ?1
RETURNING
actual_servings

View file

@ -20,17 +20,17 @@
{% if loop.index as i32 <= food.target_servings %}
<label class="ok">
{% if loop.index as i32 <= food.actual_servings %}
<input type="checkbox" checked>
<input type="checkbox" checked hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 - 1}}" hx-target="#card-{{ food.id }}">
{% else %}
<input type="checkbox">
<input type="checkbox" hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 }}" hx-target="#card-{{ food.id }}">
{% endif %}
</label>
{% else %}
<label class="bad">
{% if loop.index as i32 <= food.actual_servings %}
<input type="checkbox" checked>
<input type="checkbox" checked hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 - 1}}" hx-target="#card-{{ food.id }}">
{% else %}
<input type="checkbox">
<input type="checkbox" hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 }}" hx-target="#card-{{ food.id }}">
{% endif %}
</label>
{% endif %}