62 lines
1.6 KiB
Rust
62 lines
1.6 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use crate::db::external_id::{SecurityId, UserId};
|
|
|
|
use crate::db::securities::{NewSecurity, SecurityType};
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
pub struct CreateNewSecurity {
|
|
pub security_type: String,
|
|
pub symbol: String,
|
|
pub name: String,
|
|
pub userId: UserId,
|
|
}
|
|
|
|
impl CreateNewSecurity {
|
|
fn get_security_type(&self) -> Option<SecurityType> {
|
|
SecurityType::from_string(self.security_type.clone())
|
|
}
|
|
pub fn to_create_new_security(&self) -> Option<NewSecurity> {
|
|
self.get_security_type().map(|security_type| NewSecurity {
|
|
security_type,
|
|
symbol: self.symbol.clone(),
|
|
name: self.name.clone(),
|
|
})
|
|
}
|
|
|
|
fn validate_event(&self) -> Result<(), String> {
|
|
if SecurityType::from_string(self.security_type.clone()).is_none() {
|
|
Err(format!("Invalid SecurityType \"{}\".", self.security_type))
|
|
} else {
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
pub struct CreateNewUser {
|
|
pub name: String,
|
|
pub public_key: String,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
pub struct CreateNewAccount {
|
|
pub securityId: SecurityId,
|
|
pub userId: UserId,
|
|
}
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
pub enum Event {
|
|
CreateNewSecurity(CreateNewSecurity),
|
|
CreateNewUser(CreateNewUser),
|
|
CreateNewAccount(CreateNewAccount),
|
|
}
|
|
|
|
impl Event {
|
|
pub fn validate_event(&self) -> Result<(), String> {
|
|
match self {
|
|
Event::CreateNewSecurity(event) => event.validate_event(),
|
|
_ => Ok(()),
|
|
}
|
|
}
|
|
}
|