From a9476556b100ac545d09d58f500ef209ecffcc25 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Mon, 7 Aug 2023 22:27:27 -0700 Subject: [PATCH] got first query running --- Cargo.toml | 2 ++ SQL/Tablecreation.sql | 1 + src/app.rs | 32 +++++++++++++++++++++++++++++++- src/db/connection.rs | 35 ++++++++++++++++++++++++++++++++++- src/db/transaction.rs | 4 ++-- src/main.rs | 8 ++++++-- 6 files changed, 76 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 642a2cc..244e296 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ edition = "2021" [dependencies] crossterm = "0.26.1" +futures = "0.3.28" +log = "0.4.19" ratatui = "0.21.0" simplelog = "0.12.1" sqlx = { version = "0.6.3", features = ["postgres", "runtime-tokio-native-tls", "sqlite"] } diff --git a/SQL/Tablecreation.sql b/SQL/Tablecreation.sql index 99442a4..2d3ef9e 100644 --- a/SQL/Tablecreation.sql +++ b/SQL/Tablecreation.sql @@ -59,6 +59,7 @@ CREATE TABLE rcnt.transactions ( trns_description varchar(250) NULL, trns_account int4 NOT NULL, trns_bucket int4 NULL, + trns_date Date not null, CONSTRAINT transactions_pkey PRIMARY KEY (trns_id) ); diff --git a/src/app.rs b/src/app.rs index ee6c33d..d6b2a95 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,10 +1,18 @@ +use std::sync::Mutex; +use std::sync::Arc; + use crossterm::event::{Event, self, KeyCode}; +use tokio; use ratatui::widgets::ListState; +use log::warn; + +use crate::db::DB; use crate::uis::history::HistoryState; use crate::uis::new_transaction::NewTransactionTabState; use crate::uis::navigation_frame::NavigationState; +use crate::db::transaction::TransactionRecord; pub type AppResult = std::result::Result>; @@ -65,14 +73,19 @@ pub struct App<'a> { pub states: States<'a>, pub input_mode: InputMode, + + pub db: Arc>, + pub records: Arc>>, } impl<'a> App<'a> { - pub fn new() -> App<'a> { + pub fn new(db: DB, records: Arc>>) -> App<'a> { App { running: true, states: States::new(), input_mode: InputMode::Normal, + db: Arc::new(tokio::sync::Mutex::new(db)), + records, } } @@ -101,6 +114,9 @@ impl<'a> App<'a> { } } } + if let KeyCode::Char('r') = key.code { + self.refresh(); + } match self.states.active_frame { ActiveFrame::Navigation => { NavigationState::handle_event(key, self); @@ -115,6 +131,20 @@ impl<'a> App<'a> { } return Ok(()) } + + pub fn refresh(&mut self) { + + let fut = Arc::clone(&self.db); + + tokio::spawn(async move { + let res = fut.lock().await.get_all_records().await; + match res { + Ok(_) => {}, + Err(e) => warn!("{}", e) + } + }); + + } } pub enum ActiveFrame { diff --git a/src/db/connection.rs b/src/db/connection.rs index 934079d..cb6b789 100644 --- a/src/db/connection.rs +++ b/src/db/connection.rs @@ -1,20 +1,53 @@ +use std::sync::Mutex; +use std::sync::Arc; + +use futures::TryStreamExt; + +use sqlx::Error; use sqlx::PgPool; +use sqlx::Row; use sqlx::postgres::PgPoolOptions; +use crate::db::transaction::TransactionRecord; + pub struct DB { conn_pool: PgPool, + records: Arc>>, } impl DB { - pub async fn new() -> Result { + pub async fn new(records: Arc>>) -> 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, + records, }) } + + pub async fn get_all_records(&mut self) -> Result<(), Error> { + let mut rows = sqlx::query("SELECT trns_id, trns_amount, trns_description, trns_account, trns_bucket trns_date FROM rcnt.transactions") + .fetch(&self.conn_pool); + + while let Some(row) = rows.try_next().await? { + let id: i64 = row.try_get("trns_id")?; + let amount: &str = row.try_get("trns_amount")?; + let date: &str = row.try_get("trns_date")?; + self.records.lock().unwrap().push( + TransactionRecord{ + id: id.into(), + amount: amount.to_string(), + date: date.to_string() + } + ); + } + + return Ok(()); + + + } } diff --git a/src/db/transaction.rs b/src/db/transaction.rs index 0873b3d..511494f 100644 --- a/src/db/transaction.rs +++ b/src/db/transaction.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; // cargo add crust_decimal pub struct TransactionRecord { - pub id: usize, + pub id: i64, // pub amount: Decimal, pub amount: String, //pub record_date: Date, @@ -11,7 +11,7 @@ pub struct TransactionRecord { } impl TransactionRecord { - pub fn new(id: usize, amount: String, record_date: String) -> TransactionRecord { + pub fn new(id: i64, amount: String, record_date: String) -> TransactionRecord { TransactionRecord { id, amount, diff --git a/src/main.rs b/src/main.rs index 9aa32fd..b519503 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ use std::io; use std::fs::File; use std::time::Duration; +use std::sync::Mutex; +use std::sync::Arc; use simplelog::*; @@ -12,6 +14,7 @@ use ratatui::{ use recount::app::{App, AppResult}; use recount::tui::Tui; use recount::db::DB; +use recount::db::transaction::TransactionRecord; #[tokio::main] async fn main() -> AppResult<()> { @@ -19,10 +22,11 @@ async fn main() -> AppResult<()> { let log_file = "testing_log.txt".to_string(); init_logger(log_file); - let db = DB::new().await?; + let records = Arc::new(Mutex::new(Vec::new())); + let db = DB::new(Arc::clone(&records)).await?; // Create an application. - let mut app = App::new(); + let mut app = App::new(db, records); // Initialize the terminal user interface. let backend = CrosstermBackend::new(io::stderr());