From 2ad9dd4f710f7372081bd32e2fdc858edfcadb51 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Mon, 7 Aug 2023 21:01:13 -0700 Subject: [PATCH] cleaned up sql --- Cargo.toml | 2 + SQL/Tablecreation.sql | 94 +++++++++++++++++++++++++++++++++++++ SQL/Test Data Poplating.sql | 50 ++++++++++++++++++++ SQL/Views setup.sql | 34 ++++++++++++++ src/app.rs | 10 ++++ src/db/connection.rs | 20 ++++++++ src/db/mod.rs | 3 ++ src/main.rs | 32 ++++++++++++- src/uis/mod.rs | 2 - src/uis/navigation_frame.rs | 4 +- 10 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 SQL/Tablecreation.sql create mode 100644 SQL/Test Data Poplating.sql create mode 100644 SQL/Views setup.sql create mode 100644 src/db/connection.rs diff --git a/Cargo.toml b/Cargo.toml index 556d941..642a2cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,4 +8,6 @@ edition = "2021" [dependencies] crossterm = "0.26.1" ratatui = "0.21.0" +simplelog = "0.12.1" sqlx = { version = "0.6.3", features = ["postgres", "runtime-tokio-native-tls", "sqlite"] } +tokio = { version = "1.28.2", features = ["full"] } diff --git a/SQL/Tablecreation.sql b/SQL/Tablecreation.sql new file mode 100644 index 0000000..99442a4 --- /dev/null +++ b/SQL/Tablecreation.sql @@ -0,0 +1,94 @@ +-- rcnt.accounts definition + +-- Drop table + +-- DROP TABLE rcnt.accounts; + +CREATE TABLE rcnt.accounts ( + acnt_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, + acnt_dsply_name varchar(50) NOT NULL, + acnt_description varchar(250) NULL, + CONSTRAINT accounts_pkey PRIMARY KEY (acnt_id) +); + + + +-- rcnt.buckets definition + +-- Drop table + +-- DROP TABLE rcnt.buckets; + +CREATE TABLE rcnt.buckets ( + bkt_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, + bkt_dsply_code varchar(5) NOT NULL, + bkt_dsply_name varchar(50) NULL, + bkt_description varchar(250) NULL, + CONSTRAINT buckets_pkey PRIMARY KEY (bkt_id) +); + + + +-- rcnt.transaction_breakdown definition + +-- Drop table + +--DROP TABLE rcnt.transaction_breakdown; + +CREATE TABLE rcnt.transaction_breakdown ( + trns_brkdwn_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, + trns_brkdwn_amount numeric(10, 3) NOT NULL, + trns_brkdwn_parent_transaction int4 NOT NULL, + trns_brkdwn_catagory int4 NULL, + trns_brkdwn_bucket int4 NULL, + CONSTRAINT transaction_breakdown_pkey PRIMARY KEY (trns_brkdwn_id) +); + + + + +-- rcnt.transactions definition + +-- Drop table + +-- DROP TABLE rcnt.transactions; + +CREATE TABLE rcnt.transactions ( + trns_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, + trns_amount numeric(10, 3) NOT NULL, + trns_description varchar(250) NULL, + trns_account int4 NOT NULL, + trns_bucket int4 NULL, + CONSTRAINT transactions_pkey PRIMARY KEY (trns_id) +); + +ALTER TABLE rcnt.transactions ADD CONSTRAINT transactions_trns_account_fkey FOREIGN KEY (trns_account) REFERENCES rcnt.accounts(acnt_id); +ALTER TABLE rcnt.transactions ADD CONSTRAINT transactions_trns_bucket_fkey FOREIGN KEY (trns_bucket) REFERENCES rcnt.buckets(bkt_id) ON DELETE SET NULL; + + +-- rcnt.transaction_categories definition + +-- Drop table + +-- DROP TABLE rcnt.transaction_categories; + +CREATE TABLE rcnt.transaction_categories ( + trns_ctgry_id int4 NOT NULL GENERATED ALWAYS AS IDENTITY, + trns_ctgry_dsply_code varchar(5) NOT NULL, + trns_ctgry_dsply_name varchar(50) NOT NULL, + trns_ctgry_description varchar(250) NULL, + CONSTRAINT transaction_categories_pkey PRIMARY KEY (trns_ctgry_id) +); + +-- rcnt.transaction_breakdown foreign keys + +ALTER TABLE rcnt.transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_catagory_fkey FOREIGN KEY (trns_brkdwn_catagory) REFERENCES rcnt.transaction_categories(trns_ctgry_id) ON DELETE SET NULL; +ALTER TABLE rcnt.transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_parent_transaction_fkey FOREIGN KEY (trns_brkdwn_parent_transaction) REFERENCES rcnt.transactions(trns_id) ON DELETE CASCADE; +ALTER TABLE rcnt.transaction_breakdown ADD CONSTRAINT transaction_breakdown_trns_brkdwn_bucket_fkey FOREIGN KEY (trns_brkdwn_bucket) REFERENCES rcnt.buckets(bkt_id) ON DELETE SET NULL; + + + + + + + diff --git a/SQL/Test Data Poplating.sql b/SQL/Test Data Poplating.sql new file mode 100644 index 0000000..b111950 --- /dev/null +++ b/SQL/Test Data Poplating.sql @@ -0,0 +1,50 @@ + +INSERT INTO rcnt.accounts +(display_name, description) +VALUES('BECU Checking', 'BECU Checking Account'); + + +INSERT INTO rcnt.buckets +(display_code, display_name, description) +VALUES('SVNGS', 'Savings', 'Long term savings'); + + +INSERT INTO rcnt.buckets +(display_code, display_name, description) +VALUES('SPEND', 'Spending', 'General Spening Category'); + + +INSERT INTO rcnt.buckets +(display_code, display_name, description) +VALUES('TITHE', 'Tight', ''); + + + + +INSERT INTO rcnt.transactioncategories +(display_code, display_name, description) +VALUES('GAS', 'Gas', 'Buying gas'); + + +INSERT INTO rcnt.transactioncategories +(display_code, display_name, description) +VALUES('TAX', 'Taxes', 'One of the only constants'); + + +INSERT INTO rcnt.transactions +(amount, description, account, transaction_bucket) +VALUES(10.00, 'Optional Text', 1, 1); + + + +INSERT INTO rcnt.transactionbreakdown +(amount, parent_transaction, catagory, transaction_bucket) +VALUES(8.00, 1, 1, 1); + + +INSERT INTO rcnt.transactionbreakdown +(amount, parent_transaction, catagory, transaction_bucket) +VALUES(2.00, 1, 1, 2); + + + diff --git a/SQL/Views setup.sql b/SQL/Views setup.sql new file mode 100644 index 0000000..bbc5cf2 --- /dev/null +++ b/SQL/Views setup.sql @@ -0,0 +1,34 @@ + +create or replace view rcnt.transaction_history_view as + select + t.trns_id, + t.trns_amount, + t.trns_description, + b.bkt_id, + b.bkt_dsply_code, + b.bkt_dsply_name, + a.acnt_id, + a.acnt_dsply_name + from rcnt.transactions t + left join rcnt.buckets b on b.bkt_id = t.trns_bucket + left join rcnt.accounts a on a.acnt_id = t.trns_account + +--drop view rcnt.transaction_history_view + +select * from rcnt.transaction_history_view + +create or replace view rcnt.transaction_detail_history_view as + select + thv.trns_id, + thv.trns_amount, + thv.acnt_dsply_name, + t.trns_brkdwn_amount as breakdown_amount, + b.bkt_dsply_code, + b.bkt_dsply_name, + tc.trns_ctgry_dsply_code, + tc.trns_ctgry_dsply_name + from rcnt.transaction_history_view thv + left join rcnt.transaction_breakdown t on thv.trns_id = t.trns_brkdwn_parent_transaction + left join rcnt.transaction_categories tc on tc.trns_ctgry_id = t.trns_brkdwn_catagory + left join rcnt.buckets b on b.bkt_id = t.trns_brkdwn_bucket + diff --git a/src/app.rs b/src/app.rs index b135dfd..ee6c33d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -90,6 +90,16 @@ impl<'a> App<'a> { self.states.active_frame = ActiveFrame::Navigation; } } + } else if let KeyCode::Esc = key.code { + if let Some(_) = self.states.nav_state.message.clone() { + self.states.nav_state.message = None; + } else { + if let ActiveFrame::Navigation = self.states.active_frame { + self.running = false; + } else { + self.states.active_frame = ActiveFrame::Navigation; + } + } } match self.states.active_frame { ActiveFrame::Navigation => { diff --git a/src/db/connection.rs b/src/db/connection.rs new file mode 100644 index 0000000..934079d --- /dev/null +++ b/src/db/connection.rs @@ -0,0 +1,20 @@ + +use sqlx::PgPool; +use sqlx::postgres::PgPoolOptions; + +pub struct DB { + conn_pool: PgPool, + +} + + +impl DB { + pub async fn new() -> Result { + let connection_pool = PgPoolOptions::new() + .max_connections(3) + .connect("postgres://rcntuser:Devel@pmentPa$$w0rd@10.0.0.183/Borealis").await?; + Ok(DB { + conn_pool: connection_pool, + }) + } +} diff --git a/src/db/mod.rs b/src/db/mod.rs index b70c519..f59cbac 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,3 +1,6 @@ pub mod transaction; pub use self::transaction::TransactionRecord; + +pub mod connection; +pub use self::connection::DB; diff --git a/src/main.rs b/src/main.rs index 49e5c71..9aa32fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ use std::io; +use std::fs::File; use std::time::Duration; +use simplelog::*; + use ratatui::{ backend::CrosstermBackend, Terminal, @@ -8,8 +11,16 @@ use ratatui::{ use recount::app::{App, AppResult}; use recount::tui::Tui; +use recount::db::DB; + +#[tokio::main] +async fn main() -> AppResult<()> { + + let log_file = "testing_log.txt".to_string(); + init_logger(log_file); + + let db = DB::new().await?; -fn main() -> AppResult<()> { // Create an application. let mut app = App::new(); @@ -34,3 +45,22 @@ fn main() -> AppResult<()> { // restore terminal tui.exit() } + + +pub fn init_logger(output_file: String) { + // TODO: configure the log levels to something appropriate + CombinedLogger::init(vec![ + TermLogger::new( + LevelFilter::Info, + Config::default(), + TerminalMode::Mixed, + ColorChoice::Auto, + ), + WriteLogger::new( + LevelFilter::Info, + Config::default(), + File::create(output_file).unwrap(), + ), + ]) + .unwrap(); +} diff --git a/src/uis/mod.rs b/src/uis/mod.rs index 79c4520..0727dad 100644 --- a/src/uis/mod.rs +++ b/src/uis/mod.rs @@ -1,5 +1,3 @@ - - pub mod history; pub use self::history::render_history_tab; diff --git a/src/uis/navigation_frame.rs b/src/uis/navigation_frame.rs index d406bb3..225de46 100644 --- a/src/uis/navigation_frame.rs +++ b/src/uis/navigation_frame.rs @@ -105,8 +105,8 @@ pub fn render_navigation_frame (f: &mut Frame, status_rect: Rect, let navbar = { if let None = app.states.nav_state.message { match app.states.active_frame { - ActiveFrame::Navigation => "Navigating: `q` to exit".to_string(), - _ => "Editing Body: 'q' to go back to navigating".to_string(), + ActiveFrame::Navigation => "Navigating: q/Esc to exit".to_string(), + _ => "Editing Body: q/Esc to go back to navigating".to_string(), } } else { app.states.nav_state.message.clone().unwrap()