feat: update services to include transaction handling and refactor database connection setup

This commit is contained in:
GW_MC
2026-05-28 04:02:14 +00:00
parent 79c9865823
commit 671e0f8ead
4 changed files with 96 additions and 43 deletions

View File

@@ -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,
}
}