diff --git a/foods.db b/foods.db index 356d231..5a4e2b6 100644 Binary files a/foods.db and b/foods.db differ diff --git a/src/main.rs b/src/main.rs index 8ae9c21..420b75a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,7 +51,7 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -async fn root(State(conn): State>>) -> Html { +fn get_foods(conn: &Arc>) -> Vec { let conn = conn.lock().unwrap(); let mut stmt = conn .prepare( @@ -72,18 +72,33 @@ async fn root(State(conn): State>>) -> Html { .unwrap() .collect::>() .unwrap(); + foods +} + +fn get_sum(conn: &Arc>) -> i32 { + let conn = conn.lock().unwrap(); let mut stmt = conn .prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food") .unwrap(); let sum = stmt.query_one((), |row| row.get(0)).unwrap(); + sum +} + +async fn root(State(conn): State>>) -> Html { + let foods = get_foods(&conn); + let sum = get_sum(&conn); let index = IndexTemplate { foods, sum }; Html(index.render().unwrap()) } -async fn increase(State(conn): State>>, Path(id): Path) -> Html { +fn do_increase(conn: &Arc>, id: i32) { 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(); stmt.execute((id,)).unwrap(); +} + +fn get_food(conn: &Arc>, id: i32) -> Food { + let conn = conn.lock().unwrap(); let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food WHERE id = ?1").unwrap(); let food = stmt .query_one((id,), |row| { @@ -97,34 +112,27 @@ async fn increase(State(conn): State>>, Path(id): Path>>, Path(id): Path) -> Html { + +async fn increase(State(conn): State>>, Path(id): Path) -> Html { + do_increase(&conn, id); + let food = get_food(&conn, id); + let sum = get_sum(&conn); + let update = FoodUpdateTemplate { food, sum }; + Html(update.render().unwrap()) +} + +fn do_decrease(conn: &Arc>, id: i32) { let conn = conn.lock().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(); - let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food WHERE id = ?1").unwrap(); - let food = stmt - .query_one((id,), |row| { - Ok(Food { - id: row.get(0).unwrap(), - portion: row.get(1).unwrap(), - name: row.get(2).unwrap(), - kc_per_serving: row.get(3).unwrap(), - target_servings: row.get(4).unwrap(), - actual_servings: row.get(5).unwrap(), - }) - }) - .unwrap(); - let mut stmt = conn - .prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food") - .unwrap(); - let sum = stmt.query_one((), |row| row.get(0)).unwrap(); - let food = FoodUpdateTemplate { food, sum }; - Html(food.render().unwrap()) +} + +async fn decrease(State(conn): State>>, Path(id): Path) -> Html { + do_decrease(&conn, id); + let food = get_food(&conn, id); + let sum = get_sum(&conn); + let update = FoodUpdateTemplate { food, sum }; + Html(update.render().unwrap()) }