87 lines
2.0 KiB
Rust
87 lines
2.0 KiB
Rust
use std::collections::HashMap;
|
|
use std::sync::LazyLock;
|
|
|
|
use rocket::form::Form;
|
|
use rocket::fs::{FileServer, TempFile};
|
|
use rocket::http::Status;
|
|
use rocket::response::Redirect;
|
|
use rocket::serde::Serialize;
|
|
use rocket_dyn_templates::{context, Template};
|
|
|
|
#[derive(Serialize)]
|
|
struct Transaction {
|
|
id: u32,
|
|
amount: f64,
|
|
description: String,
|
|
date: String,
|
|
}
|
|
|
|
struct Database {
|
|
transactions: HashMap<i32, Transaction>,
|
|
}
|
|
|
|
#[derive(rocket::FromForm)]
|
|
struct TransactionPostData<'r> {
|
|
amount: f32,
|
|
notes: String,
|
|
photo: Vec<TempFile<'r>>,
|
|
}
|
|
|
|
static DB: LazyLock<Database> = LazyLock::new(|| {
|
|
let mut transactions = HashMap::new();
|
|
transactions.insert(
|
|
5411,
|
|
Transaction {
|
|
id: 5411,
|
|
amount: 140.38,
|
|
description: "THE HOME DEPOT #2218".into(),
|
|
date: "March 2nd, 2025".into(),
|
|
},
|
|
);
|
|
Database { transactions }
|
|
});
|
|
|
|
#[rocket::get("/")]
|
|
async fn index() -> Redirect {
|
|
Redirect::to(rocket::uri!(transaction_list()))
|
|
}
|
|
|
|
#[rocket::get("/transactions")]
|
|
async fn transaction_list() -> Template {
|
|
let transactions: Vec<_> = DB.transactions.values().collect();
|
|
Template::render("transaction-list", context! {
|
|
transactions: transactions,
|
|
})
|
|
}
|
|
|
|
#[rocket::get("/transactions/<id>")]
|
|
async fn get_transaction(id: i32) -> Option<Template> {
|
|
let txn = DB.transactions.get(&id)?;
|
|
Some(Template::render("transaction", txn))
|
|
}
|
|
|
|
#[rocket::post("/transactions/<id>", data = "<form>")]
|
|
async fn update_transaction(
|
|
id: f32,
|
|
form: Form<TransactionPostData<'_>>,
|
|
) -> (Status, &'static str) {
|
|
println!("{} {} {}", id, form.amount, form.photo.len());
|
|
(Status::ImATeapot, "")
|
|
}
|
|
|
|
#[rocket::launch]
|
|
async fn rocket() -> _ {
|
|
rocket::build()
|
|
.mount(
|
|
"/",
|
|
rocket::routes![
|
|
index,
|
|
transaction_list,
|
|
get_transaction,
|
|
update_transaction
|
|
],
|
|
)
|
|
.mount("/static", FileServer::from("js/dist"))
|
|
.attach(Template::fairing())
|
|
}
|