use parsers::csv::CsvFile; use crate::banking::Account; //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 } #[derive(FromForm)] pub struct TransactionFilter { filter : String } impl TransactionFilter { pub fn apply_filter(&self, transactions : Vec) -> Vec { let transactions = CsvFile::from_file("data/t.csv", ";", true); let t : Vec ; match transactions { Ok(trans) => t = crate::banking::Transaction::from_sparkasse_csv_file(trans), Err(e) => panic!("could not read file {:?}", e) } //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 } } #[post("/transactions?&", data= "")] pub fn transaction_handler_post(start : Option<&RawStr>, end : Option<&RawStr>, transaction_filter : Form) -> 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 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 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 ; match transactions { Ok(trans) => t = crate::banking::Transaction::from_sparkasse_csv_file(trans), Err(e) => panic!("could not read file {:?}", e) } let context = TransactionContext { transactions: t, 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) }