account total
This commit is contained in:
parent
d512dedd21
commit
e8b3eae889
@ -77,6 +77,18 @@ pub fn get_account(con: &Connection, id: i64) -> Account {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_account_total(id: i64, con: &Connection) -> f64 {
|
||||
let query = "SELECT SUM(amount) as total FROM Transactions WHERE account_id = ?";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
statement.bind((1, id)).unwrap();
|
||||
|
||||
if let Ok(State::Row) = statement.next() {
|
||||
return statement.read::<f64, _>("total").unwrap();
|
||||
} else {
|
||||
return 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_accounts(con: &Connection) -> Vec<Account> {
|
||||
let query = "SELECT * FROM Accounts";
|
||||
let mut statement = con.prepare(query).unwrap();
|
||||
|
||||
@ -1,5 +1,9 @@
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use sqlite::Connection;
|
||||
|
||||
use crate::data_layer;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Account {
|
||||
id: i64,
|
||||
@ -24,6 +28,9 @@ impl Account {
|
||||
pub fn get_ac_type(&self) -> AccountType {
|
||||
return AccountType::try_from(self.ac_type as i64).unwrap();
|
||||
}
|
||||
pub fn get_total(&self, con: &Connection) -> f64 {
|
||||
return data_layer::get_account_total(self.id, con);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
|
||||
@ -31,7 +31,7 @@ impl Transaction {
|
||||
date: date,
|
||||
description: desc,
|
||||
type_id: type_id,
|
||||
account: data_layer::get_account(con, id),
|
||||
account: data_layer::get_account(con, ac_id),
|
||||
tr_type: data_layer::get_transaction_type(con, type_id),
|
||||
}
|
||||
}
|
||||
|
||||
11
src/main.rs
11
src/main.rs
@ -1,6 +1,6 @@
|
||||
mod data_layer;
|
||||
mod entities;
|
||||
use chrono::DateTime;
|
||||
use chrono::{Local, TimeZone};
|
||||
use sqlite::Connection;
|
||||
use std::io;
|
||||
|
||||
@ -38,7 +38,12 @@ fn main() {
|
||||
"1" => {
|
||||
let accounts = data_layer::get_accounts(&connection);
|
||||
for ac in accounts.iter() {
|
||||
println!("Name: {}", ac.get_name());
|
||||
println!(
|
||||
"Id: {}, Name: {}, Total: {}",
|
||||
ac.get_id(),
|
||||
ac.get_name(),
|
||||
ac.get_total(&connection)
|
||||
);
|
||||
}
|
||||
}
|
||||
"2" => {
|
||||
@ -83,7 +88,7 @@ fn main() {
|
||||
for t in trx.iter() {
|
||||
println!(
|
||||
"Date: {}, Type: {}, Description: {}, Amount: {}$",
|
||||
t.get_date(),
|
||||
Local.from_utc_datetime(&t.get_date().naive_local()),
|
||||
t.get_type().get_description(),
|
||||
t.get_desc(),
|
||||
t.get_amount()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user