going somewhere.
This commit is contained in:
1 parent
494cdedfc6
commit
64e9be8b5a
12 files changed
+203
-76
No files matched your search
+17
-10
@@ -1,5 +1,6 @@
|
||||
use rusqlite::{Connection, OptionalExtension, params, Result, Row, ToSql, Transaction};
|
||||
use serde::Serialize;
|
||||
use crate::db::_external_id::{Id, SecurityId, UserId};
|
||||
use crate::db::users::User;
|
||||
|
||||
|
||||
@@ -33,11 +34,17 @@ impl ToSql for SecurityType {
|
||||
|
||||
#[derive(Debug, Serialize, PartialEq)]
|
||||
pub struct Security {
|
||||
pub id: i32,
|
||||
id: u32,
|
||||
security_type: SecurityType,
|
||||
symbol: String,
|
||||
}
|
||||
|
||||
impl Security {
|
||||
pub fn id(&self) -> SecurityId {
|
||||
SecurityId::from_id(self.id)
|
||||
}
|
||||
}
|
||||
|
||||
impl Security {
|
||||
pub fn from_row(row: &Row) -> Result<Security> {
|
||||
Ok(Security {
|
||||
@@ -56,7 +63,7 @@ pub struct NewSecurity {
|
||||
|
||||
pub fn create_security(tx: &Transaction,
|
||||
sec: &NewSecurity,
|
||||
owner: &User) -> Result<Security> {
|
||||
owner: &UserId) -> Result<Security> {
|
||||
let q = "INSERT INTO
|
||||
securities(name, symbol, security_type)
|
||||
VALUES(?1, ?2, ?3)";
|
||||
@@ -67,18 +74,18 @@ pub fn create_security(tx: &Transaction,
|
||||
let q = "INSERT INTO
|
||||
security_permissions(security_id, user_id)
|
||||
VALUES(?1, ?2)";
|
||||
tx.execute(q, params![new_sec_id, owner.id])?;
|
||||
tx.execute(q, params![new_sec_id, owner])?;
|
||||
|
||||
return get_security_by_id(tx, new_sec_id);
|
||||
}
|
||||
|
||||
pub fn is_owner(tx: &Transaction, sec: &Security, user: &User) -> Result<bool> {
|
||||
pub fn is_owner(tx: &Transaction, sec_id: &SecurityId, user_id: &UserId) -> Result<bool> {
|
||||
let q = "SELECT id
|
||||
FROM security_permissions
|
||||
WHERE security_id = ?1 AND user_id = ?2
|
||||
LIMIT 1";
|
||||
let ret: Option<u32> = tx.query_row(q,
|
||||
params![sec.id, user.id],
|
||||
params![sec_id, user_id],
|
||||
|r| r.get(0)).optional()?;
|
||||
return Ok(ret.is_some());
|
||||
}
|
||||
@@ -145,10 +152,10 @@ mod test {
|
||||
let not_owner = users.get(1).unwrap();
|
||||
let sec = mock_currency_1();
|
||||
let tx = conn.transaction()?;
|
||||
let sec = create_security(&tx, &sec, &owner)?;
|
||||
let sec = create_security(&tx, &sec, &owner.id())?;
|
||||
|
||||
assert!(is_owner(&tx, &sec, &owner)?);
|
||||
assert!(!is_owner(&tx, &sec, ¬_owner)?);
|
||||
assert!(is_owner(&tx, &sec.id(), &owner.id())?);
|
||||
assert!(!is_owner(&tx, &sec.id(), ¬_owner.id())?);
|
||||
|
||||
tx.commit()
|
||||
}
|
||||
@@ -161,7 +168,7 @@ mod test {
|
||||
|
||||
let sec = mock_currency_1();
|
||||
let tx = conn.transaction()?;
|
||||
create_security(&tx, &sec, &user)?;
|
||||
create_security(&tx, &sec, &user.id())?;
|
||||
tx.commit()?;
|
||||
let ret = list_securities(&conn)?;
|
||||
let expt = Security {
|
||||
@@ -180,7 +187,7 @@ mod test {
|
||||
symbol: "B".to_string(),
|
||||
};
|
||||
let tx = conn.transaction()?;
|
||||
create_security(&tx, &sec, &user)?;
|
||||
create_security(&tx, &sec, &user.id())?;
|
||||
tx.commit()?;
|
||||
let ret = list_securities(&conn)?;
|
||||
assert_eq!(2, ret.len());
|
||||
|
||||
Reference in new issue
Block a user