diff --git a/src/main.rs b/src/main.rs index 151daad..521a045 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,11 @@ mod data_layer; mod entities; use sqlite::Connection; +use std::io; -use crate::entities::Account; +use crate::entities::{Account, TransactionType}; fn main() { - println!("Hello, world!"); - let connection = match Connection::open("ft_rs.db") { Ok(con) => con, Err(e) => { @@ -16,10 +15,62 @@ fn main() { }; data_layer::setup(&connection); - let mut account = Account::new(0, "test".to_string(), entities::AccountType::Cash); - account = data_layer::upsert_account(&connection, account); - let accounts = data_layer::get_accounts(&connection); - for ac in accounts.iter() { - println!("name: {}", ac.get_name()) + + loop { + println!("Please choose an option:"); + println!("1. List accounts"); + println!("2. Add an account"); + println!("3. List transaction types"); + println!("4. Add a transaction type"); + println!("0. Exit"); + + let mut choice = String::new(); + io::stdin() + .read_line(&mut choice) + .expect("Failed to read line"); + + let choice = choice.trim(); + + match choice { + "1" => { + let accounts = data_layer::get_accounts(&connection); + for ac in accounts.iter() { + println!("Name: {}", ac.get_name()); + } + } + "2" => { + println!("Please enter the account name:"); + let mut ac_name = String::new(); + io::stdin() + .read_line(&mut ac_name) + .expect("Failed to read line"); + let ac_name = ac_name.trim(); + let new_ac = Account::new(0, ac_name.to_string(), entities::AccountType::Cash); + data_layer::upsert_account(&connection, new_ac); + } + "3" => { + let types = data_layer::get_transaction_types(&connection); + for t in types.iter() { + println!("Name: {}", t.get_description()); + } + } + "4" => { + println!("Please enter the transaction type:"); + let mut desc = String::new(); + io::stdin() + .read_line(&mut desc) + .expect("Failed to read line"); + let desc = desc.trim(); + let tr_type = TransactionType::new(0, desc.to_string()); + data_layer::upsert_transaction_type(&connection, tr_type); + } + "0" => { + println!("Exiting..."); + break; // Exit the loop + } + _ => { + println!("Invalid choice. Please enter 1, 2, or 3."); + } + } } }