full functionality
This commit is contained in:
parent
bc4203aee0
commit
fc64df2b2a
3 changed files with 41 additions and 7 deletions
BIN
foods.db
BIN
foods.db
Binary file not shown.
31
src/main.rs
31
src/main.rs
|
|
@ -1,6 +1,6 @@
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use axum::{extract::State, response::Html, routing::get, Router};
|
use axum::{extract::{Path, State}, response::{Html, Redirect}, routing::{get, post}, Router};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
|
||||||
|
|
@ -8,6 +8,7 @@ use askama::Template;
|
||||||
#[template(path = "index.html")]
|
#[template(path = "index.html")]
|
||||||
struct IndexTemplate {
|
struct IndexTemplate {
|
||||||
foods: Vec<Food>,
|
foods: Vec<Food>,
|
||||||
|
sum: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
|
@ -27,7 +28,10 @@ async fn main() {
|
||||||
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)).with_state(conn);
|
let app = Router::new().route("/", get(root))
|
||||||
|
.route("/increase/{id}", post(increase))
|
||||||
|
.route("/decrease/{id}", post(decrease))
|
||||||
|
.with_state(conn);
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap();
|
||||||
println!("listening on {}", listener.local_addr().unwrap());
|
println!("listening on {}", listener.local_addr().unwrap());
|
||||||
|
|
@ -49,8 +53,29 @@ async fn root(
|
||||||
actual_servings: row.get(5).unwrap(),
|
actual_servings: row.get(5).unwrap(),
|
||||||
})
|
})
|
||||||
}).unwrap().collect::<Result<_, _>>().unwrap();
|
}).unwrap().collect::<Result<_, _>>().unwrap();
|
||||||
let index = IndexTemplate {foods};
|
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(
|
Html(
|
||||||
index.render().unwrap()
|
index.render().unwrap()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn increase(
|
||||||
|
State(conn): State<Arc<Mutex<Connection>>>,
|
||||||
|
Path(id): Path<i32>
|
||||||
|
) -> Redirect{
|
||||||
|
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();
|
||||||
|
Redirect::to("/")
|
||||||
|
}
|
||||||
|
async fn decrease(
|
||||||
|
State(conn): State<Arc<Mutex<Connection>>>,
|
||||||
|
Path(id): Path<i32>
|
||||||
|
) -> Redirect{
|
||||||
|
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();
|
||||||
|
Redirect::to("/")
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<main>
|
<main>
|
||||||
<table style="display: flex">
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Portion</th>
|
<th>Portion</th>
|
||||||
<th>Lebensmittel</th>
|
<th>Lebensmittel</th>
|
||||||
|
|
@ -24,13 +24,22 @@
|
||||||
<td style="text-align: right;">{{ food.kc_per_serving }}</td>
|
<td style="text-align: right;">{{ food.kc_per_serving }}</td>
|
||||||
<td>
|
<td>
|
||||||
<progress max="{{food.target_servings}}" value="{{food.actual_servings}}" {% if food.actual_servings>
|
<progress max="{{food.target_servings}}" value="{{food.actual_servings}}" {% if food.actual_servings>
|
||||||
food.target_servings %}style="accent-color: red;"{% endif %}></progress>
|
food.target_servings %}
|
||||||
|
style="accent-color: red;"
|
||||||
|
{% endif %}>
|
||||||
|
</progress>
|
||||||
{{ food.actual_servings }}/{{ food.target_servings }}
|
{{ food.actual_servings }}/{{ food.target_servings }}
|
||||||
<button>+</button>
|
<form method="post" style="display: inline;">
|
||||||
<button>-</button>
|
<button formaction="/increase/{{ food.id }}">+</button>
|
||||||
|
<button formaction="/decrease/{{ food.id }}">-</button>
|
||||||
|
</form>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<tr>
|
||||||
|
<th colspan=3 style="text-align: right;">kc.:</th>
|
||||||
|
<th style="text-align: left;">{{ sum }}</th>
|
||||||
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</main>
|
</main>
|
||||||
</body>
|
</body>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue