account total

This commit is contained in:
thatscringebro 2025-12-10 12:58:45 -05:00
parent d512dedd21
commit e8b3eae889
4 changed files with 28 additions and 4 deletions

View File

@ -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> { pub fn get_accounts(con: &Connection) -> Vec<Account> {
let query = "SELECT * FROM Accounts"; let query = "SELECT * FROM Accounts";
let mut statement = con.prepare(query).unwrap(); let mut statement = con.prepare(query).unwrap();

View File

@ -1,5 +1,9 @@
use std::convert::TryFrom; use std::convert::TryFrom;
use sqlite::Connection;
use crate::data_layer;
#[derive(Clone)] #[derive(Clone)]
pub struct Account { pub struct Account {
id: i64, id: i64,
@ -24,6 +28,9 @@ impl Account {
pub fn get_ac_type(&self) -> AccountType { pub fn get_ac_type(&self) -> AccountType {
return AccountType::try_from(self.ac_type as i64).unwrap(); 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)] #[derive(Copy, Clone)]

View File

@ -31,7 +31,7 @@ impl Transaction {
date: date, date: date,
description: desc, description: desc,
type_id: type_id, 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), tr_type: data_layer::get_transaction_type(con, type_id),
} }
} }

View File

@ -1,6 +1,6 @@
mod data_layer; mod data_layer;
mod entities; mod entities;
use chrono::DateTime; use chrono::{Local, TimeZone};
use sqlite::Connection; use sqlite::Connection;
use std::io; use std::io;
@ -38,7 +38,12 @@ fn main() {
"1" => { "1" => {
let accounts = data_layer::get_accounts(&connection); let accounts = data_layer::get_accounts(&connection);
for ac in accounts.iter() { 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" => { "2" => {
@ -83,7 +88,7 @@ fn main() {
for t in trx.iter() { for t in trx.iter() {
println!( println!(
"Date: {}, Type: {}, Description: {}, Amount: {}$", "Date: {}, Type: {}, Description: {}, Amount: {}$",
t.get_date(), Local.from_utc_datetime(&t.get_date().naive_local()),
t.get_type().get_description(), t.get_type().get_description(),
t.get_desc(), t.get_desc(),
t.get_amount() t.get_amount()