Linted code.

This commit is contained in:
didier amyot committed 2023-01-30 08:10:00 -05:00
1 parent 894f969d0f
commit 174d7e855a
17 files changed
+304 -206

No files matched your search

+48 -19
View File
@@ -1,5 +1,5 @@
use rusqlite::{Connection, OptionalExtension, params, Result, Row, Transaction};
use fraction::{Fraction};
use fraction::Fraction;
use rusqlite::{params, Connection, OptionalExtension, Result, Row, Transaction};
use crate::db::_external_id::{AccountId, Id, UserId};
use crate::db::_securities::Security;
@@ -100,7 +100,6 @@ impl AccountHistory {
}
}
pub fn list_accounts<'a>(conn: &Connection) -> CustomResult<Vec<Account>> {
let q = "SELECT acc.id AS acc_id,
acc.amount AS acc_amount,
@@ -135,17 +134,25 @@ pub fn get_account_by_id(tx: &Connection, id: &AccountId) -> CustomResult<Accoun
FROM accounts acc
JOIN securities sec ON acc.security = sec.id
WHERE acc.id = ?1";
return tx.query_row(q, params![id], Account::from_row).map_err(Error::SqlError);
return tx
.query_row(q, params![id], Account::from_row)
.map_err(Error::SqlError);
}
fn create_account(tx: &Connection, sec: &SecurityId, user: &UserId, account_type: AccountType) -> CustomResult<Account> {
fn create_account(
tx: &Connection,
sec: &SecurityId,
user: &UserId,
account_type: AccountType,
) -> CustomResult<Account> {
let q = "INSERT INTO accounts(security, version, amount, timestamp, account_type)
VALUES (?1, 0, 0, CURRENT_TIMESTAMP, ?2)";
let account_type = match account_type {
AccountType::Checking => "checking",
AccountType::SecurityEmitter => "security_emitter",
};
tx.execute(q, (sec, account_type)).map_err(Error::SqlError)?;
tx.execute(q, (sec, account_type))
.map_err(Error::SqlError)?;
let id = AccountId::from_id(tx.last_insert_rowid() as i64);
let q = "INSERT INTO account_user_permissions(user_id, account_id)
@@ -165,7 +172,8 @@ fn get_account_history(tx: &Connection, id: &AccountId) -> CustomResult<AccountH
WHERE account_id = ?1
ORDER BY account_version DESC";
let mut stmt = tx.prepare(q).map_err(Error::SqlError)?;
let snapshots: Vec<AccountSnapshot> = stmt.query_map(params![id], AccountSnapshot::from_row)
let snapshots: Vec<AccountSnapshot> = stmt
.query_map(params![id], AccountSnapshot::from_row)
.map_err(Error::SqlError)?
.filter(Result::is_ok)
.map(Result::unwrap)
@@ -174,11 +182,19 @@ fn get_account_history(tx: &Connection, id: &AccountId) -> CustomResult<AccountH
return AccountHistory::new(get_account_by_id(tx, id)?, snapshots);
}
pub fn create_checking_account(tx: &Connection, sec: &SecurityId, user: &UserId) -> CustomResult<Account> {
pub fn create_checking_account(
tx: &Connection,
sec: &SecurityId,
user: &UserId,
) -> CustomResult<Account> {
create_account(tx, sec, user, AccountType::Checking)
}
pub fn create_security_emitter_account(tx: &Connection, sec: &SecurityId, user: &UserId) -> CustomResult<Account> {
pub fn create_security_emitter_account(
tx: &Connection,
sec: &SecurityId,
user: &UserId,
) -> CustomResult<Account> {
create_account(tx, sec, user, AccountType::SecurityEmitter)
}
@@ -186,7 +202,10 @@ pub fn is_owner(tx: &Connection, account_id: &AccountId, user_id: &UserId) -> Cu
let q = "SELECT id FROM account_user_permissions WHERE account_id = ?1 AND user_id = ?2";
let params = params![account_id, user_id];
let ite = |row: &Row| row.get::<usize, i64>(0);
let option = tx.query_row(q, params, ite).optional().map_err(Error::SqlError)?;
let option = tx
.query_row(q, params, ite)
.optional()
.map_err(Error::SqlError)?;
Ok(option.is_some())
}
@@ -218,13 +237,16 @@ fn update_account_history(tx: &Connection, account_id: &AccountId) -> CustomResu
FROM accounts
WHERE id = ?1
";
tx.execute(q, params![account_id]).map_err(Error::SqlError)?;
tx.execute(q, params![account_id])
.map_err(Error::SqlError)?;
Ok(())
}
pub fn add_to_account(tx: &Connection,
account_id: &AccountId,
amount: i64) -> CustomResult<AccountChangeReceipt> {
pub fn add_to_account(
tx: &Connection,
account_id: &AccountId,
amount: i64,
) -> CustomResult<AccountChangeReceipt> {
let q = "
UPDATE accounts SET amount = CASE account_type
WHEN 'security_emitter'
@@ -236,7 +258,9 @@ pub fn add_to_account(tx: &Connection,
RETURNING id, version, amount, UNIXEPOCH(CURRENT_TIMESTAMP) as timestamp
";
let params = params![amount, account_id];
let ret = tx.query_row(q, params, AccountChangeReceipt::from_row).map_err(Error::SqlError);
let ret = tx
.query_row(q, params, AccountChangeReceipt::from_row)
.map_err(Error::SqlError);
update_account_history(tx, account_id)?;
return ret;
}
@@ -261,7 +285,11 @@ mod test_accounts {
return (conn, users, securities);
}
fn create_all_accounts(tx: &Transaction, users: &Vec<User>, secs: &Vec<Security>) -> CustomResult<Vec<Account>> {
fn create_all_accounts(
tx: &Transaction,
users: &Vec<User>,
secs: &Vec<Security>,
) -> CustomResult<Vec<Account>> {
let mut ret: Vec<Account> = Vec::new();
for sec in secs.iter() {
for user in users.iter() {
@@ -361,8 +389,10 @@ mod test_accounts {
let acc = get_account_by_id(&tx2, &acc.id()).unwrap();
assert_eq!(acc.timestamp, receipt.timestamp);
assert_eq!(acc.amount, 33);
assert_eq!(get_account_history(&tx2, &acc.id())?.average_amount(),
Fraction::from(0 + 33) / Fraction::from(2));
assert_eq!(
get_account_history(&tx2, &acc.id())?.average_amount(),
Fraction::from(0 + 33) / Fraction::from(2)
);
Ok(())
}
@@ -390,4 +420,3 @@ mod test_accounts {
Ok(())
}
}