Added a bunch of things

This commit is contained in:
didier amyot committed 2023-03-10 16:34:56 -05:00
1 parent c365289914
commit ce92916942
15 files changed
+131 -55

No files matched your search

+34 -3
View File
@@ -4,12 +4,21 @@ use serde::Serialize;
use crate::db::_external_id::{Id, SecurityId};
use crate::util::{CustomResult, Error};
#[derive(Debug, Serialize, PartialEq)]
#[derive(Debug, Clone, Serialize, PartialEq)]
pub enum SecurityType {
Currency,
Bond,
}
impl ToString for SecurityType {
fn to_string(&self) -> String {
match self {
SecurityType::Currency => "currency".to_string(),
SecurityType::Bond => "bond".to_string(),
}
}
}
impl SecurityType {
fn from_string_unsafe(s: String) -> SecurityType {
if s == "currency".to_string() {
@@ -46,12 +55,25 @@ pub struct Security {
id: i64,
security_type: SecurityType,
symbol: String,
name: String,
}
impl Security {
pub fn id(&self) -> SecurityId {
SecurityId::from_id(self.id)
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn symbol(&self) -> String {
self.symbol.clone()
}
pub fn security_type(&self) -> SecurityType {
self.security_type.clone()
}
}
impl Security {
@@ -60,6 +82,7 @@ impl Security {
id: row.get("sec_id")?,
security_type: SecurityType::from_string_unsafe(row.get("sec_security_type")?),
symbol: row.get("sec_symbol")?,
name: row.get("sec_name")?,
})
}
}
@@ -82,7 +105,10 @@ pub fn create_security(tx: &Transaction, sec: &NewSecurity) -> CustomResult<Secu
}
pub fn get_security_by_id(tx: &Transaction, id: i32) -> CustomResult<Security> {
let q = "SELECT id AS sec_id, security_type AS sec_security_type, symbol AS sec_symbol
let q = "SELECT id AS sec_id,
security_type AS sec_security_type,
symbol AS sec_symbol,
name AS sec_name
FROM securities
WHERE id = ?1";
tx.query_row(q, params![id], Security::from_row)
@@ -90,7 +116,10 @@ pub fn get_security_by_id(tx: &Transaction, id: i32) -> CustomResult<Security> {
}
pub fn list_securities(conn: &Connection) -> CustomResult<Vec<Security>> {
let q = "SELECT id AS sec_id, security_type AS sec_security_type, symbol AS sec_symbol
let q = "SELECT id AS sec_id,
security_type AS sec_security_type,
symbol AS sec_symbol,
name AS sec_name
FROM securities
ORDER BY id ASC";
let mut stmt = conn.prepare(q).map_err(Error::SqlError)?;
@@ -166,6 +195,7 @@ mod test_securities {
id: 1,
security_type: SecurityType::Currency,
symbol: "P".to_string(),
name: "piastre".to_string(),
};
assert_eq!(1, ret.len());
assert_eq!(&expt, ret.get(0).unwrap());
@@ -175,6 +205,7 @@ mod test_securities {
id: 2,
security_type: SecurityType::Bond,
symbol: "B".to_string(),
name: "bong".to_string(),
};
let tx = conn.transaction().unwrap();
create_security(&tx, &sec)?;