show protein and fiber sums

This commit is contained in:
Khaïs COLIN 2025-10-25 13:45:02 +02:00
parent 07bfc65b67
commit fa0d8930b8
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 16 additions and 10 deletions

BIN
foods.db

Binary file not shown.

View file

@ -1,4 +1,6 @@
SELECT
SUM(kc_per_serving * actual_servings) AS kc
SUM(kc_per_serving * actual_servings) AS kc,
SUM(protein_per_portion * actual_servings) AS protein,
SUM(fiber_per_portion * actual_servings) AS bs
FROM
food

View file

@ -18,8 +18,8 @@ use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _
#[derive(Debug)]
struct Sum {
kc: i32,
bs: i32,
protein: i32,
bs: f32,
protein: f32,
}
impl std::fmt::Display for Sum {
@ -251,12 +251,13 @@ fn get_foods(conn: &ConnState) -> rusqlite::Result<Vec<Food>> {
fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<Sum> {
let conn = conn.lock();
let mut stmt = PreparedStatements::get_sum(&conn);
let kc = stmt.query_one((), |row| row.get(0))?;
let sum = Sum {
kc,
bs: 99,
protein: 99,
};
let sum = stmt.query_one((), |row| {
Ok(Sum {
kc: row.get(0)?,
bs: row.get(1)?,
protein: row.get(2)?,
})
})?;
debug!(?sum);
Ok(sum)
}
@ -269,7 +270,10 @@ fn get_date() -> String {
async fn root(State(conn): State<ConnState>) -> Result<Html<String>, StatusCode> {
let foods = get_foods(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let sum = get_sum(&conn).map_err(|e| {
error!(?e);
StatusCode::INTERNAL_SERVER_ERROR
})?;
let date = get_date();
let index = IndexTemplate { foods, sum, date };
Ok(Html(