Avec un cafe.
This commit is contained in:
1 parent
d78b4f1d90
commit
352bb498d4
7 files changed
+86
-50
No files matched your search
+50
-3
@@ -1,7 +1,7 @@
|
||||
use rusqlite::{Connection, OptionalExtension, params, Result, Row, Transaction};
|
||||
use sha2::digest::typenum::op;
|
||||
use crate::db::_external_id::{Id, AccountId, UserId};
|
||||
|
||||
use crate::db::_external_id::{AccountId, Id, UserId};
|
||||
use crate::db::_securities::Security;
|
||||
use crate::db::_users::User;
|
||||
use crate::db::external_id::SecurityId;
|
||||
@@ -26,7 +26,7 @@ impl AccountType {
|
||||
pub struct Account {
|
||||
id: u32,
|
||||
amount: u32,
|
||||
timestamp: i32,
|
||||
timestamp: i64,
|
||||
security: Security,
|
||||
account_type: AccountType,
|
||||
}
|
||||
@@ -119,12 +119,39 @@ pub fn is_owner(tx: &Transaction, account_id: &AccountId, user_id: &UserId) -> R
|
||||
Ok(option.is_some())
|
||||
}
|
||||
|
||||
pub struct Receipt {
|
||||
account_id: AccountId,
|
||||
timestamp: i64,
|
||||
amount: i64,
|
||||
}
|
||||
|
||||
impl Receipt {
|
||||
pub fn from_row(row: &Row) -> Result<Receipt> {
|
||||
Ok(Receipt {
|
||||
account_id: AccountId::from_id(row.get("id")?),
|
||||
amount: row.get("amount")?,
|
||||
timestamp: row.get("timestamp")?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_to_account(tx: &Transaction, account_id: &AccountId, amount: i64) -> Result<Receipt> {
|
||||
let q = "
|
||||
UPDATE accounts SET amount = amount + ?1,
|
||||
timestamp = CURRENT_TIMESTAMP
|
||||
WHERE id = ?2
|
||||
RETURNING id, amount, UNIXEPOCH(CURRENT_TIMESTAMP) as timestamp
|
||||
";
|
||||
let params = params![amount, account_id];
|
||||
tx.query_row(q, params, Receipt::from_row)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_accounts {
|
||||
use rusqlite::Connection;
|
||||
|
||||
use crate::db::_test_utils::{setup_users_and_securities};
|
||||
use crate::db::_securities;
|
||||
use crate::db::_test_utils::setup_users_and_securities;
|
||||
use crate::db::_users;
|
||||
use crate::migration;
|
||||
|
||||
@@ -222,5 +249,25 @@ mod test_accounts {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_to_account() -> Result<()> {
|
||||
let (mut conn, users, secs) = setup();
|
||||
let tx = conn.transaction().unwrap();
|
||||
let sec = secs.get(0).unwrap().id();
|
||||
let user = users.get(0).unwrap().id();
|
||||
let acc = create_checking_account(&tx, &sec, &user)?;
|
||||
let receipt = add_to_account(& tx, & acc.id(), 33)?;
|
||||
assert_eq!(receipt.account_id, acc.id());
|
||||
assert_eq!(receipt.amount, 33);
|
||||
tx.commit()?;
|
||||
|
||||
let tx = conn.transaction().unwrap();
|
||||
let acc = get_account_by_id(&tx, & acc.id())?;
|
||||
assert_eq!(acc.timestamp, receipt.timestamp);
|
||||
assert_eq!(acc.amount, 33);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user