From d5e34d153469fb96bc7875ac0a3171bd4b751803 Mon Sep 17 00:00:00 2001 From: thatscringebro Date: Fri, 17 Oct 2025 08:49:29 -0400 Subject: [PATCH] started sqlite logic --- Cargo.toml | 2 ++ src/data_layer.rs | 44 ++++++++++++++++++++++++++++++++++++ src/entities.rs | 5 ++++ src/entities/account.rs | 34 ++++++++++++++++++++++++++++ src/entities/transaction.rs | 19 ++++++++++++++++ src/ft_rs.db | Bin 0 -> 20480 bytes src/main.rs | 18 +++++++++++++++ 7 files changed, 122 insertions(+) create mode 100644 src/data_layer.rs create mode 100644 src/entities.rs create mode 100644 src/entities/account.rs create mode 100644 src/entities/transaction.rs create mode 100644 src/ft_rs.db diff --git a/Cargo.toml b/Cargo.toml index c8a2ae1..5451aaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2024" [dependencies] +chrono = "0.4.42" +sqlite = "0.37.0" diff --git a/src/data_layer.rs b/src/data_layer.rs new file mode 100644 index 0000000..750c0a3 --- /dev/null +++ b/src/data_layer.rs @@ -0,0 +1,44 @@ +use sqlite::{Connection, State}; +use crate::entities::*; + +pub fn setup(con: &Connection) { + let _ = con.execute( + " + CREATE TABLE IF NOT EXISTS Accounts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + type INTEGER NOT NULL + ); + + CREATE TABLE IF NOT EXISTS TransactionTypes ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + description TEXT NOT NULL + ); + + CREATE TABLE IF NOT EXISTS Transactions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + account_id INTEGER NOT NULL, + type_id INTEGER NOT NULL, + amount FLOAT NOT NULL, + date DATE NOT NULL, + description TEXT, + FOREIGN KEY(account_id) REFERENCES Accounts(id), + FOREIGN KEY(type_id) REFERENCES TransactionTypes(id) + ); + "); + +} + +//pub fn + +pub fn get_accounts(con: &Connection) -> Vec { + let query = "SELECT * FROM Accounts"; + let mut statement = con.prepare(query).unwrap(); + let mut vec = Vec::::new(); + + while let Ok(State::Row) = statement.next() { + vec.push(Account::new(statement.read::("id").unwrap(), statement.read::("name").unwrap(), statement.read::("type").unwrap().try_into().unwrap())); + } + + return vec; +} diff --git a/src/entities.rs b/src/entities.rs new file mode 100644 index 0000000..3f0f1ba --- /dev/null +++ b/src/entities.rs @@ -0,0 +1,5 @@ +pub mod account; +pub mod transaction; + +pub use account::{Account, AccountType}; +pub use transaction::{Transaction, TransactionType}; diff --git a/src/entities/account.rs b/src/entities/account.rs new file mode 100644 index 0000000..d81e0e1 --- /dev/null +++ b/src/entities/account.rs @@ -0,0 +1,34 @@ +use std::convert::TryFrom; +pub struct Account { + id: i64, + pub name: String, + ac_type: AccountType, +} + +impl Account{ + pub fn new(id: i64, name: String, ac_type: AccountType) -> Self { + Account { + id: id, + name: name, + ac_type: ac_type, + } + } +} + +pub enum AccountType{ + Cash, + Assets, +} + + +impl TryFrom for AccountType { + type Error = (); + + fn try_from(v: i64) -> Result { + match v { + x if x == AccountType::Cash as i64 => Ok(AccountType::Cash), + x if x == AccountType::Assets as i64 => Ok(AccountType::Assets), + _ => Ok(AccountType::Cash), + } + } +} diff --git a/src/entities/transaction.rs b/src/entities/transaction.rs new file mode 100644 index 0000000..9b45ab8 --- /dev/null +++ b/src/entities/transaction.rs @@ -0,0 +1,19 @@ +use chrono::{DateTime, Utc}; +use crate::entities::Account; + +pub struct Transaction { + id: i32, + account_id: i32, + amount: f64, + date: DateTime, + description: String, + type_id: i32, + + account: Account, + tr_type: TransactionType, +} + +pub struct TransactionType{ + id: i32, + description: String, +} diff --git a/src/ft_rs.db b/src/ft_rs.db new file mode 100644 index 0000000000000000000000000000000000000000..f76c74d30910624cd008e438823f707258598345 GIT binary patch literal 20480 zcmeI#F;Ck-6u@ztghoP?cFTlx3u-Jvmu~G+0~Zw4IMf_!qYjblxRNE}Kx~GNP>Ju_ zZ_%^C!FHOe1C#YXDe*bJd%E{~&hXAZJ5d^nAM?e`PsKaqwPBjZMOiN`RsEG+eh|!W;&f8l?T(aiFC1rear*Nr{J)j{EQ=}z&dAom)&`vVy1_cm zQvAo7FbNjXRfZ#!{Q8$11|v@nPuwis-cvr7@Z>;x(mj&r!hT3I=|xj(HLr4j#~drW zOY%w+ con, + Err(e) => { + eprintln!("Error opening database: {}", e); + return; + } + }; + + data_layer::setup(&connection); + let accounts = data_layer::get_accounts(&connection); + for ac in accounts.iter() { + println!("name: {}", ac.name) + } }