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 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 * changes! */ #[derive(Serialize)] struct TransactionContext { transactions : Vec, account_name : String, filter : String, date_start : String, date_end : String } 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 } #[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"); 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 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; // 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) }