summaryrefslogtreecommitdiff
path: root/src/web_frontend/transactions.rs
diff options
context:
space:
mode:
authorBenedict Börger <benedict@0xb8000.de>2019-04-14 20:06:27 +0200
committerBenedict Börger <benedict@0xb8000.de>2019-04-14 20:06:27 +0200
commit6712b19dbd2d21e5e89ac7a8d748369ea0a4612f (patch)
tree0e6b0d3ab2804d0473eb64db0410a3dcd340462a /src/web_frontend/transactions.rs
parentf28e8a59c781f24ed9399268cc2536aad0cf534d (diff)
[web-fronted] refactoring code
For historic reason, two handlers exists, one for POST and one for GET request. Merge these two to one GET request handler. This handler has optionally three parameteres: * filter: Filters the transactions by the given regex * date_start: Output only transactions after this date * date_end: Output only transactions before this date If more than one parameter is given, all filters must match for a transaction to be printed.
Diffstat (limited to 'src/web_frontend/transactions.rs')
-rw-r--r--src/web_frontend/transactions.rs75
1 files changed, 24 insertions, 51 deletions
diff --git a/src/web_frontend/transactions.rs b/src/web_frontend/transactions.rs
index 28e2dd1..1b6d22c 100644
--- a/src/web_frontend/transactions.rs
+++ b/src/web_frontend/transactions.rs
@@ -30,33 +30,16 @@ struct TransactionContext {
date_end : String
}
-#[derive(FromForm)]
-pub struct TransactionFilter {
- filter : String
-}
-
-impl TransactionFilter {
- pub fn apply_filter(&self, transactions : Vec<crate::banking::account::Transaction>) -> Vec<crate::banking::account::Transaction> {
- let asset_ini = "data/asset.ini";
- let asset : Asset = crate::banking::asset::Asset::from_ini_file(asset_ini);
- let transactions = asset.get_account_by_name("Girokonto");
- let acc;
- match transactions {
- Some(trans) => acc = trans,
- None => panic!("could not read file")
- }
- let t = acc.transactions;
- //self.filter.split("=").collect::<Vec<&str>>();
- let re = Regex::new(&self.filter).unwrap();
- let tmp = t.into_iter().filter(|transaction| re.is_match(&transaction.sender_name) || re.is_match(&transaction.reference) ).collect();
- tmp
+fn apply_transaction_filter(filter : String, transactions : Vec<crate::banking::account::Transaction>) -> Vec<crate::banking::account::Transaction> {
+ let re = Regex::new(&filter).unwrap();
+ let tmp = transactions.into_iter().filter(|transaction| re.is_match(&transaction.sender_name) || re.is_match(&transaction.reference) ).collect();
+ tmp
- }
}
-#[post("/transactions?<start>&<end>", data= "<transaction_filter>")]
-pub fn transaction_handler_post(start : Option<&RawStr>, end : Option<&RawStr>,
- transaction_filter : Form<TransactionFilter>) -> rocket_contrib::templates::Template {
+#[get("/transactions?<start>&<end>&<filter>")]
+pub fn transaction_handler(start : Option<&RawStr>, end : Option<&RawStr>,
+ filter : Option<&RawStr>) -> rocket_contrib::templates::Template {
let date_start = match start {
Some(s) => { let mut tmp = s.to_string();
tmp.push_str("-01");
@@ -69,36 +52,26 @@ pub fn transaction_handler_post(start : Option<&RawStr>, end : Option<&RawStr>,
chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() },
None => Utc::today().naive_utc()
};
- let input : TransactionFilter = transaction_filter.into_inner();
- let tmp = Vec::new();
- let ft = input.apply_filter(tmp);
- let context = TransactionContext { transactions : ft, account_name : String::from("TEST"),
- filter : input.filter, date_start : date_start.to_string(),
- date_end : date_end.to_string()};
- Template::render("transaction", context)
-}
-
-#[get("/transactions?<start>&<end>")]
-pub fn transaction_handler(start : Option<&RawStr>, end : Option<&RawStr>) -> rocket_contrib::templates::Template {
- let date_start = match start {
- Some(s) => { let mut tmp = s.to_string();
- tmp.push_str("-01");
- chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() },
- None => Utc::today().naive_utc()
+ let transaction_filter = match filter {
+ Some(s) => s.to_string(),
+ None => String::from("")
};
- let date_end = match end {
- Some(s) => { let mut tmp = s.to_string();
- tmp.push_str("-01");
- chrono::NaiveDate::parse_from_str(&tmp, "%Y-%m-%d").unwrap() },
- None => Utc::today().naive_utc()
- };
- let transactions = CsvFile::from_file("data/t.csv", ";", true);
- let t : Vec<crate::banking::account::Transaction> ;
+ let asset_ini = "data/asset.ini";
+ let asset : Asset = crate::banking::asset::Asset::from_ini_file(asset_ini);
+ let transactions = asset.get_account_by_name("Girokonto");
+ let acc;
match transactions {
- Ok(trans) => t = crate::banking::account::Transaction::from_sparkasse_csv_file(trans),
- Err(e) => panic!("could not read file {:?}", e)
+ Some(trans) => acc = trans,
+ None => panic!("could not read file")
}
- let context = TransactionContext { transactions: t, account_name : String::from("Girokonto"),
+ let t = acc.transactions;
+ // apply parameters
+ // apply date filters
+
+ // apply filter
+ let ft = apply_transaction_filter(transaction_filter, t);
+
+ let context = TransactionContext { transactions: ft, account_name : String::from("Girokonto"),
filter : String::from(""), date_start : date_start.to_string()[0..7].to_string(),
date_end : date_end.to_string()[0..7].to_string()};
Template::render("transaction", context)