diff --git a/flake.nix b/flake.nix index c98584e..ca41ec9 100644 --- a/flake.nix +++ b/flake.nix @@ -22,6 +22,7 @@ pkgs.sqlite pkgs.superhtml pkgs.vscode-langservers-extracted + pkgs.rustfmt ]; }; } diff --git a/src/main.rs b/src/main.rs index 198ed63..8ae9c21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,13 @@ use std::sync::{Arc, Mutex}; -use axum::{extract::{Path, State}, response::Html, routing::{get, post}, Router}; -use rusqlite::Connection; use askama::Template; +use axum::{ + Router, + extract::{Path, State}, + response::Html, + routing::{get, post}, +}; +use rusqlite::Connection; #[derive(Template)] #[template(path = "index.html")] @@ -21,8 +26,8 @@ struct FoodUpdateTemplate { #[derive(Debug, Clone, PartialEq)] struct Food { id: i32, - portion: String, - name: String, + portion: String, + name: String, kc_per_serving: i32, target_servings: i32, actual_servings: i32, @@ -34,8 +39,9 @@ async fn main() { let conn = Connection::open(db_connecion_str).unwrap(); conn.execute(include_str!("create_tables.sql"), ()).unwrap(); let conn = Arc::new(Mutex::new(conn)); - - let app = Router::new().route("/", get(root)) + + let app = Router::new() + .route("/", get(root)) .route("/increase/{id}", post(increase)) .route("/decrease/{id}", post(decrease)) .with_state(conn); @@ -45,72 +51,80 @@ async fn main() { axum::serve(listener, app).await.unwrap(); } -async fn root( - State(conn): State>> -) -> Html { +async fn root(State(conn): State>>) -> Html { let conn = conn.lock().unwrap(); - let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food").unwrap(); - let foods = stmt.query_map((), |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(), + let mut stmt = conn + .prepare( + "SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food", + ) + .unwrap(); + let foods = stmt + .query_map((), |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().collect::>().unwrap(); - let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").unwrap(); + .unwrap() + .collect::>() + .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 index = IndexTemplate {foods, sum}; - Html( - index.render().unwrap() - ) + let index = IndexTemplate { foods, sum }; + Html(index.render().unwrap()) } -async fn increase( - State(conn): State>>, - Path(id): Path -) -> Html{ +async fn increase(State(conn): State>>, Path(id): Path) -> Html { 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(); 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 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() - ) + let food = FoodUpdateTemplate { food, sum }; + Html(food.render().unwrap()) } -async fn decrease( - State(conn): State>>, - Path(id): Path -) -> Html{ +async fn decrease(State(conn): State>>, Path(id): Path) -> Html { 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 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() - ) + let food = FoodUpdateTemplate { food, sum }; + Html(food.render().unwrap()) }