diff options
| -rw-r--r-- | src/main.rs | 1 | ||||
| -rw-r--r-- | src/web_frontend/transactions.rs | 75 | ||||
| -rw-r--r-- | templates/transaction.html.tera | 7 |
3 files changed, 27 insertions, 56 deletions
diff --git a/src/main.rs b/src/main.rs index eef92b6..c085bfc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,6 @@ fn main() { rocket::ignite() .attach(Template::fairing()) .mount("/", routes![web_frontend::transactions::transaction_handler, - web_frontend::transactions::transaction_handler_post, web_frontend::balance::balance_handler, web_frontend::static_handler, web_frontend::chart::chart_handler, web_frontend::asset::asset_handler]) .launch(); 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) diff --git a/templates/transaction.html.tera b/templates/transaction.html.tera index a1fe899..a39b7e3 100644 --- a/templates/transaction.html.tera +++ b/templates/transaction.html.tera @@ -45,19 +45,18 @@ <!-- content --> <div class="container"> <div class="md-form mt-0" style="padding-top: 20px;"> - <form name="filter" method="post" action="/transactions"> + <form name="filter" method="get" action="/transactions"> {% if filter == "" %} <input id="filter" name="filter" class="form-control" type="text" placeholder="Filter"> {% else %} <input id="filter" name="filter" class="form-control" type="text" value="{{ filter }}"> {% endif %} <input type="submit" style="visibility: hidden; display: none" /> - </form> <h6 style="padding-top:5px; visibility: hidden;" id="help" class="text-success">Help: search single rows with: sender=value; logical || and && are also possible</h6> </div> <div style="text-align:center; margin: auto; width:100%; padding: 10px;"> - <form method="get" action="/transactions"><input type="month" id="start" name="start" value="{{ date_start }}"> to <input type="month" id="end" name="end" value="{{ date_end }}"> - <input type="submit" value="Show" class="btn btn-light"> + <input type="month" id="start" name="start" value="{{ date_start }}"> to <input type="month" id="end" name="end" value="{{ date_end }}"> + <input type="submit" value="Show" class="btn btn-light"> </form> </div> <h1>Transactions <small class="text-muted">{{ account_name }}</small></h1> |
