format rust code
This commit is contained in:
parent
2bd2b95f05
commit
72f13b0a58
2 changed files with 73 additions and 58 deletions
|
|
@ -22,6 +22,7 @@
|
||||||
pkgs.sqlite
|
pkgs.sqlite
|
||||||
pkgs.superhtml
|
pkgs.superhtml
|
||||||
pkgs.vscode-langservers-extracted
|
pkgs.vscode-langservers-extracted
|
||||||
|
pkgs.rustfmt
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
130
src/main.rs
130
src/main.rs
|
|
@ -1,8 +1,13 @@
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use axum::{extract::{Path, State}, response::Html, routing::{get, post}, Router};
|
|
||||||
use rusqlite::Connection;
|
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use axum::{
|
||||||
|
Router,
|
||||||
|
extract::{Path, State},
|
||||||
|
response::Html,
|
||||||
|
routing::{get, post},
|
||||||
|
};
|
||||||
|
use rusqlite::Connection;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
|
|
@ -21,8 +26,8 @@ struct FoodUpdateTemplate {
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct Food {
|
struct Food {
|
||||||
id: i32,
|
id: i32,
|
||||||
portion: String,
|
portion: String,
|
||||||
name: String,
|
name: String,
|
||||||
kc_per_serving: i32,
|
kc_per_serving: i32,
|
||||||
target_servings: i32,
|
target_servings: i32,
|
||||||
actual_servings: i32,
|
actual_servings: i32,
|
||||||
|
|
@ -34,8 +39,9 @@ async fn main() {
|
||||||
let conn = Connection::open(db_connecion_str).unwrap();
|
let conn = Connection::open(db_connecion_str).unwrap();
|
||||||
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
|
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
|
||||||
let conn = Arc::new(Mutex::new(conn));
|
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("/increase/{id}", post(increase))
|
||||||
.route("/decrease/{id}", post(decrease))
|
.route("/decrease/{id}", post(decrease))
|
||||||
.with_state(conn);
|
.with_state(conn);
|
||||||
|
|
@ -45,72 +51,80 @@ async fn main() {
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn root(
|
async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
|
||||||
State(conn): State<Arc<Mutex<Connection>>>
|
|
||||||
) -> Html<String> {
|
|
||||||
let conn = conn.lock().unwrap();
|
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 mut stmt = conn
|
||||||
let foods = stmt.query_map((), |row| {
|
.prepare(
|
||||||
Ok(Food {
|
"SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food",
|
||||||
id: row.get(0).unwrap(),
|
)
|
||||||
portion: row.get(1).unwrap(),
|
.unwrap();
|
||||||
name: row.get(2).unwrap(),
|
let foods = stmt
|
||||||
kc_per_serving: row.get(3).unwrap(),
|
.query_map((), |row| {
|
||||||
target_servings: row.get(4).unwrap(),
|
Ok(Food {
|
||||||
actual_servings: row.get(5).unwrap(),
|
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();
|
.unwrap()
|
||||||
let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").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 sum = stmt.query_one((), |row| row.get(0)).unwrap();
|
||||||
let index = IndexTemplate {foods, sum};
|
let index = IndexTemplate { foods, sum };
|
||||||
Html(
|
Html(index.render().unwrap())
|
||||||
index.render().unwrap()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn increase(
|
async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||||
State(conn): State<Arc<Mutex<Connection>>>,
|
|
||||||
Path(id): Path<i32>
|
|
||||||
) -> Html<String>{
|
|
||||||
let conn = conn.lock().unwrap();
|
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();
|
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();
|
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 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 {
|
let food = stmt
|
||||||
id: row.get(0).unwrap(),
|
.query_one((id,), |row| {
|
||||||
portion: row.get(1).unwrap(),
|
Ok(Food {
|
||||||
name: row.get(2).unwrap(),
|
id: row.get(0).unwrap(),
|
||||||
kc_per_serving: row.get(3).unwrap(),
|
portion: row.get(1).unwrap(),
|
||||||
target_servings: row.get(4).unwrap(),
|
name: row.get(2).unwrap(),
|
||||||
actual_servings: row.get(5).unwrap(),
|
kc_per_serving: row.get(3).unwrap(),
|
||||||
})).unwrap();
|
target_servings: row.get(4).unwrap(),
|
||||||
let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").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 sum = stmt.query_one((), |row| row.get(0)).unwrap();
|
||||||
let food = FoodUpdateTemplate{food, sum};
|
let food = FoodUpdateTemplate { food, sum };
|
||||||
Html(
|
Html(food.render().unwrap())
|
||||||
food.render().unwrap()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
async fn decrease(
|
async fn decrease(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32>) -> Html<String> {
|
||||||
State(conn): State<Arc<Mutex<Connection>>>,
|
|
||||||
Path(id): Path<i32>
|
|
||||||
) -> Html<String>{
|
|
||||||
let conn = conn.lock().unwrap();
|
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();
|
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();
|
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 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 {
|
let food = stmt
|
||||||
id: row.get(0).unwrap(),
|
.query_one((id,), |row| {
|
||||||
portion: row.get(1).unwrap(),
|
Ok(Food {
|
||||||
name: row.get(2).unwrap(),
|
id: row.get(0).unwrap(),
|
||||||
kc_per_serving: row.get(3).unwrap(),
|
portion: row.get(1).unwrap(),
|
||||||
target_servings: row.get(4).unwrap(),
|
name: row.get(2).unwrap(),
|
||||||
actual_servings: row.get(5).unwrap(),
|
kc_per_serving: row.get(3).unwrap(),
|
||||||
})).unwrap();
|
target_servings: row.get(4).unwrap(),
|
||||||
let mut stmt = conn.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food").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 sum = stmt.query_one((), |row| row.get(0)).unwrap();
|
||||||
let food = FoodUpdateTemplate {food,sum};
|
let food = FoodUpdateTemplate { food, sum };
|
||||||
Html(
|
Html(food.render().unwrap())
|
||||||
food.render().unwrap()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue