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"
|
maxlength="40"
|
||||||
placeholder="Narnia Dollar"
|
placeholder="Narnia Dollar"
|
||||||
aria-label="Default"
|
aria-label="Default"
|
||||||
name="event.Event.name">
|
name="event.CreateNewSecurity.name">
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
<div class="uk-width-1-4@s">
|
<div class="uk-width-1-4@s">
|
||||||
@@ -16,16 +16,16 @@
|
|||||||
placeholder="ND"
|
placeholder="ND"
|
||||||
maxlength="5"
|
maxlength="5"
|
||||||
aria-label="Default"
|
aria-label="Default"
|
||||||
name="event.Event.symbol">
|
name="event.CreateNewSecurity.symbol">
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="uk-margin">
|
<div class="uk-margin">
|
||||||
<label class="uk-form-label"> Type
|
<label class="uk-form-label"> Type
|
||||||
<select name="event.Event.symbol" class="uk-select">
|
<select name="event.CreateNewSecurity.security_type" class="uk-select">
|
||||||
<option>Currency</option>
|
<option value="currency">Currency</option>
|
||||||
<option>Bond</option>
|
<option value="bond">Bond</option>
|
||||||
</select>
|
</select>
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</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 serde::Serialize;
|
||||||
|
|
||||||
use crate::db::_external_id::{Id, SecurityId};
|
use crate::db::_external_id::{Id, SecurityId};
|
||||||
@@ -11,7 +11,7 @@ pub enum SecurityType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl SecurityType {
|
impl SecurityType {
|
||||||
fn from_string(s: String) -> SecurityType {
|
fn from_string_unsafe(s: String) -> SecurityType {
|
||||||
if s == "currency".to_string() {
|
if s == "currency".to_string() {
|
||||||
return SecurityType::Currency;
|
return SecurityType::Currency;
|
||||||
}
|
}
|
||||||
@@ -20,6 +20,15 @@ impl SecurityType {
|
|||||||
}
|
}
|
||||||
panic!("Impossible to convert string {} to SecurityType.", s);
|
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 {
|
impl ToSql for SecurityType {
|
||||||
@@ -49,7 +58,7 @@ impl Security {
|
|||||||
pub fn from_row(row: &Row) -> Result<Security> {
|
pub fn from_row(row: &Row) -> Result<Security> {
|
||||||
Ok(Security {
|
Ok(Security {
|
||||||
id: row.get("sec_id")?,
|
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")?,
|
symbol: row.get("sec_symbol")?,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -97,7 +106,7 @@ pub fn list_securities(conn: &Connection) -> CustomResult<Vec<Security>> {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_securities {
|
mod test_securities {
|
||||||
use rusqlite::{params, Connection};
|
use rusqlite::{Connection, params};
|
||||||
|
|
||||||
use crate::db::_test_utils::*;
|
use crate::db::_test_utils::*;
|
||||||
use crate::db::users::User;
|
use crate::db::users::User;
|
||||||
|
|||||||
+1
-1
@@ -11,7 +11,7 @@ pub mod external_id {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub mod securities {
|
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 {
|
pub mod users {
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
use rusqlite::{Connection, Transaction};
|
use rusqlite::{Connection, Transaction};
|
||||||
|
|
||||||
|
use crate::db::securities::create_security;
|
||||||
use crate::db::users::create_user;
|
use crate::db::users::create_user;
|
||||||
use crate::events::{CreateNewSecurity, CreateNewUser, Event};
|
use crate::events::{CreateNewSecurity, CreateNewUser, Event};
|
||||||
use crate::util::{CustomResult, Error};
|
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(())
|
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<()> {
|
pub fn process_event(conn: &mut Connection, event: Event) -> CustomResult<()> {
|
||||||
let mut tx = conn.transaction().map_err(Error::SqlError)?;
|
let mut tx = conn.transaction().map_err(Error::SqlError)?;
|
||||||
match event {
|
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)?,
|
Event::CreateNewUser(e) => process_create_new_user_event(&mut tx, e)?,
|
||||||
};
|
};
|
||||||
tx.commit().map_err(Error::SqlError)
|
tx.commit().map_err(Error::SqlError)
|
||||||
|
|||||||
+35
-1
@@ -1,10 +1,35 @@
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
use crate::db::securities::{NewSecurity, SecurityType};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct CreateNewSecurity {
|
pub struct CreateNewSecurity {
|
||||||
|
pub security_type: String,
|
||||||
|
pub symbol: String,
|
||||||
pub name: 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)]
|
#[derive(Deserialize, Debug)]
|
||||||
@@ -18,3 +43,12 @@ pub enum Event {
|
|||||||
CreateNewSecurity(CreateNewSecurity),
|
CreateNewSecurity(CreateNewSecurity),
|
||||||
CreateNewUser(CreateNewUser),
|
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 log::info;
|
||||||
use minijinja::context;
|
use minijinja::context;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use crate::db::users::list_users;
|
|
||||||
|
|
||||||
|
use crate::db::users::list_users;
|
||||||
use crate::events::Event;
|
use crate::events::Event;
|
||||||
use crate::services::ServiceFactory;
|
use crate::services::ServiceFactory;
|
||||||
use crate::util::SQLiteConnectionFactory;
|
use crate::util::SQLiteConnectionFactory;
|
||||||
@@ -81,9 +81,13 @@ pub async fn push_event(
|
|||||||
if path_event_type.to_string() != form.event_type {
|
if path_event_type.to_string() != form.event_type {
|
||||||
return Err(error::ErrorBadRequest("Inconsistent 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 _ = event_channel.send(form.event).await;
|
||||||
let ctx = context! {
|
let ctx = context! {
|
||||||
event_type => "bob le chef",
|
event_type => "event received.",
|
||||||
};
|
};
|
||||||
tmpl_env.render("partials/v1/event_processed.html", ctx)
|
tmpl_env.render("partials/v1/event_processed.html", ctx)
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user