From e8b3eae8891410cbbf7c1c7bf75bee0c46c99640 Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Wed, 10 Dec 2025 12:58:45 -0500 Subject: [PATCH] account total --- src/data_layer.rs | 12 ++++++++++++ src/entities/account.rs | 7 +++++++ src/entities/transaction.rs | 2 +- src/main.rs | 11 ++++++++--- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/data_layer.rs b/src/data_layer.rs index 52b5d6e..1d9b59d 100644 --- a/src/data_layer.rs +++ b/src/data_layer.rs @@ -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::("total").unwrap(); + } else { + return 0.0; + } +} + pub fn get_accounts(con: &Connection) -> Vec { let query = "SELECT * FROM Accounts"; let mut statement = con.prepare(query).unwrap(); diff --git a/src/entities/account.rs b/src/entities/account.rs index c4a0ab9..f212dc2 100644 --- a/src/entities/account.rs +++ b/src/entities/account.rs @@ -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)] diff --git a/src/entities/transaction.rs b/src/entities/transaction.rs index f8c6f06..0b79998 100644 --- a/src/entities/transaction.rs +++ b/src/entities/transaction.rs @@ -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), } } diff --git a/src/main.rs b/src/main.rs index 5955fd4..4babf35 100644 --- a/src/main.rs +++ b/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()