started with new design
This commit is contained in:
parent
28ea420ecb
commit
9238366137
6 changed files with 525 additions and 38 deletions
33
src/main.rs
33
src/main.rs
|
|
@ -15,18 +15,32 @@ use tower_request_id::{RequestId, RequestIdLayer};
|
|||
use tracing::{debug, error, info, info_span};
|
||||
use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Sum {
|
||||
kc: i32,
|
||||
bs: i32,
|
||||
protein: i32,
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Sum {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
foods: Vec<Food>,
|
||||
sum: i32,
|
||||
sum: Sum,
|
||||
date: String,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "food-update.html")]
|
||||
struct FoodUpdateTemplate {
|
||||
food: Food,
|
||||
sum: i32,
|
||||
sum: Sum,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
|
|
@ -179,18 +193,25 @@ fn get_foods(conn: &ConnState) -> rusqlite::Result<Vec<Food>> {
|
|||
Ok(foods)
|
||||
}
|
||||
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<i32> {
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> rusqlite::Result<Sum> {
|
||||
let conn = conn.lock();
|
||||
let mut stmt = PreparedStatements::get_sum(&conn);
|
||||
let sum = stmt.query_one((), |row| row.get(0))?;
|
||||
debug!(sum);
|
||||
let kc = stmt.query_one((), |row| row.get(0))?;
|
||||
let sum = Sum {
|
||||
kc,
|
||||
bs: 99,
|
||||
protein: 99,
|
||||
};
|
||||
debug!(?sum);
|
||||
Ok(sum)
|
||||
}
|
||||
|
||||
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 index = IndexTemplate { foods, sum };
|
||||
let now = chrono::Local::now();
|
||||
let date = format!("{}", now.format("%Y-%M-%d"));
|
||||
let index = IndexTemplate { foods, sum, date };
|
||||
Ok(Html(
|
||||
index
|
||||
.render()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue