merge retrieve_portfolio.rs with retrieval_portfolio.rs
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
499b38a17c
commit
ff5148df75
|
@ -1,14 +1,15 @@
|
|||
use log::warn;
|
||||
|
||||
use crate::common::account::portfolio::Portfolio;
|
||||
use crate::common::account::position::Position;
|
||||
use crate::common::message::message::Message;
|
||||
use crate::common::message::message_builder::message_builder;
|
||||
use crate::common::message::message_type::MessageType;
|
||||
use crate::common::misc::return_flags::ReturnFlags;
|
||||
use crate::common::message::inst::DataTransferInst;
|
||||
use crate::common::account::portfolio::Portfolio;
|
||||
use crate::common::account::position::Position;
|
||||
use crate::common::misc::assert_msg::assert_msg;
|
||||
|
||||
use crate::server::db::initializer::db_connect;
|
||||
use crate::server::network::jwt_wrapper::verify_jwt_token;
|
||||
use crate::server::db::initializer::db_connect;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
|
@ -17,14 +18,50 @@ use tokio_rustls::server::TlsStream;
|
|||
pub async fn acc_retrieve_portfolio(
|
||||
tls_connection: &mut TlsStream<TcpStream>,
|
||||
message: &Message,
|
||||
) -> Result<(), ReturnFlags> {
|
||||
) -> std::io::Result<()> {
|
||||
/* assert recieved message */
|
||||
if !assert_msg(
|
||||
message,
|
||||
MessageType::Command,
|
||||
true,
|
||||
1,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
) && message.instruction == DataTransferInst::GetUserPortfolio as i64
|
||||
&& message.data.len() != 0
|
||||
{
|
||||
warn!("RETRIEVE_PORTFOLIO_INVALID_MESSAGE");
|
||||
return tls_connection.shutdown().await;
|
||||
}
|
||||
|
||||
/* verify JWT token */
|
||||
let token = match verify_jwt_token(bincode::deserialize(&message.data).unwrap()) {
|
||||
Ok(token) => token,
|
||||
Err(_) => {
|
||||
warn!("ACC_RETRIEVE_PORTFOLIO_UNAUTH_TOKEN");
|
||||
tls_connection.shutdown().await.unwrap();
|
||||
return Err(ReturnFlags::ServerAccUnauthorized);
|
||||
let server_response = message_builder(
|
||||
MessageType::ServerReturn,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
bincode::serialize(&"Password Incorrect").unwrap(),
|
||||
);
|
||||
match tls_connection
|
||||
.write_all(&bincode::serialize(&server_response).unwrap())
|
||||
.await
|
||||
{
|
||||
_ => {
|
||||
// TODO: do we shutdown connection or do we let handle_data caller do it's
|
||||
// thing
|
||||
tls_connection.shutdown().await.unwrap();
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -33,8 +70,7 @@ pub async fn acc_retrieve_portfolio(
|
|||
std::env::var("DB_PORTFOLIO_USER").unwrap(),
|
||||
std::env::var("DB_PORTFOLIO_PASS").unwrap(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| ReturnFlags::ServerRetrievePortfolioFailed)?;
|
||||
.await?;
|
||||
|
||||
/* get userId's portfolio positions */
|
||||
let mut portfolio: Portfolio = Portfolio::default();
|
||||
|
@ -71,10 +107,14 @@ pub async fn acc_retrieve_portfolio(
|
|||
0,
|
||||
bincode::serialize(&portfolio).unwrap(),
|
||||
);
|
||||
let _ = tls_connection
|
||||
match tls_connection
|
||||
.write_all(&bincode::serialize(&message).unwrap())
|
||||
.await
|
||||
.expect("could not write to client");
|
||||
|
||||
Ok(())
|
||||
.await {
|
||||
Ok(()) => Ok(()),
|
||||
Err(err) => {
|
||||
// Log issue in writing to client
|
||||
warn!("Could not write to cient! With Error: {}\nIgnoring...", err);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
pub async fn db_connect(
|
||||
user: String,
|
||||
pass: String,
|
||||
) -> Result<tokio_postgres::Client, tokio_postgres::Error> {
|
||||
) -> std::io::Result<tokio_postgres::Client> {
|
||||
/* Generate the requested string */
|
||||
let db_connect_str = format!(
|
||||
"host={} port={} dbname={} user={} password={}",
|
||||
|
@ -28,7 +28,14 @@ pub async fn db_connect(
|
|||
pass
|
||||
);
|
||||
let (client, connection) =
|
||||
tokio_postgres::connect(db_connect_str.as_str(), tokio_postgres::NoTls).await?;
|
||||
tokio_postgres::connect(db_connect_str.as_str(), tokio_postgres::NoTls)
|
||||
.await
|
||||
.map_err(|err| {
|
||||
std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
format!("{}", err)
|
||||
)
|
||||
})?;
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = connection.await {
|
||||
eprintln!("SQL connection error: {}", e);
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
pub mod retrieve_portfolio;
|
||||
pub mod retrieve_transactions;
|
||||
|
|
|
@ -1,50 +0,0 @@
|
|||
use log::warn;
|
||||
use std::io;
|
||||
|
||||
use crate::common::message::inst::DataTransferInst;
|
||||
use crate::common::message::message::Message;
|
||||
use crate::common::message::message_type::MessageType;
|
||||
use crate::common::misc::assert_msg::assert_msg;
|
||||
use crate::common::misc::return_flags::ReturnFlags;
|
||||
|
||||
use crate::server::account::retrieval_portfolio::acc_retrieve_portfolio;
|
||||
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio_rustls::server::TlsStream;
|
||||
|
||||
pub async fn retrieve_portfolio(
|
||||
tls_connection: &mut TlsStream<TcpStream>,
|
||||
message: &Message,
|
||||
) -> std::io::Result<()> {
|
||||
/* assert recieved message */
|
||||
if !assert_msg(
|
||||
message,
|
||||
MessageType::Command,
|
||||
true,
|
||||
1,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
false,
|
||||
0,
|
||||
) && message.instruction == DataTransferInst::GetUserPortfolio as i64
|
||||
&& message.data.len() != 0
|
||||
{
|
||||
warn!("RETRIEVE_PORTFOLIO_INVALID_MESSAGE");
|
||||
return tls_connection.shutdown().await;
|
||||
}
|
||||
|
||||
/* call acc_retrieve_portfolio() server version */
|
||||
match acc_retrieve_portfolio(tls_connection, message).await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
warn!("RETRIEVE_PORTFOLIO_FAILED: {}", err);
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("{}: {}", ReturnFlags::ServerRetrievePortfolioFailed, err),
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,8 +7,8 @@ use crate::common::message::message_type::MessageType;
|
|||
|
||||
use crate::server::account::authorization::acc_auth;
|
||||
use crate::server::account::creation::acc_create;
|
||||
use crate::server::account::retrieval_portfolio::acc_retrieve_portfolio;
|
||||
|
||||
use crate::server::network::cmd::retrieve_portfolio::retrieve_portfolio;
|
||||
use crate::server::network::cmd::retrieve_transactions::retrieve_transactions;
|
||||
|
||||
//use tokio::net::TcpStream;
|
||||
|
@ -123,7 +123,7 @@ pub async fn handle_data(
|
|||
acc_auth(sql_conn, socket, &client_msg).await
|
||||
}
|
||||
_ if client_msg.instruction == DataTransferInst::GetUserPortfolio as i64 => {
|
||||
retrieve_portfolio(socket, &client_msg).await
|
||||
acc_retrieve_portfolio(socket, &client_msg).await
|
||||
}
|
||||
_ if client_msg.instruction == DataTransferInst::GetUserTransactionHist as i64 => {
|
||||
retrieve_transactions(sql_conn, socket, &client_msg).await
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
## To do
|
||||
|
||||
- create correct modules
|
||||
* [ ] fix weird file naming
|
||||
* [ ] fix namespace naming
|
||||
- data implmentation stuff
|
||||
* [ ] implement buy & sell
|
||||
* [ ] impl on client
|
||||
|
@ -18,6 +20,7 @@
|
|||
## In progress
|
||||
|
||||
- server move network code to somewhere plausable.
|
||||
* [ ] move assert_msg to message namespace
|
||||
|
||||
## Done
|
||||
|
||||
|
|
Loading…
Reference in New Issue