2025-10-18 20:54:23 +02:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
2025-10-19 13:57:14 +02:00
|
|
|
use axum::{extract::{Path, State}, response::Html, routing::{get, post}, Router};
|
2025-10-18 20:54:23 +02:00
|
|
|
use rusqlite::Connection;
|
|
|
|
|
use askama::Template;
|
|
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
|
2025-10-19 13:57:14 +02:00
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "food.html")]
|
|
|
|
|
struct FoodTemplate {
|
|
|
|
|
food: Food,
|
|
|
|
|
}
|
|
|
|
|
|
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-18 21:21:58 +02:00
|
|
|
portion: String,
|
|
|
|
|
name: String,
|
|
|
|
|
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-18 22:12:51 +02:00
|
|
|
let app = Router::new().route("/", get(root))
|
|
|
|
|
.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-18 20:54:23 +02:00
|
|
|
async fn root(
|
|
|
|
|
State(conn): State<Arc<Mutex<Connection>>>
|
|
|
|
|
) -> Html<String> {
|
|
|
|
|
let conn = conn.lock().unwrap();
|
2025-10-18 21:21:58 +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 {
|
2025-10-18 20:54:23 +02:00
|
|
|
id: row.get(0).unwrap(),
|
2025-10-18 21:21:58 +02:00
|
|
|
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
|
|
|
})
|
|
|
|
|
}).unwrap().collect::<Result<_, _>>().unwrap();
|
2025-10-18 22:12:51 +02:00
|
|
|
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};
|
2025-10-18 20:54:23 +02:00
|
|
|
Html(
|
|
|
|
|
index.render().unwrap()
|
|
|
|
|
)
|
2025-10-18 20:49:21 +02:00
|
|
|
}
|
2025-10-18 22:12:51 +02:00
|
|
|
|
|
|
|
|
async fn increase(
|
|
|
|
|
State(conn): State<Arc<Mutex<Connection>>>,
|
|
|
|
|
Path(id): Path<i32>
|
2025-10-19 13:57:14 +02:00
|
|
|
) -> 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();
|
2025-10-19 13:57:14 +02:00
|
|
|
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 food = FoodTemplate {food};
|
|
|
|
|
Html(
|
|
|
|
|
food.render().unwrap()
|
|
|
|
|
)
|
2025-10-18 22:12:51 +02:00
|
|
|
}
|
|
|
|
|
async fn decrease(
|
|
|
|
|
State(conn): State<Arc<Mutex<Connection>>>,
|
|
|
|
|
Path(id): Path<i32>
|
2025-10-19 13:57:14 +02:00
|
|
|
) -> 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();
|
2025-10-19 13:57:14 +02:00
|
|
|
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 food = FoodTemplate {food};
|
|
|
|
|
Html(
|
|
|
|
|
food.render().unwrap()
|
|
|
|
|
)
|
2025-10-18 22:12:51 +02:00
|
|
|
}
|