put sql queries in separate functions
This commit is contained in:
parent
72f13b0a58
commit
5cf4a12eae
2 changed files with 36 additions and 28 deletions
BIN
foods.db
BIN
foods.db
Binary file not shown.
64
src/main.rs
64
src/main.rs
|
|
@ -51,7 +51,7 @@ async fn main() {
|
|||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
|
||||
fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> {
|
||||
let conn = conn.lock().unwrap();
|
||||
let mut stmt = conn
|
||||
.prepare(
|
||||
|
|
@ -72,18 +72,33 @@ async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
|
|||
.unwrap()
|
||||
.collect::<Result<_, _>>()
|
||||
.unwrap();
|
||||
foods
|
||||
}
|
||||
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> 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<Arc<Mutex<Connection>>>) -> Html<String> {
|
||||
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<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
fn do_increase(conn: &Arc<Mutex<Connection>>, 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<Mutex<Connection>>, 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<Arc<Mutex<Connection>>>, Path(id): Path<i32
|
|||
})
|
||||
})
|
||||
.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())
|
||||
food
|
||||
}
|
||||
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
|
||||
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
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<Mutex<Connection>>, 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<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||
do_decrease(&conn, id);
|
||||
let food = get_food(&conn, id);
|
||||
let sum = get_sum(&conn);
|
||||
let update = FoodUpdateTemplate { food, sum };
|
||||
Html(update.render().unwrap())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue