table rendering
This commit is contained in:
parent
d4db9155a6
commit
bc4203aee0
5 changed files with 59 additions and 34 deletions
30
src/main.rs
30
src/main.rs
|
|
@ -7,20 +7,22 @@ use askama::Template;
|
|||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
struct IndexTemplate {
|
||||
tasks: Vec<Task>,
|
||||
foods: Vec<Food>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
struct Task {
|
||||
struct Food {
|
||||
id: i32,
|
||||
name: String,
|
||||
worth: f32,
|
||||
times_completed: i32,
|
||||
portion: String,
|
||||
name: String,
|
||||
kc_per_serving: i32,
|
||||
target_servings: i32,
|
||||
actual_servings: i32,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let db_connecion_str = "./tasks.db".to_string();
|
||||
let db_connecion_str = "./foods.db".to_string();
|
||||
let conn = Connection::open(db_connecion_str).unwrap();
|
||||
conn.execute(include_str!("create_tables.sql"), ()).unwrap();
|
||||
let conn = Arc::new(Mutex::new(conn));
|
||||
|
|
@ -36,16 +38,18 @@ async fn root(
|
|||
State(conn): State<Arc<Mutex<Connection>>>
|
||||
) -> Html<String> {
|
||||
let conn = conn.lock().unwrap();
|
||||
let mut stmt = conn.prepare("SELECT id, name, worth, times_completed FROM task").unwrap();
|
||||
let tasks = stmt.query_map((), |row| {
|
||||
Ok(Task {
|
||||
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(),
|
||||
name: row.get(1).unwrap(),
|
||||
worth: row.get(2).unwrap(),
|
||||
times_completed: row.get(3).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 index = IndexTemplate {tasks};
|
||||
let index = IndexTemplate {foods};
|
||||
Html(
|
||||
index.render().unwrap()
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue