food-tracker/src/main.rs

131 lines
4.4 KiB
Rust
Raw Normal View History

2025-10-18 20:54:23 +02:00
use std::sync::{Arc, Mutex};
use askama::Template;
2025-10-19 14:23:41 +02:00
use axum::{
Router,
extract::{Path, State},
response::Html,
routing::{get, post},
};
use rusqlite::Connection;
2025-10-18 20:54:23 +02:00
#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate {
2025-10-18 21:21:58 +02:00
foods: Vec<Food>,
2025-10-18 22:12:51 +02:00
sum: i32,
2025-10-18 20:54:23 +02:00
}
#[derive(Template)]
#[template(path = "food-update.html")]
struct FoodUpdateTemplate {
food: Food,
sum: i32,
}
2025-10-18 20:54:23 +02:00
#[derive(Debug, Clone, PartialEq)]
2025-10-18 21:21:58 +02:00
struct Food {
2025-10-18 20:54:23 +02:00
id: i32,
2025-10-19 14:23:41 +02:00
portion: String,
name: String,
2025-10-18 21:21:58 +02:00
kc_per_serving: i32,
target_servings: i32,
actual_servings: i32,
2025-10-18 20:54:23 +02:00
}
2025-10-18 20:50:04 +02:00
#[tokio::main]
async fn main() {
2025-10-18 21:21:58 +02:00
let db_connecion_str = "./foods.db".to_string();
2025-10-18 20:54:23 +02:00
let conn = Connection::open(db_connecion_str).unwrap();
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
let conn = Arc::new(Mutex::new(conn));
2025-10-19 14:23:41 +02:00
let app = Router::new()
.route("/", get(root))
2025-10-18 22:12:51 +02:00
.route("/increase/{id}", post(increase))
.route("/decrease/{id}", post(decrease))
.with_state(conn);
2025-10-18 20:50:04 +02:00
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
2025-10-19 14:23:41 +02:00
async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
2025-10-18 20:54:23 +02:00
let conn = conn.lock().unwrap();
2025-10-19 14:23:41 +02:00
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(),
})
2025-10-18 20:54:23 +02:00
})
2025-10-19 14:23:41 +02:00
.unwrap()
.collect::<Result<_, _>>()
.unwrap();
let mut stmt = conn
.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food")
.unwrap();
2025-10-18 22:12:51 +02:00
let sum = stmt.query_one((), |row| row.get(0)).unwrap();
2025-10-19 14:23:41 +02:00
let index = IndexTemplate { foods, sum };
Html(index.render().unwrap())
2025-10-18 20:49:21 +02:00
}
2025-10-18 22:12:51 +02:00
2025-10-19 14:23:41 +02:00
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
2025-10-18 22:12:51 +02:00
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();
2025-10-19 14:23:41 +02:00
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();
2025-10-19 14:23:41 +02:00
let food = FoodUpdateTemplate { food, sum };
Html(food.render().unwrap())
2025-10-18 22:12:51 +02:00
}
2025-10-19 14:23:41 +02:00
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
2025-10-18 22:12:51 +02:00
let conn = conn.lock().unwrap();
2025-10-18 22:45:44 +02:00
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();
2025-10-18 22:12:51 +02:00
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();
2025-10-19 14:23:41 +02:00
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();
2025-10-19 14:23:41 +02:00
let food = FoodUpdateTemplate { food, sum };
Html(food.render().unwrap())
2025-10-18 22:12:51 +02:00
}