checkbox behaviour
This commit is contained in:
parent
d1585409a2
commit
f5d758ed2d
4 changed files with 43 additions and 4 deletions
BIN
foods.db
BIN
foods.db
Binary file not shown.
31
src/main.rs
31
src/main.rs
|
|
@ -87,6 +87,7 @@ impl<'conn> PreparedStatements {
|
||||||
conn.prepare_cached(include_str!("get_food.sql"))?;
|
conn.prepare_cached(include_str!("get_food.sql"))?;
|
||||||
conn.prepare_cached(include_str!("get_foods.sql"))?;
|
conn.prepare_cached(include_str!("get_foods.sql"))?;
|
||||||
conn.prepare_cached(include_str!("get_sum.sql"))?;
|
conn.prepare_cached(include_str!("get_sum.sql"))?;
|
||||||
|
conn.prepare_cached(include_str!("set.sql"))?;
|
||||||
Ok(PreparedStatements {})
|
Ok(PreparedStatements {})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,6 +120,11 @@ impl<'conn> PreparedStatements {
|
||||||
conn.prepare_cached(include_str!("get_sum.sql"))
|
conn.prepare_cached(include_str!("get_sum.sql"))
|
||||||
.expect("cached statement is invalid")
|
.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>>;
|
type ConnState = Arc<Mutex<Connection>>;
|
||||||
|
|
@ -153,6 +159,7 @@ async fn main() -> Result<(), std::io::Error> {
|
||||||
.route("/", get(root))
|
.route("/", get(root))
|
||||||
.route("/increase/{id}", post(increase))
|
.route("/increase/{id}", post(increase))
|
||||||
.route("/decrease/{id}", post(decrease))
|
.route("/decrease/{id}", post(decrease))
|
||||||
|
.route("/set/{id}/to/{amount}", post(set))
|
||||||
.layer(
|
.layer(
|
||||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||||
let matched_path = request
|
let matched_path = request
|
||||||
|
|
@ -285,3 +292,27 @@ async fn decrease(
|
||||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?,
|
.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
8
src/set.sql
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
UPDATE
|
||||||
|
food
|
||||||
|
SET
|
||||||
|
actual_servings = MAX(?2, 0)
|
||||||
|
WHERE
|
||||||
|
id = ?1
|
||||||
|
RETURNING
|
||||||
|
actual_servings
|
||||||
|
|
@ -20,17 +20,17 @@
|
||||||
{% if loop.index as i32 <= food.target_servings %}
|
{% if loop.index as i32 <= food.target_servings %}
|
||||||
<label class="ok">
|
<label class="ok">
|
||||||
{% if loop.index as i32 <= food.actual_servings %}
|
{% 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 %}
|
{% else %}
|
||||||
<input type="checkbox">
|
<input type="checkbox" hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 }}" hx-target="#card-{{ food.id }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</label>
|
</label>
|
||||||
{% else %}
|
{% else %}
|
||||||
<label class="bad">
|
<label class="bad">
|
||||||
{% if loop.index as i32 <= food.actual_servings %}
|
{% 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 %}
|
{% else %}
|
||||||
<input type="checkbox">
|
<input type="checkbox" hx-post="/set/{{ food.id }}/to/{{ loop.index as i32 }}" hx-target="#card-{{ food.id }}">
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</label>
|
</label>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue