From c9632b3d981ce7ab1c6bb1e532f9c27245c9ced3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Forc=C3=A9n=20Mu=C3=B1oz?= Date: Wed, 22 May 2024 19:11:54 +0200 Subject: [PATCH] Added more descriptive names to account ui --- templates/account_summary.html | 113 ++++++++++++++++++ templates/{accounts.html => account_txs.html} | 0 webserver/src/main.rs | 6 +- webserver/src/routes/ui/account.rs | 67 +++++++++-- 4 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 templates/account_summary.html rename templates/{accounts.html => account_txs.html} (100%) diff --git a/templates/account_summary.html b/templates/account_summary.html new file mode 100644 index 0000000..9d208fe --- /dev/null +++ b/templates/account_summary.html @@ -0,0 +1,113 @@ +{% extends "base.html" %} +{% block title %}Account {{account.account_name}}{% endblock title %} +{% block body %} +
+ {{account.account_name}} +
+ + +
+
+
+

Net amount

+
+ +
+
+ +
+
+
+

Last transactions

+ + More + + + + + + + + + + + + + {% for tx in transactions %} + + + + + + + + + {% endfor %} + +
DescriptionDateAmountAccCategoryLink
{{tx.description}}{{tx.tx_date}}{{tx.amount/100}}{{tx.accumulated/100}}{% if tx.category %}{{categories[tx.category]}}{% endif %}Go to
+
+ + + + +{% endblock body %} + diff --git a/templates/accounts.html b/templates/account_txs.html similarity index 100% rename from templates/accounts.html rename to templates/account_txs.html diff --git a/webserver/src/main.rs b/webserver/src/main.rs index 0a34dc4..2100794 100644 --- a/webserver/src/main.rs +++ b/webserver/src/main.rs @@ -35,7 +35,7 @@ async fn main() { "/", Router::new() .route("/", get(routes::ui::index)) - .route("/accounts/id/:id", get(routes::ui::account::list)) + .route("/accounts/id/:id", get(routes::ui::account::show)) .route( "/accounts/id/:id/transactions/add", get(routes::ui::account::add_transactions_view), @@ -44,6 +44,10 @@ async fn main() { "/accounts/id/:id/transactions/add", post(routes::ui::account::add_transactions_action), ) + .route( + "/accounts/id/:id/transactions", + get(routes::ui::account::list_transactions), + ) .route("/transaction/:id", get(routes::ui::transaction::view)) .route("/transaction/:id", post(routes::ui::transaction::update)) .route( diff --git a/webserver/src/routes/ui/account.rs b/webserver/src/routes/ui/account.rs index 4bd2254..854a14b 100644 --- a/webserver/src/routes/ui/account.rs +++ b/webserver/src/routes/ui/account.rs @@ -18,8 +18,6 @@ use accounters::models::{account::Account, categories::Category, transaction::Tr pub struct AccountViewParams { from: Option, to: Option, - entries: Option, - page: Option, } fn parse_date(s: &str) -> Option> { @@ -30,17 +28,12 @@ fn parse_date(s: &str) -> Option> { Utc.with_ymd_and_hms(year, month, day, 0, 0, 0).single() } -pub async fn list( +pub async fn show( State(db): State>, State(tmpls): State>, uid: UserToken, Path(account_id): Path, - Query(AccountViewParams { - from, - to, - entries, - page, - }): Query, + Query(AccountViewParams { from, to }): Query, ) -> impl IntoResponse { let mut ctx = Context::new(); @@ -87,6 +80,60 @@ pub async fn list( .collect(); ctx.insert("categories", &categories); + let txs = match Transaction::list(db.as_ref(), account.get_id(), 10, 0, false).await { + Ok(t) => t, + Err(e) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + [(CONTENT_TYPE, "text/plain")], + format!("Error at loading transactions: {e}"), + ); + } + }; + + ctx.insert("account", &account); + ctx.insert("transactions", &txs); + ( + StatusCode::OK, + [(CONTENT_TYPE, "text/html;charset=utf-8")], + tmpls.render("account_summary.html", &ctx).unwrap(), + ) +} + +#[derive(Deserialize)] +pub struct AccountTxListParams { + entries: Option, + page: Option, +} + +pub async fn list_transactions( + State(db): State>, + State(tmpls): State>, + uid: UserToken, + Path(account_id): Path, + Query(AccountTxListParams { entries, page }): Query, +) -> impl IntoResponse { + let mut ctx = Context::new(); + + let account = match Account::get_by_id(db.as_ref(), account_id).await { + Ok(a) => a, + Err(e) => { + return ( + StatusCode::INTERNAL_SERVER_ERROR, + [(CONTENT_TYPE, "text/plain")], + format!("{e}"), + ); + } + }; + + if account.get_user() != uid.user_id { + return ( + StatusCode::UNAUTHORIZED, + [(CONTENT_TYPE, "text/plain")], + String::from("You cannot access this resource"), + ); + } + let n_entries = entries.unwrap_or(10).max(10); let page = page.unwrap_or(0).max(0); @@ -119,7 +166,7 @@ pub async fn list( ( StatusCode::OK, [(CONTENT_TYPE, "text/html;charset=utf-8")], - tmpls.render("accounts.html", &ctx).unwrap(), + tmpls.render("account_txs.html", &ctx).unwrap(), ) }