Skip to content

Commit

Permalink
Merge pull request #101 from afadil/dev
Browse files Browse the repository at this point in the history
Merge dev
  • Loading branch information
afadil authored Sep 11, 2024
2 parents 098cafc + d05ace0 commit 561662a
Show file tree
Hide file tree
Showing 17 changed files with 530 additions and 489 deletions.
9 changes: 0 additions & 9 deletions diesel.toml

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "wealthfolio-app",
"private": true,
"version": "1.0.11",
"version": "1.0.12",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
525 changes: 287 additions & 238 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

42 changes: 14 additions & 28 deletions src-core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 10 additions & 11 deletions src-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
diesel = { version = "2.2.3", features = ["sqlite", "chrono", "numeric", "returning_clauses_for_sqlite_3_35"] }
dotenvy = "0.15.7"
chrono = { version = "0.4", features = ["serde"] }
serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
diesel = { version = "2.2.4", features = ["sqlite", "chrono", "numeric", "returning_clauses_for_sqlite_3_35"] }
chrono = { version = "0.4.38", features = ["serde"] }
uuid = { version = "1.10.0", features = ["v4"] }
rusqlite = { version = "0.30.0", features = ["bundled"] }
rusqlite = { version = "0.32.1", features = ["bundled"] }
csv = "1.3.0"
yahoo_finance_api = "2.2.1"
regex = "1.10.2"
regex = "1.10.6"
reqwest = { version = "0.12.7", features = ["json", "cookies" ] }
thiserror = "1.0.50"
lazy_static = "1.4.0"
diesel_migrations = { version = "2.1.0", features = ["sqlite" ] }
rayon = "1.8.0"
thiserror = "1.0.63"
lazy_static = "1.5.0"
diesel_migrations = { version = "2.2.0", features = ["sqlite" ] }
rayon = "1.10.0"
1 change: 1 addition & 0 deletions src-core/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ use std::sync::Mutex;

pub struct AppState {
pub conn: Mutex<SqliteConnection>,
pub db_path: String,
}
14 changes: 6 additions & 8 deletions src-core/src/asset/asset_service.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::db;
use crate::models::{Asset, AssetProfile, NewAsset, Quote, QuoteSummary};
use crate::providers::yahoo_provider::YahooProvider;
use std::time::SystemTime;
Expand Down Expand Up @@ -51,9 +50,7 @@ impl AssetService {
conn: &mut SqliteConnection,
asset_id: &str,
) -> Result<Asset, diesel::result::Error> {
assets::table
.find(asset_id)
.first::<Asset>(conn)
assets::table.find(asset_id).first::<Asset>(conn)
}

pub fn get_asset_data(
Expand Down Expand Up @@ -369,16 +366,17 @@ impl AssetService {
Ok(())
}

pub async fn initialize_and_sync_quotes(&self) -> Result<(), String> {
pub async fn initialize_and_sync_quotes(
&self,
conn: &mut SqliteConnection,
) -> Result<(), String> {
// Initialize crumb data
if let Err(e) = self.initialize_crumb_data().await {
return Err(format!("Failed to initialize crumb data: {}", e));
}

let mut conn = db::establish_connection();

// Synchronize history quotes
if let Err(e) = self.sync_history_quotes_for_all_assets(&mut conn).await {
if let Err(e) = self.sync_history_quotes_for_all_assets(conn).await {
return Err(format!("Failed to sync history quotes: {}", e));
}

Expand Down
49 changes: 14 additions & 35 deletions src-core/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,23 @@
use std::fs;
use std::path::Path;
use std::{env, fs};

use diesel::sqlite::SqliteConnection;
use diesel::{prelude::*, sql_query};
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use dotenvy::dotenv;

const MIGRATIONS: EmbeddedMigrations = embed_migrations!();

pub fn init() {
dotenv().ok(); // Load environment variables from .env file if available

if !db_file_exists() {
create_db_file();
pub fn init(db_path: &str) {
if !db_file_exists(db_path) {
create_db_file(db_path);
}

run_migrations();
run_migrations(db_path);
}

pub fn establish_connection() -> SqliteConnection {
let db_path = get_db_path();

pub fn establish_connection(db_path: &str) -> SqliteConnection {
// Establish the database connection
let mut conn = SqliteConnection::establish(&db_path)
let mut conn = SqliteConnection::establish(db_path)
.unwrap_or_else(|_| panic!("Error connecting to {}", db_path));

// Enable foreign key constraint enforcement
Expand All @@ -33,14 +28,13 @@ pub fn establish_connection() -> SqliteConnection {
conn // Return the established database connection
}

fn run_migrations() {
let mut connection = establish_connection();
fn run_migrations(db_path: &str) {
let mut connection = establish_connection(db_path);
connection.run_pending_migrations(MIGRATIONS).unwrap();
}

fn create_db_file() {
let db_path = get_db_path();
let db_dir = Path::new(&db_path).parent().unwrap();
fn create_db_file(db_path: &str) {
let db_dir = Path::new(db_path).parent().unwrap();

if !db_dir.exists() {
fs::create_dir_all(db_dir).unwrap();
Expand All @@ -49,22 +43,7 @@ fn create_db_file() {
fs::File::create(db_path).unwrap();
}

fn db_file_exists() -> bool {
let db_path = get_db_path();
Path::new(&db_path).exists()
}

fn get_db_path() -> String {
// Try to get the database URL from the environment variable
match env::var("DATABASE_URL") {
Ok(url) => url, // If DATABASE_URL is set, use it
Err(_) => {
// Fall back to ./app.db
Path::new(&env::current_dir().unwrap())
.join("app.db")
.to_str()
.unwrap()
.to_string()
}
}
fn db_file_exists(db_path: &str) -> bool {
println!("db_path: {}", db_path);
Path::new(db_path).exists()
}
4 changes: 2 additions & 2 deletions src-core/src/portfolio/portfolio_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl PortfolioService {
}
}

pub async fn compute_holdings(
pub fn compute_holdings(
&self,
conn: &mut SqliteConnection,
) -> Result<Vec<Holding>, Box<dyn std::error::Error>> {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl PortfolioService {
Ok((accounts, activities, market_data))
}

pub async fn calculate_historical_portfolio_values(
pub fn calculate_historical_portfolio_values(
&self,
conn: &mut SqliteConnection,
) -> Result<Vec<FinancialHistory>, Box<dyn std::error::Error>> {
Expand Down
Loading

0 comments on commit 561662a

Please sign in to comment.