feat: update services to include transaction handling and refactor database connection setup
This commit is contained in:
@@ -3,7 +3,7 @@ name = "otter"
|
||||
version = "0.1.0"
|
||||
description = "A Tauri App"
|
||||
authors = ["you"]
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@@ -23,9 +23,10 @@ tauri-plugin-opener = "2"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
serde_json = "1"
|
||||
async-trait = "0.1.89"
|
||||
decimal = "2.1.0"
|
||||
rust_decimal = "1.42.0"
|
||||
|
||||
sea-orm.workspace = true
|
||||
migration = { path = "../crates/migration" }
|
||||
chrono.workspace = true
|
||||
thiserror = "2.0.18"
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use tauri::Manager;
|
||||
|
||||
use crate::services::create_services;
|
||||
|
||||
mod commands;
|
||||
mod db;
|
||||
mod services;
|
||||
|
||||
@@ -27,8 +28,31 @@ pub fn run() {
|
||||
tauri::Builder::default()
|
||||
.setup(|app| {
|
||||
tauri::async_runtime::block_on(async {
|
||||
let mut connect_options =
|
||||
ConnectOptions::new("sqlite://finance_manager.db".to_string());
|
||||
// Get the app's data directory
|
||||
let app_data_dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.expect("Failed to get app data directory");
|
||||
|
||||
// Create the directory if it doesn't exist
|
||||
std::fs::create_dir_all(&app_data_dir)
|
||||
.expect("Failed to create app data directory");
|
||||
|
||||
// Construct the database path
|
||||
let db_path = app_data_dir.join("finance_manager.db");
|
||||
|
||||
// Ensure the database file exists
|
||||
if !db_path.exists() {
|
||||
std::fs::File::create(&db_path).expect("Failed to create database file");
|
||||
}
|
||||
|
||||
// Get the canonical path
|
||||
let canonical_path = db_path
|
||||
.canonicalize()
|
||||
.expect("Failed to get canonical path");
|
||||
let db_url = format!("sqlite://{}", canonical_path.to_string_lossy());
|
||||
|
||||
let mut connect_options = ConnectOptions::new(db_url);
|
||||
connect_options.after_connect(|conn| {
|
||||
Box::pin(async move {
|
||||
// Enable foreign key support for SQLite
|
||||
@@ -50,12 +74,22 @@ pub fn run() {
|
||||
migration::Migrator::up(&db, None)
|
||||
.await
|
||||
.expect("Failed to run migrations");
|
||||
app.manage(create_services(db))
|
||||
|
||||
let services = create_services(db);
|
||||
app.manage(services)
|
||||
});
|
||||
Ok(())
|
||||
})
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![greet])
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
commands::get_account,
|
||||
commands::get_accounts,
|
||||
commands::create_account,
|
||||
commands::get_transactions_for_account,
|
||||
commands::create_transaction,
|
||||
commands::update_transaction
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
@@ -2,22 +2,65 @@ use std::sync::Arc;
|
||||
|
||||
use sea_orm::DatabaseConnection;
|
||||
|
||||
pub mod accounts;
|
||||
use crate::services::{
|
||||
accounts::{
|
||||
AccountsService, AccountsServiceImpl,
|
||||
category::{AccountCategoryService, AccountCategoryServiceImpl},
|
||||
},
|
||||
transaction::{
|
||||
TransactionService, TransactionServiceImpl,
|
||||
category::{CategoryService, CategoryServiceImpl},
|
||||
tag::{TagService, TagServiceImpl},
|
||||
},
|
||||
};
|
||||
|
||||
pub struct Services {
|
||||
pub account_category: Arc<dyn accounts::category::AccountCategoryService>,
|
||||
pub accounts: Arc<dyn accounts::AccountsService>,
|
||||
pub mod accounts;
|
||||
pub mod transaction;
|
||||
|
||||
pub type AppState = Services<
|
||||
dyn AccountCategoryService,
|
||||
dyn AccountsService,
|
||||
dyn TransactionService,
|
||||
dyn CategoryService,
|
||||
dyn TagService,
|
||||
>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Services<AC, A, T, TC, TT>
|
||||
where
|
||||
AC: AccountCategoryService + ?Sized,
|
||||
A: AccountsService + ?Sized,
|
||||
T: TransactionService + ?Sized,
|
||||
TC: CategoryService + ?Sized,
|
||||
TT: TagService + ?Sized,
|
||||
{
|
||||
pub account_category: Arc<AC>,
|
||||
pub accounts: Arc<A>,
|
||||
pub transaction: Arc<T>,
|
||||
pub transaction_category: Arc<TC>,
|
||||
pub transaction_tag: Arc<TT>,
|
||||
}
|
||||
|
||||
pub fn create_services(db: DatabaseConnection) -> Services {
|
||||
let account_category_service = Arc::new(accounts::category::AccountCategoryServiceImpl::new(
|
||||
pub fn create_services(db: DatabaseConnection) -> AppState {
|
||||
let account_category_service: Arc<dyn AccountCategoryService> =
|
||||
Arc::new(AccountCategoryServiceImpl::new(db.clone()));
|
||||
let account_service: Arc<dyn AccountsService> = Arc::new(AccountsServiceImpl::new(
|
||||
db.clone(),
|
||||
Arc::new(AccountCategoryServiceImpl::new(db.clone())),
|
||||
));
|
||||
let transaction_category_service: Arc<dyn CategoryService> =
|
||||
Arc::new(CategoryServiceImpl::new(db.clone()));
|
||||
let transaction_tag_service: Arc<dyn TagService> = Arc::new(TagServiceImpl::new(db.clone()));
|
||||
Services {
|
||||
account_category: account_category_service.clone(),
|
||||
accounts: Arc::new(accounts::AccountsServiceImpl::new(
|
||||
db,
|
||||
account_category_service,
|
||||
)),
|
||||
accounts: account_service.clone(),
|
||||
transaction: Arc::new(TransactionServiceImpl::new(
|
||||
db.clone(),
|
||||
account_service.clone(),
|
||||
transaction_category_service.clone(),
|
||||
transaction_tag_service.clone(),
|
||||
)) as Arc<dyn TransactionService>,
|
||||
transaction_category: transaction_category_service,
|
||||
transaction_tag: transaction_tag_service,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user