cleaned up sql

This commit is contained in:
Nickiel12 2023-08-07 21:01:13 -07:00
parent b4380f2125
commit 2ad9dd4f71
10 changed files with 246 additions and 5 deletions

View file

@ -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"] }

94
SQL/Tablecreation.sql Normal file
View file

@ -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;

View file

@ -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', '<Random Bible Verse About Tithes>');
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);

34
SQL/Views setup.sql Normal file
View file

@ -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

View file

@ -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 => {

20
src/db/connection.rs Normal file
View file

@ -0,0 +1,20 @@
use sqlx::PgPool;
use sqlx::postgres::PgPoolOptions;
pub struct DB {
conn_pool: PgPool,
}
impl DB {
pub async fn new() -> Result<DB, sqlx::Error> {
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,
})
}
}

View file

@ -1,3 +1,6 @@
pub mod transaction;
pub use self::transaction::TransactionRecord;
pub mod connection;
pub use self::connection::DB;

View file

@ -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();
}

View file

@ -1,5 +1,3 @@
pub mod history;
pub use self::history::render_history_tab;

View file

@ -105,8 +105,8 @@ pub fn render_navigation_frame<B: Backend> (f: &mut Frame<B>, 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()