format rust code

This commit is contained in:
Khaïs COLIN 2025-10-19 14:23:41 +02:00
parent 2bd2b95f05
commit 72f13b0a58
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 73 additions and 58 deletions

View file

@ -22,6 +22,7 @@
pkgs.sqlite
pkgs.superhtml
pkgs.vscode-langservers-extracted
pkgs.rustfmt
];
};
}

View file

@ -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<Arc<Mutex<Connection>>>
) -> Html<String> {
async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
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::<Result<_, _>>().unwrap();
let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").unwrap();
.unwrap()
.collect::<Result<_, _>>()
.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<Arc<Mutex<Connection>>>,
Path(id): Path<i32>
) -> Html<String>{
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
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<Arc<Mutex<Connection>>>,
Path(id): Path<i32>
) -> Html<String>{
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
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())
}