use parsers::csv::CsvFile; use crate::banking::account::Account; use crate::banking::asset::Asset; //use parsers::ini::IniFile; use std::collections::HashMap; use rocket_contrib::templates::Template; 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 context is passed to the template rendering engine * If you modify this structure, adapt also the template to include the * changes! */ #[derive(Serialize)] struct TransactionContext<'t> { transactions : Vec<&'t crate::banking::account::Transaction>, account_name : String, filter : String, date_start : String, date_end : String } #[get("/transactions/?&&")] pub fn transaction_handler(account : &RawStr, start : Option<&RawStr>, end : Option<&RawStr>, filter : Option<&RawStr>) -> rocket_contrib::templates::Template { let account_name = account.to_string(); 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 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 transaction_filter = match filter { Some(s) => s.to_string(), None => String::from("") }; let tf_c = transaction_filter.clone(); let filter_html_decoded = Uri::percent_decode_lossy(tf_c.as_bytes()); let transaction_filter = filter_html_decoded.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); let acc; match transactions { Some(trans) => acc = trans, None => panic!("could not read file") } let t = acc.transactions; // apply parameters // apply date filters let t_filtered = crate::web_frontend::util::apply_date_filter(t.clone(), date_start, date_end); // apply filter let ft = crate::web_frontend::util::apply_transaction_filter(&transaction_filter, &t_filtered); let context = TransactionContext { transactions: ft, account_name : account_name, filter : transaction_filter, date_start : date_start.to_string()[0..7].to_string(), date_end : date_end.to_string()[0..7].to_string()}; Template::render("transaction", context) }