started sqlite logic
This commit is contained in:
parent
120cc3e583
commit
d5e34d1534
@ -4,3 +4,5 @@ version = "0.1.0"
|
|||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
chrono = "0.4.42"
|
||||||
|
sqlite = "0.37.0"
|
||||||
|
|||||||
44
src/data_layer.rs
Normal file
44
src/data_layer.rs
Normal file
@ -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<Account> {
|
||||||
|
let query = "SELECT * FROM Accounts";
|
||||||
|
let mut statement = con.prepare(query).unwrap();
|
||||||
|
let mut vec = Vec::<Account>::new();
|
||||||
|
|
||||||
|
while let Ok(State::Row) = statement.next() {
|
||||||
|
vec.push(Account::new(statement.read::<i64, _>("id").unwrap(), statement.read::<String, _>("name").unwrap(), statement.read::<i64, _>("type").unwrap().try_into().unwrap()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return vec;
|
||||||
|
}
|
||||||
5
src/entities.rs
Normal file
5
src/entities.rs
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
pub mod account;
|
||||||
|
pub mod transaction;
|
||||||
|
|
||||||
|
pub use account::{Account, AccountType};
|
||||||
|
pub use transaction::{Transaction, TransactionType};
|
||||||
34
src/entities/account.rs
Normal file
34
src/entities/account.rs
Normal file
@ -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<i64> for AccountType {
|
||||||
|
type Error = ();
|
||||||
|
|
||||||
|
fn try_from(v: i64) -> Result<Self, Self::Error> {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/entities/transaction.rs
Normal file
19
src/entities/transaction.rs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use crate::entities::Account;
|
||||||
|
|
||||||
|
pub struct Transaction {
|
||||||
|
id: i32,
|
||||||
|
account_id: i32,
|
||||||
|
amount: f64,
|
||||||
|
date: DateTime<Utc>,
|
||||||
|
description: String,
|
||||||
|
type_id: i32,
|
||||||
|
|
||||||
|
account: Account,
|
||||||
|
tr_type: TransactionType,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TransactionType{
|
||||||
|
id: i32,
|
||||||
|
description: String,
|
||||||
|
}
|
||||||
BIN
src/ft_rs.db
Normal file
BIN
src/ft_rs.db
Normal file
Binary file not shown.
18
src/main.rs
18
src/main.rs
@ -1,3 +1,21 @@
|
|||||||
|
mod data_layer;
|
||||||
|
mod entities;
|
||||||
|
use sqlite::Connection;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user