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 0000000..f76c74d Binary files /dev/null and b/src/ft_rs.db differ diff --git a/src/main.rs b/src/main.rs index e7a11a9..7b55bdd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,21 @@ +mod data_layer; +mod entities; +use sqlite::Connection; + fn main() { println!("Hello, world!"); + + let connection = match Connection::open("ft_rs.db") { + Ok(con) => 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) + } }