From 18dd51842832ac21d81b398cc512e9c6ef245039 Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Mon, 24 Jun 2019 21:49:51 +0200 Subject: [web_frontend] move transaction_filter to util Since the sites transactions and chart both filter the transactions, move the function to util, whre both can access it. --- src/web_frontend/transactions.rs | 69 +++------------------------------------- 1 file changed, 5 insertions(+), 64 deletions(-) (limited to 'src/web_frontend/transactions.rs') diff --git a/src/web_frontend/transactions.rs b/src/web_frontend/transactions.rs index f6d65af..6e5ae8c 100644 --- a/src/web_frontend/transactions.rs +++ b/src/web_frontend/transactions.rs @@ -8,14 +8,11 @@ use rocket::response::NamedFile; use std::path::{PathBuf, Path}; use rocket::request::Form; use rocket::http::RawStr; +use rocket::http::uri::Uri; use regex::Regex; use chrono::{NaiveDate, Utc}; use chrono::Datelike; -/* - This files contains the code to handle the /transactions requests -*/ - /* * This context is passed to the template rendering engine * If you modify this structure, adapt also the template to include the @@ -30,65 +27,6 @@ struct TransactionContext { date_end : String } -struct TransactionFilter { - sender_name_filter : Vec, - reference_filter : Vec -} - -/* - * files syntax: . seperates different search critereas - * a search crietera is a key value pair, with key as fiter type, e.g. - * sender name of transation. The value is what is should be and can be a regex - * key value pair is split by - - */ -fn apply_transaction_filter(filter : String, transactions : Vec) -> Vec { - let mut tmp = transactions.clone(); - // special case if for general search - if !filter.to_string().contains(".") { - println!("in special case"); - let re = Regex::new(&filter).unwrap(); - tmp = transactions.into_iter().filter(|transaction| re.is_match(&transaction.sender_name) || re.is_match(&transaction.reference) ).collect(); - return tmp; - } - - // parse filter string and construct TransactionFilter struct - let mut sender_name_vec = Vec::new(); - let mut reference_vec = Vec::new(); - for entry in filter.to_string().split(".") { - let v : Vec<_> = entry.split("-").collect(); - println!("{:?}", v); - if v.len() > 1 { - if v[0] == "sender" { - println!("Filter for sender_name"); - sender_name_vec.push(v[1].to_string()); - } - else if v[0] == "reference" { - println!("filter for reference"); - reference_vec.push(v[1].to_string()); - } - } - - } - tmp = transactions.clone().into_iter().filter(|transaction| { - let mut sender_match = false; - let mut reference_match = false; - for sender in &sender_name_vec { - let re_sender = Regex::new(&sender).unwrap(); - if re_sender.is_match(&transaction.sender_name) { - sender_match = true; - } - } - for reference in &reference_vec { - let re = Regex::new(&reference).unwrap(); - if re.is_match(&transaction.reference) { - reference_match = true; - } - } - sender_match || reference_match - }).collect(); - tmp - -} #[get("/transactions/?&&")] pub fn transaction_handler(account : &RawStr, start : Option<&RawStr>, end : Option<&RawStr>, @@ -110,6 +48,9 @@ pub fn transaction_handler(account : &RawStr, start : Option<&RawStr>, end : Opt Some(s) => s.to_string(), None => String::from("") }; + let tf_c = transaction_filter.clone(); + let test = Uri::percent_decode_lossy(tf_c.as_bytes()); + let transaction_filter = test.to_string(); 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(&account_name); @@ -130,7 +71,7 @@ pub fn transaction_handler(account : &RawStr, start : Option<&RawStr>, end : Opt } // apply filter - let ft = apply_transaction_filter(transaction_filter.clone(), t_filtered); + let ft = crate::web_frontend::util::apply_transaction_filter(transaction_filter.clone(), t_filtered); let context = TransactionContext { transactions: ft, account_name : account_name, filter : transaction_filter, date_start : date_start.to_string()[0..7].to_string(), -- cgit v1.2.3-70-g09d2