add: create_company

This commit is contained in:
realaltffour 2020-07-25 14:57:36 +03:00
parent d42410ded3
commit 4d888a9954
No known key found for this signature in database
GPG Key ID: C1265D839D44DCB1
4 changed files with 41 additions and 3 deletions

View File

@ -1,11 +1,21 @@
use libtrader::initializer::libtrader_init;
use libtrader::db::cmd::create_company::create_company;
use libtrader::ds::generic::company::Company;
use libtrader::ds::server::global_state::GlobalState;
fn main() {
match libtrader_init() {
Ok(state) => println!("inited state: {:?}", state),
Err(err) => println!("Failed with error: {}", err),
let mut state: GlobalState = match libtrader_init() {
Ok(state) => {println!("inited state: {:?}\n", state); state},
Err(err) => panic!("Failed with error: {}", err),
};
let company: Company = Company::default();
match create_company(&mut state, company) {
Ok(()) => println!("created company"),
Err(err) => panic!("Failed to create company with error: {}", err),
}
println!("Hello World!");
println!("state: {:?}\n", state);
}

View File

@ -0,0 +1,23 @@
use crate::db::init::db_connect;
use crate::ds::server::global_state::GlobalState;
use crate::ds::generic::company::Company;
pub fn create_company(state: &mut GlobalState, company: Company) -> Result<(), String> {
// Connect to database.
let mut client = db_connect(&state)?;
// Insert argument company into public.companies database table.
match client.execute(
"INSERT INTO public.companies VALUES ($1,$2, $3, $4, $5, $6, $7, $8, $9)",
&[&company.id, &company.symbol, &company.isin, &company.company_name,
&company.primary_exchange, &company.sector, &company.industry,
&company.primary_sic_code, &company.employees]) {
#[allow(unused_variables)] /* We do not need the number of rows modified. */
Ok(row) => {
// add company to state
state.companies.insert(company.symbol.to_string(), company);
Ok(())
},
Err(error) => Err(format!("Failed to create company with error: {}", error))
}
}

View File

@ -0,0 +1,4 @@
pub mod create_company;
pub mod get_company;
pub mod create_stock;
pub mod get_stock;

View File

@ -1,2 +1,3 @@
pub mod config;
pub mod init;
pub mod cmd;