From 04c9461ef53f5eaa8e7d72442e211d91546b88e3 Mon Sep 17 00:00:00 2001 From: Benedict Börger Date: Wed, 26 Jun 2019 23:54:49 +0200 Subject: [web_fronted][asset] use new iterator over Asset --- src/web_frontend/asset.rs | 37 ++++++++----------------------------- 1 file changed, 8 insertions(+), 29 deletions(-) (limited to 'src/web_frontend') diff --git a/src/web_frontend/asset.rs b/src/web_frontend/asset.rs index f69f184..58960f9 100644 --- a/src/web_frontend/asset.rs +++ b/src/web_frontend/asset.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use rocket_contrib::templates::Template; use crate::parsers::ini::IniFile; use crate::parsers::csv::CsvFile; +use chrono::Utc; #[derive(Serialize)] @@ -19,39 +20,17 @@ pub struct Account { #[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 asset = crate::banking::asset::Asset::from_ini_file(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 ; - 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 today = Utc::today().naive_utc(); + + for account in asset.iter() { let tmp = Account { - name : account_name, - category : category.to_string(), - balance : balance + name : account.name.to_string(), + category : account.category.to_string(), + balance : account.get_balance(today) }; acc.push(tmp); } -- cgit v1.2.3-70-g09d2