Added the capability to upload CSV files
This commit is contained in:
parent
90b02eef79
commit
d1e736d7a7
19 changed files with 678 additions and 99 deletions
147
webserver/src/routes/ui/account.rs
Normal file
147
webserver/src/routes/ui/account.rs
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, Query, State},
|
||||
response::IntoResponse,
|
||||
Json,
|
||||
};
|
||||
use chrono::{DateTime, Utc};
|
||||
use hyper::{header::CONTENT_TYPE, StatusCode};
|
||||
use serde::Deserialize;
|
||||
use sqlx::SqlitePool;
|
||||
use tera::{Context, Tera};
|
||||
|
||||
use crate::users::UserToken;
|
||||
use accounters::models::{transaction::TxConflictResolutionMode, Account, Transaction};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct AccountViewParams {
|
||||
movements: Option<i32>,
|
||||
}
|
||||
|
||||
pub async fn list(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
State(tmpls): State<Arc<Tera>>,
|
||||
uid: UserToken,
|
||||
Path(account_id): Path<i32>,
|
||||
Query(AccountViewParams { movements }): Query<AccountViewParams>,
|
||||
) -> 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 txs = match Transaction::list(
|
||||
db.as_ref(),
|
||||
account.get_id(),
|
||||
movements.unwrap_or(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);
|
||||
ctx.insert("n_txs", &txs.len());
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(CONTENT_TYPE, "text/html;charset=utf-8")],
|
||||
tmpls.render("accounts.html", &ctx).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
pub async fn add_transactions_view(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
State(tmpls): State<Arc<Tera>>,
|
||||
uid: UserToken,
|
||||
Path(account_id): Path<i32>,
|
||||
) -> impl IntoResponse {
|
||||
let mut ctxt = Context::new();
|
||||
ctxt.insert("account_id", &account_id);
|
||||
|
||||
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"),
|
||||
);
|
||||
}
|
||||
|
||||
ctxt.insert("account", &account);
|
||||
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(CONTENT_TYPE, "text/html;charset=utf-8")],
|
||||
tmpls.render("accounts_add_txs.html", &ctxt).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct CreateTransactionRequest {
|
||||
date: DateTime<Utc>,
|
||||
description: String,
|
||||
amount: f32,
|
||||
}
|
||||
|
||||
pub async fn add_transactions_action(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
uid: UserToken,
|
||||
Path(account_id): Path<i32>,
|
||||
Json(body): Json<Vec<CreateTransactionRequest>>,
|
||||
) -> impl IntoResponse {
|
||||
// TODO missing user id check
|
||||
for tx in body.iter() {
|
||||
if let Err(e) = Transaction::new(
|
||||
db.as_ref(),
|
||||
account_id,
|
||||
&tx.description,
|
||||
&tx.date,
|
||||
None,
|
||||
(tx.amount * 100.0).round() as i32,
|
||||
TxConflictResolutionMode::Nothing,
|
||||
)
|
||||
.await
|
||||
{
|
||||
return (StatusCode::INTERNAL_SERVER_ERROR, format!("{e:?}"));
|
||||
}
|
||||
}
|
||||
(StatusCode::OK, String::new())
|
||||
}
|
||||
72
webserver/src/routes/ui/categories.rs
Normal file
72
webserver/src/routes/ui/categories.rs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use accounters::models::categories::Category;
|
||||
use axum::{
|
||||
extract::{Form, State},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use hyper::{header::CONTENT_TYPE, StatusCode};
|
||||
use serde::Deserialize;
|
||||
use sqlx::SqlitePool;
|
||||
use tera::{Context, Tera};
|
||||
|
||||
use crate::users::UserToken;
|
||||
|
||||
pub async fn list(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
State(tmpl): State<Arc<Tera>>,
|
||||
uid: UserToken,
|
||||
) -> impl IntoResponse {
|
||||
match Category::list(db.as_ref()).await {
|
||||
Ok(categories) => {
|
||||
let mut ctx = Context::new();
|
||||
ctx.insert("categories", &categories);
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(CONTENT_TYPE, "text/html;charset=utf-8")],
|
||||
tmpl.render("categories_list.html", &ctx).unwrap(),
|
||||
)
|
||||
}
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
[(CONTENT_TYPE, "text/plain;charset=utf-8")],
|
||||
format!("{e}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn new_view(State(tmpl): State<Arc<Tera>>, uid: UserToken) -> impl IntoResponse {
|
||||
(
|
||||
StatusCode::OK,
|
||||
[(CONTENT_TYPE, "text/html;charset=utf-8")],
|
||||
tmpl.render("categories_new.html", &Context::new()).unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct NewRuleParams {
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
pub async fn new_action(
|
||||
State(db): State<Arc<SqlitePool>>,
|
||||
State(tmpls): State<Arc<Tera>>,
|
||||
uid: UserToken,
|
||||
Form(params): Form<NewRuleParams>,
|
||||
) -> impl IntoResponse {
|
||||
match Category::new(db.as_ref(), ¶ms.name, ¶ms.description).await {
|
||||
Ok(_) => (
|
||||
StatusCode::OK,
|
||||
[(CONTENT_TYPE, "text/html;charset=utf-8")],
|
||||
tmpls
|
||||
.render("rules_new_success.html", &Context::new())
|
||||
.unwrap(),
|
||||
),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
[(CONTENT_TYPE, "text/plain;charset=utf-8")],
|
||||
format!("{e}"),
|
||||
),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue