From 6712b19dbd2d21e5e89ac7a8d748369ea0a4612f Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Sun, 14 Apr 2019 20:06:27 +0200 Subject: [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. --- src/web_frontend/transactions.rs | 75 +++++++++++++--------------------------- 1 file changed, 24 insertions(+), 51 deletions(-) (limited to 'src/web_frontend') 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) -> Vec { - 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::>(); - 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) -> Vec { + 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?&", data= "")] -pub fn transaction_handler_post(start : Option<&RawStr>, end : Option<&RawStr>, - transaction_filter : Form) -> rocket_contrib::templates::Template { +#[get("/transactions?&&")] +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?&")] -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 ; + 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) -- cgit v1.2.3-70-g09d2