summaryrefslogtreecommitdiff
path: root/src/web_frontend/transactions.rs
blob: 417ca967b1f2a52070fbda3de0181aaba2518390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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<crate::banking::Transaction>,
	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<crate::banking::Transaction>) -> Vec<crate::banking::Transaction> {
	    let transactions = CsvFile::from_file("data/t.csv", ";", true);
	    let t : Vec<crate::banking::Transaction> ; 
	    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::<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

	}
}

#[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 {
	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?<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 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::Transaction> ; 
    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)
}