chore: initialized repository
This commit is contained in:
41
src/handlers.rs
Normal file
41
src/handlers.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use axum::response::Html;
|
||||
use askama::Template;
|
||||
use sqlx::SqlitePool;
|
||||
use axum::{extract::State, Form};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {
|
||||
pub weights: Vec<super::models::Weight>,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "input.html")]
|
||||
pub struct InputTemplate;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct WeightForm {
|
||||
date: String,
|
||||
weight: f64,
|
||||
}
|
||||
|
||||
pub async fn index(State(pool): State<SqlitePool>) -> Html<String> {
|
||||
let weights = super::models::get_all_weights(&pool).await.unwrap_or_default();
|
||||
let template = IndexTemplate { weights };
|
||||
Html(template.render().unwrap())
|
||||
}
|
||||
|
||||
pub async fn input_get() -> Html<String> {
|
||||
let template = InputTemplate;
|
||||
Html(template.render().unwrap())
|
||||
}
|
||||
|
||||
pub async fn input_post(
|
||||
State(pool): State<SqlitePool>,
|
||||
Form(form): Form<WeightForm>,
|
||||
) -> Html<String> {
|
||||
let user_id = "test_user"; // TODO: Implement OIDC to get real user_id
|
||||
super::models::insert_weight(&pool, user_id, &form.date, form.weight).await.unwrap();
|
||||
Html("<p>Weight added successfully!</p>".to_string())
|
||||
}
|
||||
Reference in New Issue
Block a user