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())
|
||||
}
|
||||
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod handlers;
|
||||
pub mod models;
|
||||
27
src/main.rs
Normal file
27
src/main.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use axum::{
|
||||
routing::get,
|
||||
Router,
|
||||
};
|
||||
use tower_http::services::ServeDir;
|
||||
use sqlx::SqlitePool;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Set up database
|
||||
let database_url = "sqlite:weight_tracker.db";
|
||||
let pool = SqlitePool::connect(database_url).await.expect("Failed to connect to database");
|
||||
|
||||
// build our application with a route
|
||||
let app = Router::new()
|
||||
.route("/", get(weight_tracker::handlers::index))
|
||||
.route("/input", get(weight_tracker::handlers::input_get).post(weight_tracker::handlers::input_post))
|
||||
.with_state(pool)
|
||||
.nest_service("/static", ServeDir::new("static"));
|
||||
|
||||
// run it
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
|
||||
.await
|
||||
.unwrap();
|
||||
println!("listening on {}", listener.local_addr().unwrap());
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
26
src/models.rs
Normal file
26
src/models.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::FromRow;
|
||||
|
||||
#[derive(Debug, FromRow, Serialize, Deserialize)]
|
||||
pub struct Weight {
|
||||
pub id: i64,
|
||||
pub user_id: String,
|
||||
pub date: String,
|
||||
pub weight: f64,
|
||||
}
|
||||
|
||||
pub async fn get_all_weights(pool: &sqlx::SqlitePool) -> Result<Vec<Weight>, sqlx::Error> {
|
||||
sqlx::query_as::<_, Weight>("SELECT id, user_id, date, weight FROM weights ORDER BY date DESC")
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn insert_weight(pool: &sqlx::SqlitePool, user_id: &str, date: &str, weight: f64) -> Result<i64, sqlx::Error> {
|
||||
let result = sqlx::query("INSERT INTO weights (user_id, date, weight) VALUES (?, ?, ?)")
|
||||
.bind(user_id)
|
||||
.bind(date)
|
||||
.bind(weight)
|
||||
.execute(pool)
|
||||
.await?;
|
||||
Ok(result.last_insert_rowid())
|
||||
}
|
||||
Reference in New Issue
Block a user