checkbox behaviour
This commit is contained in:
parent
d1585409a2
commit
f5d758ed2d
4 changed files with 43 additions and 4 deletions
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_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
8
src/set.sql
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
UPDATE
|
||||
food
|
||||
SET
|
||||
actual_servings = MAX(?2, 0)
|
||||
WHERE
|
||||
id = ?1
|
||||
RETURNING
|
||||
actual_servings
|
||||
Loading…
Add table
Add a link
Reference in a new issue