Skip to content

Commit

Permalink
refactor: Refactor trade view and validation in CLI dialogs
Browse files Browse the repository at this point in the history
- Refactored multiple files to use `TradeView::display` instead of `TradeView::display_trade`
- Added input validation for fee price and quantity fields in `TradeFillDialogBuilder` and `TradeCreateDialog`
- Added an option to show or hide detailed trades information in `TradeSearchDialog`
- Renamed methods in `TradeView` and `TradeOverviewView` for better code organization and readability
- Added error handling and removed unused code snippets in various files.
  • Loading branch information
matiasvillaverde committed Jun 15, 2023
1 parent 4948caa commit 7fab7d5
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Matias",
"Rustfmt",
"rustup",
"shellexpand",
"TSLA",
"Villaverde"
]
Expand Down
2 changes: 1 addition & 1 deletion trust-cli/src/dialogs/trade_create_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl TradeDialogBuilder {
.expect("No result found, did you forget to call build?")
{
Ok(trade) => {
TradeView::display_trade(&trade, &self.account.unwrap().name);
TradeView::display(&trade, &self.account.unwrap().name);
TradeOverviewView::display(&trade.overview);
}
Err(error) => println!("Error creating account: {:?}", error),
Expand Down
2 changes: 1 addition & 1 deletion trust-cli/src/dialogs/trade_exit_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ExitDialogBuilder {
let account_name = self.account.clone().unwrap().name;

println!("Trade exit executed:");
TradeView::display_trade(&self.trade.unwrap(), account_name.as_str());
TradeView::display(&self.trade.unwrap(), account_name.as_str());

println!("With transaction of exit:");
TransactionView::display(&tx_exit, account_name.as_str());
Expand Down
2 changes: 1 addition & 1 deletion trust-cli/src/dialogs/trade_fill_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl FillTradeDialogBuilder {
Ok((trade, tx)) => {
let name = self.account.unwrap().name;
println!("Trade entry executed:");
TradeView::display_trade(&trade, name.as_str());
TradeView::display(&trade, name.as_str());
println!("Trade overview:");
TradeOverviewView::display(&trade.overview);
println!("Transaction:");
Expand Down
2 changes: 1 addition & 1 deletion trust-cli/src/dialogs/trade_funding_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl FundingDialogBuilder {
let account = self.account.clone().unwrap().name;

println!("Trade approved:");
TradeView::display_trade(&trade, &self.account.unwrap().name);
TradeView::display(&trade, &self.account.unwrap().name);

println!("Trade overview:");
TradeOverviewView::display(&trade_overview);
Expand Down
36 changes: 32 additions & 4 deletions trust-cli/src/dialogs/trade_search_dialog.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use trust_core::TrustFacade;
use trust_model::{Account, Status, Trade};

use crate::dialogs::AccountSearchDialog;
use crate::views::TradeView;
use dialoguer::{theme::ColorfulTheme, FuzzySelect};
use crate::{dialogs::AccountSearchDialog, views::TradeOverviewView};
use dialoguer::{theme::ColorfulTheme, Confirm, FuzzySelect};
use std::error::Error;

pub struct TradeSearchDialogBuilder {
account: Option<Account>,
status: Option<Status>,
overview: bool,
result: Option<Result<Vec<Trade>, Box<dyn Error>>>,
}

Expand All @@ -17,6 +18,7 @@ impl TradeSearchDialogBuilder {
TradeSearchDialogBuilder {
result: None,
account: None,
overview: true,
status: None,
}
}
Expand All @@ -31,8 +33,20 @@ impl TradeSearchDialogBuilder {
println!("No trades found");
return;
}
println!("Trades found:");
TradeView::display_trades(trades, self.account.unwrap().name.as_str());

if self.overview {
println!("Trades found:");
let name = self.account.clone().unwrap().name;
for trade in trades {
println!("Trade: {}", trade.id);
TradeView::display(&trade, name.as_str());
println!("Overview:");
TradeOverviewView::display(&trade.overview);
}
} else {
println!("Trades found:");
TradeView::display_trades(trades, self.account.unwrap().name.as_str());
}
}
Err(error) => println!("Error searching account: {:?}", error),
}
Expand Down Expand Up @@ -66,4 +80,18 @@ impl TradeSearchDialogBuilder {
self.status = Some(*status);
self
}

pub fn show_overview(mut self) -> Self {
if Confirm::with_theme(&ColorfulTheme::default())
.with_prompt("Do you want to see details form each trade?")
.default(true)
.interact()
.unwrap()
{
self.overview = true;
} else {
self.overview = false;
}
self
}
}
2 changes: 1 addition & 1 deletion trust-cli/src/dialogs/trade_submit_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl SubmitDialogBuilder {
{
Ok((trade, log)) => {
println!("Trade submitted:");
TradeView::display_trade(&trade, &self.account.unwrap().name);
TradeView::display(&trade, &self.account.unwrap().name);

println!("Trade overview:");
TradeOverviewView::display(&trade.overview);
Expand Down
1 change: 1 addition & 0 deletions trust-cli/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ impl ArgDispatcher {
TradeSearchDialogBuilder::new()
.account(&mut self.trust)
.status()
.show_overview()
.search(&mut self.trust)
.display();
}
Expand Down
2 changes: 1 addition & 1 deletion trust-cli/src/views/trade_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl TradeView {
}
}

pub fn display_trade(a: &Trade, account_name: &str) {
pub fn display(a: &Trade, account_name: &str) {
TradeView::display_trades(vec![a.clone()], account_name);
println!();
}
Expand Down
3 changes: 2 additions & 1 deletion trust-model/src/trade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ impl std::fmt::Display for Trade {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}: quantity: {}, category: {}, currency: {}, safety_stop: {}, entry: {}, target: {}",
"{}: quantity: {}, category: {}, currency: {}, safety_stop: {}, entry: {}, target: {}, status: {}",
self.trading_vehicle.symbol,
self.safety_stop.quantity,
self.category,
self.currency,
self.safety_stop.unit_price,
self.entry.unit_price,
self.target.unit_price,
self.status,
)
}
}
Expand Down

0 comments on commit 7fab7d5

Please sign in to comment.