summaryrefslogtreecommitdiff
path: root/src/web_frontend/asset.rs
blob: f69f184dd909669581b43d34b0d544798425e0b7 (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
use std::collections::HashMap;
use rocket_contrib::templates::Template;
use crate::parsers::ini::IniFile;
use crate::parsers::csv::CsvFile;


#[derive(Serialize)]
pub struct AssetContext {
	accounts : Vec<Account>
}

#[derive(Serialize)]
pub struct Account {
	name : String,
	category : String,
	balance : f32
}

#[get("/asset")]
pub fn asset_handler() -> rocket_contrib::templates::Template {
	let asset_file = "data/asset.ini";
	let asset_config = IniFile::from_file(asset_file);
	let ini_file;
	match asset_config {
		Ok(file) => ini_file = file,
		Err(e) => panic!("could not read asset file")
	}

	let mut acc = Vec::new();

	for (account_name, config) in ini_file.sections {
		let mut category = String::from("");
		if let Some(cat) = config.get("Category") {
			if let Some(c) = cat.get(0) {
				category = c.to_string();
			}
		}
		let mut all_trans = Vec::new();
		if let Some(transaction_files) = config.get("TransactionFile") {
			for file in transaction_files {
				let transactions = CsvFile::from_file(file, ";", true);
				let mut t : Vec<crate::banking::account::Transaction> ; 
				match transactions {
				    Ok(trans) => t = crate::banking::account::Transaction::from_sparkasse_csv_file(trans),
				    Err(e) => panic!("could not read file {:?}", e)
				}
				all_trans.append(& mut t);
			}
		}
		let balance = all_trans.iter().fold(0.0, |acc, x| acc + x.amount).abs();
		let tmp = Account {
			name : account_name,
			category : category.to_string(),
			balance : balance
		};
		acc.push(tmp);
	}

	let context = AssetContext { accounts : acc };
	Template::render("asset", context)
}