table rendering
This commit is contained in:
parent
d4db9155a6
commit
bc4203aee0
5 changed files with 59 additions and 34 deletions
|
|
@ -20,6 +20,8 @@
|
||||||
pkgs.rust-analyzer
|
pkgs.rust-analyzer
|
||||||
pkgs.clippy
|
pkgs.clippy
|
||||||
pkgs.sqlite
|
pkgs.sqlite
|
||||||
|
pkgs.superhtml
|
||||||
|
pkgs.vscode-langservers-extracted
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -1,6 +1,8 @@
|
||||||
CREATE TABLE IF NOT EXISTS task (
|
CREATE TABLE IF NOT EXISTS food (
|
||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
|
portion TEXT NOT NULL,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
worth FLOAT NOT NULL DEFAULT 1.0,
|
kc_per_serving INTEGER NOT NULL DEFAULT 0,
|
||||||
times_completed INTEGER NOT NULL DEFAULT 0
|
target_servings INTEGER NOT NULL DEFAULT 1,
|
||||||
|
actual_servings INTEGER NOT NULL DEFAULT 0
|
||||||
);
|
);
|
||||||
|
|
|
||||||
28
src/main.rs
28
src/main.rs
|
|
@ -7,20 +7,22 @@ use askama::Template;
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct IndexTemplate {
|
struct IndexTemplate {
|
||||||
tasks: Vec<Task>,
|
foods: Vec<Food>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
struct Task {
|
struct Food {
|
||||||
id: i32,
|
id: i32,
|
||||||
|
portion: String,
|
||||||
name: String,
|
name: String,
|
||||||
worth: f32,
|
kc_per_serving: i32,
|
||||||
times_completed: i32,
|
target_servings: i32,
|
||||||
|
actual_servings: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn 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();
|
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));
|
||||||
|
|
@ -36,16 +38,18 @@ async fn root(
|
||||||
State(conn): State<Arc<Mutex<Connection>>>
|
State(conn): State<Arc<Mutex<Connection>>>
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
let conn = conn.lock().unwrap();
|
let conn = conn.lock().unwrap();
|
||||||
let mut stmt = conn.prepare("SELECT id, name, worth, times_completed FROM task").unwrap();
|
let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings FROM food").unwrap();
|
||||||
let tasks = stmt.query_map((), |row| {
|
let foods = stmt.query_map((), |row| {
|
||||||
Ok(Task {
|
Ok(Food {
|
||||||
id: row.get(0).unwrap(),
|
id: row.get(0).unwrap(),
|
||||||
name: row.get(1).unwrap(),
|
portion: row.get(1).unwrap(),
|
||||||
worth: row.get(2).unwrap(),
|
name: row.get(2).unwrap(),
|
||||||
times_completed: row.get(3).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().collect::<Result<_, _>>().unwrap();
|
||||||
let index = IndexTemplate {tasks};
|
let index = IndexTemplate {foods};
|
||||||
Html(
|
Html(
|
||||||
index.render().unwrap()
|
index.render().unwrap()
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,38 @@
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
|
||||||
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||||
<title>Task Counter</title>
|
<title>Food Tracker</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
|
||||||
<header>
|
<body>
|
||||||
<h1>Task Counter!</h1>
|
|
||||||
</header>
|
|
||||||
<main>
|
<main>
|
||||||
<ul>
|
<table style="display: flex">
|
||||||
{% for task in tasks %}
|
<tr>
|
||||||
<li>{{ task.name }}</li>
|
<th>Portion</th>
|
||||||
|
<th>Lebensmittel</th>
|
||||||
|
<th>kc.</th>
|
||||||
|
<th>Servings (target)</th>
|
||||||
|
</tr>
|
||||||
|
{% for food in foods %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ food.portion }}</td>
|
||||||
|
<td>{{ food.name }}</td>
|
||||||
|
<td style="text-align: right;">{{ food.kc_per_serving }}</td>
|
||||||
|
<td>
|
||||||
|
<progress max="{{food.target_servings}}" value="{{food.actual_servings}}" {% if food.actual_servings>
|
||||||
|
food.target_servings %}style="accent-color: red;"{% endif %}></progress>
|
||||||
|
{{ food.actual_servings }}/{{ food.target_servings }}
|
||||||
|
<button>+</button>
|
||||||
|
<button>-</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</table>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue