adding and listing transactions baby
This commit is contained in:
69
src/main.rs
69
src/main.rs
@@ -1,9 +1,10 @@
|
||||
mod data_layer;
|
||||
mod entities;
|
||||
use chrono::DateTime;
|
||||
use sqlite::Connection;
|
||||
use std::io;
|
||||
|
||||
use crate::entities::{Account, TransactionType};
|
||||
use crate::entities::{Account, Transaction, TransactionType};
|
||||
|
||||
fn main() {
|
||||
let connection = match Connection::open("ft_rs.db") {
|
||||
@@ -22,6 +23,8 @@ fn main() {
|
||||
println!("2. Add an account");
|
||||
println!("3. List transaction types");
|
||||
println!("4. Add a transaction type");
|
||||
println!("5. List transactions");
|
||||
println!("6. Add a transaction");
|
||||
println!("0. Exit");
|
||||
|
||||
let mut choice = String::new();
|
||||
@@ -64,12 +67,74 @@ fn main() {
|
||||
let tr_type = TransactionType::new(0, desc.to_string());
|
||||
data_layer::upsert_transaction_type(&connection, tr_type);
|
||||
}
|
||||
"5" => {
|
||||
println!("Please enter the account id:");
|
||||
let mut ac_id_str = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut ac_id_str)
|
||||
.expect("Failed to read line");
|
||||
ac_id_str = ac_id_str.trim().to_string();
|
||||
let ac_id = ac_id_str.parse::<i64>().unwrap();
|
||||
let trx: Vec<entities::Transaction> =
|
||||
data_layer::get_account_transactions(&connection, ac_id);
|
||||
// .sort_by(|a, b| b.get_date().cmp(&a.get_date()))
|
||||
// .try_into()
|
||||
// .unwrap();
|
||||
for t in trx.iter() {
|
||||
println!(
|
||||
"Date: {}, Type: {}, Description: {}, Amount: {}$",
|
||||
t.get_date(),
|
||||
t.get_type().get_description(),
|
||||
t.get_desc(),
|
||||
t.get_amount()
|
||||
);
|
||||
}
|
||||
}
|
||||
"6" => {
|
||||
println!("Please enter the account id:");
|
||||
let mut ac_id_str = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut ac_id_str)
|
||||
.expect("Failed to read line");
|
||||
ac_id_str = ac_id_str.trim().to_string();
|
||||
let ac_id = ac_id_str.parse::<i64>().unwrap();
|
||||
println!("Please enter the transaction type id:");
|
||||
let mut tr_type_id_str = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut tr_type_id_str)
|
||||
.expect("Failed to read line");
|
||||
tr_type_id_str = tr_type_id_str.trim().to_string();
|
||||
let type_id = tr_type_id_str.parse::<i64>().unwrap();
|
||||
println!("Please enter the amount:");
|
||||
let mut amnt_str = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut amnt_str)
|
||||
.expect("Failed to read line");
|
||||
amnt_str = amnt_str.trim().to_string();
|
||||
let amount = amnt_str.parse::<f64>().unwrap();
|
||||
println!("Please enter the description:");
|
||||
let mut desc = String::new();
|
||||
io::stdin()
|
||||
.read_line(&mut desc)
|
||||
.expect("Failed to read line");
|
||||
desc = desc.trim().to_string();
|
||||
let tr = Transaction::new(
|
||||
0,
|
||||
ac_id,
|
||||
amount,
|
||||
chrono::offset::Utc::now(),
|
||||
desc,
|
||||
type_id,
|
||||
&connection,
|
||||
);
|
||||
data_layer::upsert_transaction(&connection, tr);
|
||||
}
|
||||
"0" => {
|
||||
println!("Exiting...");
|
||||
break; // Exit the loop
|
||||
}
|
||||
_ => {
|
||||
println!("Invalid choice. Please enter 1, 2, or 3.");
|
||||
println!("Invalid choice.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user