53 lines
1.4 KiB
Rust
53 lines
1.4 KiB
Rust
use crate::db::external_id::SecurityId;
|
|
use crate::db::securities::{Security};
|
|
use rusqlite::Connection;
|
|
use serde::{Deserialize, Serialize};
|
|
use crate::db::{accounts, securities};
|
|
use crate::util::CustomResult;
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct SecurityDTO {
|
|
id: SecurityId,
|
|
symbol: String,
|
|
name: String,
|
|
security_type: String,
|
|
}
|
|
|
|
impl SecurityDTO {
|
|
pub fn from_entity(sec: &Security) -> SecurityDTO {
|
|
SecurityDTO {
|
|
id: sec.id(),
|
|
symbol: sec.symbol(),
|
|
name: sec.name().to_string(),
|
|
security_type: sec.security_type().to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub struct SecurityService<'a> {
|
|
conn: &'a Connection,
|
|
}
|
|
|
|
impl SecurityService<'_> {
|
|
pub fn new(conn: &Connection) -> SecurityService {
|
|
SecurityService { conn }
|
|
}
|
|
|
|
pub fn list_securities(&self) -> Vec<SecurityDTO> {
|
|
securities::list_securities(self.conn)
|
|
.unwrap()
|
|
.iter()
|
|
.map(SecurityDTO::from_entity)
|
|
.collect()
|
|
}
|
|
|
|
pub fn get_security_by_id(&self, sec_id: &SecurityId) -> CustomResult<Option<SecurityDTO>> {
|
|
let ret = securities::get_security_by_id(self.conn, sec_id)?;
|
|
Ok(ret.map(|sec|SecurityDTO::from_entity(&sec)))
|
|
}
|
|
|
|
pub fn get_security_market_cap(&self, sec_id: &SecurityId)-> CustomResult<i64> {
|
|
accounts::get_security_market_cap(self.conn, sec_id)
|
|
}
|
|
}
|