Started to implement accounts in the UI.

This commit is contained in:
didier amyot committed 2023-04-01 09:16:38 -04:00
1 parent ce92916942
commit 76e1e8cb53
7 files changed
+172 -9

No files matched your search

+52
View File
@@ -0,0 +1,52 @@
use rusqlite::Connection;
use crate::db::accounts::{Account, list_user_accounts};
use crate::db::external_id::{Id, UserId};
use crate::util::{CustomResult, Error};
use serde::Deserialize;
use serde::Serialize;
pub struct AccountService<'a> {
conn: &'a Connection,
}
#[derive(Deserialize, Serialize, Debug)]
pub struct AccountDTO {
pub id: String,
pub amount: i64,
pub account_type: String,
pub last_update: i64,
pub security_symbol: String,
}
impl AccountDTO {
fn from_entity(account: & Account) -> AccountDTO {
AccountDTO{
id: account.id().to_string(),
amount: account.amount(),
account_type: account.account_type().to_string(),
last_update: account.timestamp(),
security_symbol: account.security().symbol()
}
}
}
impl AccountService<'_> {
pub fn new(conn: & Connection) -> AccountService{
AccountService{conn}
}
pub fn get_user_accounts(& self, user_id: String) -> CustomResult<Vec<AccountDTO>>{
let p = UserId::from_string(&user_id);
if p.is_none() {
let msg = format!("{} is an invalid user id.", &user_id);
return Err(Error::UserInputError(msg));
}
let ret = list_user_accounts(&self.conn, &p.unwrap())?;
Ok(ret.iter().map(AccountDTO::from_entity).collect())
}
}