Still working on this shit...
This commit is contained in:
1 parent
89597dcac2
commit
801abed9a4
6 files changed
+65
-15
No files matched your search
@@ -6,7 +6,7 @@
|
||||
maxlength="40"
|
||||
placeholder="Narnia Dollar"
|
||||
aria-label="Default"
|
||||
name="event.Event.name">
|
||||
name="event.CreateNewSecurity.name">
|
||||
</label>
|
||||
</div>
|
||||
<div class="uk-width-1-4@s">
|
||||
@@ -16,16 +16,16 @@
|
||||
placeholder="ND"
|
||||
maxlength="5"
|
||||
aria-label="Default"
|
||||
name="event.Event.symbol">
|
||||
name="event.CreateNewSecurity.symbol">
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="uk-margin">
|
||||
<label class="uk-form-label"> Type
|
||||
<select name="event.Event.symbol" class="uk-select">
|
||||
<option>Currency</option>
|
||||
<option>Bond</option>
|
||||
<select name="event.CreateNewSecurity.security_type" class="uk-select">
|
||||
<option value="currency">Currency</option>
|
||||
<option value="bond">Bond</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
+13
-4
@@ -1,4 +1,4 @@
|
||||
use rusqlite::{params, Connection, Result, Row, ToSql, Transaction};
|
||||
use rusqlite::{Connection, params, Result, Row, ToSql, Transaction};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::db::_external_id::{Id, SecurityId};
|
||||
@@ -11,7 +11,7 @@ pub enum SecurityType {
|
||||
}
|
||||
|
||||
impl SecurityType {
|
||||
fn from_string(s: String) -> SecurityType {
|
||||
fn from_string_unsafe(s: String) -> SecurityType {
|
||||
if s == "currency".to_string() {
|
||||
return SecurityType::Currency;
|
||||
}
|
||||
@@ -20,6 +20,15 @@ impl SecurityType {
|
||||
}
|
||||
panic!("Impossible to convert string {} to SecurityType.", s);
|
||||
}
|
||||
pub fn from_string(s: String) -> Option<SecurityType> {
|
||||
if s == "currency".to_string() {
|
||||
Some(SecurityType::Currency)
|
||||
} else if s == "bond".to_string() {
|
||||
Some(SecurityType::Bond)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql for SecurityType {
|
||||
@@ -49,7 +58,7 @@ impl Security {
|
||||
pub fn from_row(row: &Row) -> Result<Security> {
|
||||
Ok(Security {
|
||||
id: row.get("sec_id")?,
|
||||
security_type: SecurityType::from_string(row.get("sec_security_type")?),
|
||||
security_type: SecurityType::from_string_unsafe(row.get("sec_security_type")?),
|
||||
symbol: row.get("sec_symbol")?,
|
||||
})
|
||||
}
|
||||
@@ -97,7 +106,7 @@ pub fn list_securities(conn: &Connection) -> CustomResult<Vec<Security>> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test_securities {
|
||||
use rusqlite::{params, Connection};
|
||||
use rusqlite::{Connection, params};
|
||||
|
||||
use crate::db::_test_utils::*;
|
||||
use crate::db::users::User;
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ pub mod external_id {
|
||||
}
|
||||
|
||||
pub mod securities {
|
||||
pub use super::_securities::{get_security_by_id, list_securities, Security};
|
||||
pub use super::_securities::{get_security_by_id, create_security, list_securities, Security, NewSecurity, SecurityType};
|
||||
}
|
||||
|
||||
pub mod users {
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
use rusqlite::{Connection, Transaction};
|
||||
|
||||
use crate::db::securities::create_security;
|
||||
use crate::db::users::create_user;
|
||||
use crate::events::{CreateNewSecurity, CreateNewUser, Event};
|
||||
use crate::util::{CustomResult, Error};
|
||||
|
||||
fn process_bogus_event(tx: &Transaction, event: CreateNewSecurity) -> CustomResult<()> {
|
||||
fn process_create_new_security_event(tx: &Transaction, event: CreateNewSecurity) -> CustomResult<()> {
|
||||
let r = event.to_create_new_security().unwrap();
|
||||
create_security(tx, &r)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,7 +19,7 @@ fn process_create_new_user_event(tx: &mut Transaction, event: CreateNewUser) ->
|
||||
pub fn process_event(conn: &mut Connection, event: Event) -> CustomResult<()> {
|
||||
let mut tx = conn.transaction().map_err(Error::SqlError)?;
|
||||
match event {
|
||||
Event::CreateNewSecurity(e) => process_bogus_event(&mut tx, e)?,
|
||||
Event::CreateNewSecurity(e) => process_create_new_security_event(&mut tx, e)?,
|
||||
Event::CreateNewUser(e) => process_create_new_user_event(&mut tx, e)?,
|
||||
};
|
||||
tx.commit().map_err(Error::SqlError)
|
||||
|
||||
+35
-1
@@ -1,10 +1,35 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::db::securities::{NewSecurity, SecurityType};
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct CreateNewSecurity {
|
||||
pub security_type: String,
|
||||
pub symbol: String,
|
||||
pub name: String,
|
||||
pub age: i64,
|
||||
}
|
||||
|
||||
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, Debug)]
|
||||
@@ -17,4 +42,13 @@ pub struct CreateNewUser {
|
||||
pub enum Event {
|
||||
CreateNewSecurity(CreateNewSecurity),
|
||||
CreateNewUser(CreateNewUser),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn validate_event(&self) -> Result<(), String>{
|
||||
match self {
|
||||
Event::CreateNewSecurity(event) => event.validate_event(),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
-2
@@ -5,8 +5,8 @@ use clap::builder::TypedValueParser;
|
||||
use log::info;
|
||||
use minijinja::context;
|
||||
use serde::Deserialize;
|
||||
use crate::db::users::list_users;
|
||||
|
||||
use crate::db::users::list_users;
|
||||
use crate::events::Event;
|
||||
use crate::services::ServiceFactory;
|
||||
use crate::util::SQLiteConnectionFactory;
|
||||
@@ -81,9 +81,13 @@ pub async fn push_event(
|
||||
if path_event_type.to_string() != form.event_type {
|
||||
return Err(error::ErrorBadRequest("Inconsistent event type."))
|
||||
}
|
||||
let validation = form.event.validate_event();
|
||||
if validation.is_err() {
|
||||
return Err(error::ErrorBadRequest(validation.err().unwrap()));
|
||||
}
|
||||
let _ = event_channel.send(form.event).await;
|
||||
let ctx = context! {
|
||||
event_type => "bob le chef",
|
||||
event_type => "event received.",
|
||||
};
|
||||
tmpl_env.render("partials/v1/event_processed.html", ctx)
|
||||
}
|
||||
Reference in new issue
Block a user