implement account and account category services with CRUD operations
This commit is contained in:
33
Cargo.lock
generated
33
Cargo.lock
generated
@@ -827,8 +827,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
|
checksum = "c673075a2e0e5f4a1dde27ce9dee1ea4558c7ffe648f576438a20ca1d2acc4b0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"iana-time-zone",
|
"iana-time-zone",
|
||||||
|
"js-sys",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
"serde",
|
"serde",
|
||||||
|
"wasm-bindgen",
|
||||||
"windows-link 0.2.1",
|
"windows-link 0.2.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1164,6 +1166,20 @@ dependencies = [
|
|||||||
"windows-sys 0.61.2",
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "decimal"
|
||||||
|
version = "2.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a8ab77e91baeb15034c3be91e87bff4665c9036216148e4996d9a9f5792114d"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
"ord_subset",
|
||||||
|
"rustc-serialize",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "der"
|
name = "der"
|
||||||
version = "0.7.10"
|
version = "0.7.10"
|
||||||
@@ -3254,6 +3270,12 @@ version = "0.2.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ord_subset"
|
||||||
|
version = "3.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fdcf5505c0f054ce51fa0fa74142738930a45d5ac1faacae4dd4e2f54afe00fa"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ordered-float"
|
name = "ordered-float"
|
||||||
version = "4.6.0"
|
version = "4.6.0"
|
||||||
@@ -3277,6 +3299,11 @@ dependencies = [
|
|||||||
name = "otter"
|
name = "otter"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"async-trait",
|
||||||
|
"chrono",
|
||||||
|
"decimal",
|
||||||
|
"migration",
|
||||||
|
"sea-orm",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"tauri",
|
"tauri",
|
||||||
@@ -3980,6 +4007,12 @@ version = "2.1.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"
|
checksum = "94300abf3f1ae2e2b8ffb7b58043de3d399c73fa6f4b73826402a5c457614dbe"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustc-serialize"
|
||||||
|
version = "0.3.25"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fe834bc780604f4674073badbad26d7219cadfb4a2275802db12cbae17498401"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustc_version"
|
name = "rustc_version"
|
||||||
version = "0.4.1"
|
version = "0.4.1"
|
||||||
|
|||||||
@@ -22,4 +22,10 @@ tauri = { version = "2", features = [] }
|
|||||||
tauri-plugin-opener = "2"
|
tauri-plugin-opener = "2"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
async-trait = "0.1.89"
|
||||||
|
decimal = "2.1.0"
|
||||||
|
|
||||||
|
sea-orm.workspace = true
|
||||||
|
migration = { path = "../crates/migration" }
|
||||||
|
chrono.workspace = true
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,20 @@
|
|||||||
|
#![forbid(unsafe_code, clippy::unwrap_used, clippy::panic)]
|
||||||
|
#![deny(
|
||||||
|
unused_must_use,
|
||||||
|
clippy::expect_used,
|
||||||
|
clippy::unimplemented,
|
||||||
|
clippy::todo
|
||||||
|
)]
|
||||||
|
|
||||||
|
use migration::MigratorTrait;
|
||||||
|
use sea_orm::{ConnectOptions, ConnectionTrait, Database, Statement};
|
||||||
|
use tauri::Manager;
|
||||||
|
|
||||||
|
use crate::services::create_services;
|
||||||
|
|
||||||
|
mod db;
|
||||||
|
mod services;
|
||||||
|
|
||||||
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn greet(name: &str) -> String {
|
fn greet(name: &str) -> String {
|
||||||
@@ -6,7 +23,37 @@ fn greet(name: &str) -> String {
|
|||||||
|
|
||||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
|
#[allow(clippy::expect_used)]
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
|
.setup(|app| {
|
||||||
|
tauri::async_runtime::block_on(async {
|
||||||
|
let mut connect_options =
|
||||||
|
ConnectOptions::new("sqlite://finance_manager.db".to_string());
|
||||||
|
connect_options.after_connect(|conn| {
|
||||||
|
Box::pin(async move {
|
||||||
|
// Enable foreign key support for SQLite
|
||||||
|
conn.execute_raw(Statement::from_string(
|
||||||
|
conn.get_database_backend(),
|
||||||
|
"PRAGMA foreign_keys = ON;",
|
||||||
|
))
|
||||||
|
.await
|
||||||
|
.map(|_| ())
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
#[allow(clippy::expect_used)]
|
||||||
|
let db = Database::connect(connect_options)
|
||||||
|
.await
|
||||||
|
.expect("Failed to connect to database");
|
||||||
|
// Run migrations if needed
|
||||||
|
#[allow(clippy::expect_used)]
|
||||||
|
migration::Migrator::up(&db, None)
|
||||||
|
.await
|
||||||
|
.expect("Failed to run migrations");
|
||||||
|
app.manage(create_services(db))
|
||||||
|
});
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
.plugin(tauri_plugin_opener::init())
|
.plugin(tauri_plugin_opener::init())
|
||||||
.invoke_handler(tauri::generate_handler![greet])
|
.invoke_handler(tauri::generate_handler![greet])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
|
|||||||
196
src-tauri/src/services/accounts/category.rs
Normal file
196
src-tauri/src/services/accounts/category.rs
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
use crate::db::{
|
||||||
|
entities::account_category::{
|
||||||
|
ActiveModel as AccountCategoryActiveModel, Entity as AccountCategoryEntity,
|
||||||
|
},
|
||||||
|
Db,
|
||||||
|
};
|
||||||
|
use sea_orm::{
|
||||||
|
ActiveModelTrait, ActiveValue::Set, ColumnTrait, ConnectionTrait, DatabaseConnection,
|
||||||
|
DatabaseTransaction, EntityTrait, IntoActiveModel, ModelTrait, PaginatorTrait, QueryFilter,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct AccountCategory {
|
||||||
|
pub id: i64,
|
||||||
|
pub name: String,
|
||||||
|
pub account_type: AccountType,
|
||||||
|
//
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub enum AccountType {
|
||||||
|
Asset, // Positive balance, e.g. Checking, Savings
|
||||||
|
Liability, // Negative balance, e.g. Credit Card
|
||||||
|
Unknown, // Fallback for unknown types
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CreateAccountCategoryRequest {
|
||||||
|
pub name: String,
|
||||||
|
pub account_type: AccountType,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UpdateAccountCategoryRequest {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub account_type: Option<AccountType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for AccountType {
|
||||||
|
fn from(s: String) -> Self {
|
||||||
|
match s.as_str() {
|
||||||
|
"Asset" => AccountType::Asset,
|
||||||
|
"Liability" => AccountType::Liability,
|
||||||
|
_ => AccountType::Unknown,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for AccountType {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
AccountType::Asset => write!(f, "Asset"),
|
||||||
|
AccountType::Liability => write!(f, "Liability"),
|
||||||
|
AccountType::Unknown => write!(f, "Unknown"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AccountCategoryService: Send + Sync + 'static {
|
||||||
|
async fn get_categories(&self) -> Result<Vec<AccountCategory>, String>;
|
||||||
|
async fn create_category(
|
||||||
|
&self,
|
||||||
|
create_request: CreateAccountCategoryRequest,
|
||||||
|
) -> Result<i64, String>;
|
||||||
|
|
||||||
|
async fn create_category_with_tx(
|
||||||
|
&self,
|
||||||
|
create_request: CreateAccountCategoryRequest,
|
||||||
|
tx: &Db,
|
||||||
|
) -> Result<i64, String>;
|
||||||
|
|
||||||
|
async fn update_category(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountCategoryRequest,
|
||||||
|
) -> Result<(), String>;
|
||||||
|
|
||||||
|
async fn update_category_with_tx(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountCategoryRequest,
|
||||||
|
tx: &Db,
|
||||||
|
) -> Result<(), String>;
|
||||||
|
|
||||||
|
async fn delete_category(&self, id: &i64) -> Result<(), String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AccountCategoryServiceImpl {
|
||||||
|
db: DatabaseConnection,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AccountCategoryServiceImpl {
|
||||||
|
pub fn new(db: DatabaseConnection) -> Self {
|
||||||
|
Self { db }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AccountCategoryService for AccountCategoryServiceImpl {
|
||||||
|
async fn get_categories(&self) -> Result<Vec<AccountCategory>, String> {
|
||||||
|
let categories = AccountCategoryEntity::find()
|
||||||
|
.all(&self.db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
Ok(categories
|
||||||
|
.into_iter()
|
||||||
|
.map(|model| AccountCategory {
|
||||||
|
id: model.id,
|
||||||
|
name: model.name,
|
||||||
|
account_type: AccountType::from(model.account_type),
|
||||||
|
created_at: model.created_at,
|
||||||
|
updated_at: model.updated_at,
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_category(
|
||||||
|
&self,
|
||||||
|
create_request: CreateAccountCategoryRequest,
|
||||||
|
) -> Result<i64, String> {
|
||||||
|
self.create_category_with_tx(create_request, &(&self.db).into())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_category_with_tx(
|
||||||
|
&self,
|
||||||
|
create_request: CreateAccountCategoryRequest,
|
||||||
|
tx: &Db,
|
||||||
|
) -> Result<i64, String> {
|
||||||
|
let new_category = AccountCategoryActiveModel {
|
||||||
|
name: Set(create_request.name),
|
||||||
|
account_type: Set(create_request.account_type.to_string()),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
let res = new_category.insert(tx).await.map_err(|e| e.to_string())?;
|
||||||
|
Ok(res.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_category(&self, id: &i64) -> Result<(), String> {
|
||||||
|
// check if any accounts are using this category before deleting
|
||||||
|
let accounts_using_category = crate::db::entities::account::Entity::find()
|
||||||
|
.filter(crate::db::entities::account::Column::AccountCategoryId.eq(*id))
|
||||||
|
.count(&self.db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
if accounts_using_category > 0 {
|
||||||
|
return Err("Cannot delete category that is in use by accounts".to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
let category = AccountCategoryEntity::find_by_id(*id)
|
||||||
|
.one(&self.db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())?;
|
||||||
|
if let Some(category) = category {
|
||||||
|
category.delete(&self.db).await.map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("Category not found".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_category(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountCategoryRequest,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
self.update_category_with_tx(id, update_request, &(&self.db).into())
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_category_with_tx(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountCategoryRequest,
|
||||||
|
tx: &Db,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let category = AccountCategoryEntity::find_by_id(*id)
|
||||||
|
.one(&self.db)
|
||||||
|
.await
|
||||||
|
.map_err(|e| e.to_string())
|
||||||
|
.map(|opt| opt.map(|model| model.into_active_model()))?;
|
||||||
|
|
||||||
|
if let Some(mut category) = category {
|
||||||
|
if let Some(name) = update_request.name {
|
||||||
|
category.name = Set(name);
|
||||||
|
}
|
||||||
|
if let Some(account_type) = update_request.account_type {
|
||||||
|
category.account_type = Set(account_type.to_string());
|
||||||
|
}
|
||||||
|
category.update(tx).await.map_err(|e| e.to_string())?;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err("Category not found".to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
186
src-tauri/src/services/accounts/mod.rs
Normal file
186
src-tauri/src/services/accounts/mod.rs
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
db::entities::{
|
||||||
|
account::{
|
||||||
|
ActiveModel as AccountActiveModel, Entity as AccountEntity, Model as AccountModel,
|
||||||
|
},
|
||||||
|
account_category::{
|
||||||
|
ActiveModel as AccountCategoryActiveModel, Entity as AccountCategoryEntity,
|
||||||
|
Model as AccountCategoryModel,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
services::accounts::category::{
|
||||||
|
AccountType, CreateAccountCategoryRequest, UpdateAccountCategoryRequest,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use sea_orm::{
|
||||||
|
ActiveModelTrait, ActiveValue::Set, DatabaseConnection, EntityTrait, TransactionTrait,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub mod category;
|
||||||
|
|
||||||
|
pub struct Account {
|
||||||
|
pub id: i64,
|
||||||
|
pub name: String,
|
||||||
|
pub account_type: AccountType,
|
||||||
|
pub account_category: String,
|
||||||
|
//
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<(AccountModel, AccountCategoryModel)> for Account {
|
||||||
|
fn from((account_model, category_model): (AccountModel, AccountCategoryModel)) -> Self {
|
||||||
|
Account {
|
||||||
|
id: account_model.id,
|
||||||
|
name: account_model.name,
|
||||||
|
account_type: AccountType::from(category_model.account_type),
|
||||||
|
account_category: category_model.name,
|
||||||
|
created_at: account_model.created_at,
|
||||||
|
updated_at: account_model.updated_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CreateAccountRequest {
|
||||||
|
pub name: String,
|
||||||
|
pub category: CreateAccountCategoryRequest,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UpdateAccountRequest {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub category: Option<UpdateAccountCategoryRequest>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
pub trait AccountsService: Send + Sync + 'static {
|
||||||
|
async fn get_accounts(&self) -> Result<Vec<Account>, String>;
|
||||||
|
async fn get_account(&self, id: &i64) -> Result<Option<Account>, String>;
|
||||||
|
async fn create_account(&self, create_request: CreateAccountRequest) -> Result<i64, String>;
|
||||||
|
async fn update_account(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountRequest,
|
||||||
|
) -> Result<(), String>;
|
||||||
|
async fn delete_account(&self, id: &i64) -> Result<(), String>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AccountsServiceImpl {
|
||||||
|
db: DatabaseConnection,
|
||||||
|
account_category_service: Arc<dyn category::AccountCategoryService>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AccountsServiceImpl {
|
||||||
|
pub fn new(
|
||||||
|
db: DatabaseConnection,
|
||||||
|
account_category_service: Arc<dyn category::AccountCategoryService>,
|
||||||
|
) -> Self {
|
||||||
|
Self {
|
||||||
|
db,
|
||||||
|
account_category_service,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait::async_trait]
|
||||||
|
impl AccountsService for AccountsServiceImpl {
|
||||||
|
async fn get_accounts(&self) -> Result<Vec<Account>, String> {
|
||||||
|
let accounts = AccountEntity::find()
|
||||||
|
.find_both_related(AccountCategoryEntity)
|
||||||
|
.all(&self.db)
|
||||||
|
.await;
|
||||||
|
match accounts {
|
||||||
|
Ok(accounts) => Ok(accounts.into_iter().map(|a| a.into()).collect()),
|
||||||
|
Err(e) => Err(format!("Failed to fetch accounts: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_account(&self, id: &i64) -> Result<Option<Account>, String> {
|
||||||
|
let result = AccountEntity::find_by_id(*id)
|
||||||
|
.find_both_related(AccountCategoryEntity)
|
||||||
|
.one(&self.db)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(Some(account)) => Ok(Some(account.into())),
|
||||||
|
Ok(None) => Ok(None),
|
||||||
|
Err(e) => Err(format!("Failed to fetch account: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn create_account(&self, create_request: CreateAccountRequest) -> Result<i64, String> {
|
||||||
|
let tx = self.db.begin().await.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
let category_id = self
|
||||||
|
.account_category_service
|
||||||
|
.create_category_with_tx(
|
||||||
|
CreateAccountCategoryRequest {
|
||||||
|
name: create_request.category.name,
|
||||||
|
account_type: create_request.category.account_type,
|
||||||
|
},
|
||||||
|
&(&tx).into(),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let new_account = AccountEntity::insert(AccountActiveModel {
|
||||||
|
name: Set(create_request.name),
|
||||||
|
created_at: Set(chrono::Utc::now().to_string()),
|
||||||
|
updated_at: Set(chrono::Utc::now().to_string()),
|
||||||
|
account_category_id: Set(category_id),
|
||||||
|
..Default::default()
|
||||||
|
})
|
||||||
|
.exec(&tx)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
tx.commit().await.map_err(|e| e.to_string())?;
|
||||||
|
|
||||||
|
match new_account {
|
||||||
|
Ok(res) => Ok(res.last_insert_id),
|
||||||
|
Err(e) => Err(format!("Failed to create account: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn update_account(
|
||||||
|
&self,
|
||||||
|
id: &i64,
|
||||||
|
update_request: UpdateAccountRequest,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let tx = self.db.begin().await.map_err(|e| e.to_string())?;
|
||||||
|
let account = AccountEntity::find_by_id(*id).one(&tx).await;
|
||||||
|
match account {
|
||||||
|
Ok(Some(account)) => {
|
||||||
|
let mut active_model: AccountActiveModel = account.into();
|
||||||
|
// Only update fields that are provided in the request
|
||||||
|
if let Some(name) = update_request.name {
|
||||||
|
active_model.name = Set(name);
|
||||||
|
}
|
||||||
|
//
|
||||||
|
if let Some(category_request) = update_request.category {
|
||||||
|
// update the category if it is provided in the request
|
||||||
|
self.account_category_service
|
||||||
|
.update_category_with_tx(id, category_request, &(&tx).into())
|
||||||
|
.await?;
|
||||||
|
}
|
||||||
|
active_model.updated_at = Set(chrono::Utc::now().to_string());
|
||||||
|
//
|
||||||
|
active_model.update(&tx).await.map_err(|e| e.to_string())?;
|
||||||
|
let res = tx.commit().await.map_err(|e| e.to_string());
|
||||||
|
match res {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => Err(format!("Failed to update account: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(None) => Err("Account not found".to_string()),
|
||||||
|
Err(e) => Err(format!("Failed to fetch account: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_account(&self, id: &i64) -> Result<(), String> {
|
||||||
|
let res = AccountEntity::delete_by_id(*id).exec(&self.db).await;
|
||||||
|
match res {
|
||||||
|
Ok(_) => Ok(()),
|
||||||
|
Err(e) => Err(format!("Failed to delete account: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
23
src-tauri/src/services/mod.rs
Normal file
23
src-tauri/src/services/mod.rs
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use sea_orm::DatabaseConnection;
|
||||||
|
|
||||||
|
pub mod accounts;
|
||||||
|
|
||||||
|
pub struct Services {
|
||||||
|
pub account_category: Arc<dyn accounts::category::AccountCategoryService>,
|
||||||
|
pub accounts: Arc<dyn accounts::AccountsService>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_services(db: DatabaseConnection) -> Services {
|
||||||
|
let account_category_service = Arc::new(accounts::category::AccountCategoryServiceImpl::new(
|
||||||
|
db.clone(),
|
||||||
|
));
|
||||||
|
Services {
|
||||||
|
account_category: account_category_service.clone(),
|
||||||
|
accounts: Arc::new(accounts::AccountsServiceImpl::new(
|
||||||
|
db,
|
||||||
|
account_category_service,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user