Skip to content

Commit 94a4d5d

Browse files
committed
Added the ability to create new market orders.
1 parent a6266d6 commit 94a4d5d

File tree

4 files changed

+39
-19
lines changed

4 files changed

+39
-19
lines changed

Services/Services/SummaryService.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public ISummaryService.Summary GetMarketOrderSummary(MarketOrder order, decimal
2323
{
2424
var marketValue = order.Size * assetPrice;
2525
var cost = (order.Size * order.FilledPrice) + order.Fee;
26+
if (cost == 0)
27+
{
28+
return new(0, 0, 0, 0);
29+
}
30+
2631
var relativeChange = (marketValue / cost) - new decimal(1);
2732
var absoluteChange = marketValue - cost;
2833
return new(absoluteChange, relativeChange, marketValue, cost);
@@ -41,18 +46,19 @@ public ISummaryService.Summary GetAverageOfSummaries(IEnumerable<ISummaryService
4146
totalCost += summary.Cost;
4247
totalAbsoluteChange += summary.AbsoluteChange;
4348
}
44-
49+
4550
if (totalCost == 0)
4651
{
47-
return new ISummaryService.Summary(0,0,0,0);
48-
}
52+
return new ISummaryService.Summary(0, 0, 0, 0);
53+
}
4954

50-
decimal totalRelativeChange = (totalMarketValue / totalCost) -1m;
55+
decimal totalRelativeChange = (totalMarketValue / totalCost) - 1m;
5156

5257
return new(totalAbsoluteChange, totalRelativeChange, totalMarketValue, totalCost);
5358
}
5459

55-
public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfolioEntryOrders, decimal assetPrice)
60+
public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfolioEntryOrders,
61+
decimal assetPrice)
5662
{
5763
decimal totalHoldingSize = 0;
5864
decimal totalSellValue = 0;
@@ -77,8 +83,8 @@ public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfo
7783

7884
if (totalCost == 0)
7985
{
80-
return new ISummaryService.Summary(0,0,0,0);
81-
}
86+
return new ISummaryService.Summary(0, 0, 0, 0);
87+
}
8288

8389
decimal currentTotalHoldingValue = totalHoldingSize * assetPrice;
8490

@@ -89,10 +95,8 @@ public ISummaryService.Summary GetPortfolioEntrySummary(List<MarketOrder> portfo
8995
totalCost + totalFee);
9096
}
9197

92-
public ISummaryService.Summary GetPortfolioSummary(List<ISummaryService.Summary> portfolioEntrySummaries) => GetAverageOfSummaries(portfolioEntrySummaries);
98+
public ISummaryService.Summary GetPortfolioSummary(List<ISummaryService.Summary> portfolioEntrySummaries) =>
99+
GetAverageOfSummaries(portfolioEntrySummaries);
93100

94-
public class AssetPriceNotFoundException : Exception
95-
{
96-
}
97101
}
98102
}

WebFrontend/Pages/NewMarketOrder.razor

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
@page "/newmarketorder"
1+
@page "/newmarketorder/{entryId:int}"
22
@using Model
33
@using Services
44
@using Utils
55
@using System.ComponentModel.DataAnnotations
66
@inject IPortfolioService PortfolioService
7+
@inject IPortfolioEntryService PortfolioEntrySerivce
8+
@inject IMarketOrderService MarketOrderService
79
@inject IMatDialogService MatDialogService
810
@inject IMatToaster Toaster
911
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
@@ -26,6 +28,7 @@
2628
<div class="mat-layout-grid">
2729
<div class="mat-layout-grid-inner">
2830
<div class="mat-layout-grid-cell">
31+
<MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-bottom: 1rem;" OnClick='() => { NavigationManager.NavigateTo($"/entries/{ActiveEntry.Id}"); }'>Back</MatButton>
2932
<MatCard>
3033
<MatCardContent class="demo-mat-card-content">
3134
<h2>Create a new market order</h2>
@@ -79,6 +82,9 @@
7982

8083
@code
8184
{
85+
[Parameter]
86+
public int EntryId { get; set; }
87+
8288
protected Portfolio ActivePortfolio = new("", "", Currency.Usd);
8389
protected PortfolioEntry ActiveEntry = new("btc", 1);
8490
protected NewOrderModel FormModel = new();
@@ -122,6 +128,12 @@
122128
}
123129
}
124130

131+
protected override void OnInitialized()
132+
{
133+
ActiveEntry = PortfolioEntrySerivce.GetPortfolioEntry(EntryId);
134+
ActivePortfolio = PortfolioService.GetPortfolio(ActiveEntry.PortfolioId);
135+
}
136+
125137
protected override async Task OnInitializedAsync()
126138
{
127139
}
@@ -138,7 +150,8 @@
138150

139151
private void OnCreateOrderFormSubmit()
140152
{
141-
Console.WriteLine(FormModel.OrderDate.ToLongDateString());
142-
Toaster.Add("New order successfully added", MatToastType.Warning, "", "");
153+
MarketOrderService.CreateMarketOrder(FormModel.FilledPrice, FormModel.Fee, FormModel.Size, FormModel.OrderDate, true, ActiveEntry.Id);
154+
FormModel.Reset();
155+
Toaster.Add("New order successfully added", MatToastType.Success, "", "");
143156
}
144157
}

WebFrontend/Pages/PortfolioDetail.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
private async void _loadEntryInfo()
133133
{
134134
// resolve names of all portfolio entries
135+
await CryptoNameResolver.Refresh();
135136
var portfolioEntriesNames = await Task.WhenAll(
136137
ActivePortfolioEntries.Select(
137138
async entry => (await CryptoNameResolver.Resolve(entry.Symbol)).ToLower())
@@ -202,7 +203,7 @@
202203
{
203204
if (row != null)
204205
{
205-
NavigationManager.NavigateTo($"entrydetail/{((PortfolioEntryRow) row).EntryId}");
206+
NavigationManager.NavigateTo($"entries/{((PortfolioEntryRow) row).EntryId}");
206207
}
207208
}
208209

WebFrontend/Pages/PortfolioEntryDetail.razor

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/entrydetail/{entryId:int}"
1+
@page "/entries/{entryId:int}"
22
@using Model
33
@using Services
44
@using Utils
@@ -124,7 +124,7 @@
124124
<td>@CurrencyUtils.Format(context.Item1.Fee, ActivePortfolio.Currency)</td>
125125
<td>
126126
<MatIconButton Icon="edit" OnClick="EditMarketOrder"></MatIconButton>
127-
<MatIconButton Icon="delete" OnClick="DeletePortfolio"></MatIconButton>
127+
<MatIconButton Icon="delete" OnClick="() => DeletePortfolio(context.Item1)"></MatIconButton>
128128
</td>
129129
</MatTableRow>
130130
</MatTable>
@@ -133,7 +133,7 @@
133133
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
134134
</div>
135135
</div>
136-
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new order"></MatFAB>
136+
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new order" OnClick='() => { NavigationManager.NavigateTo($"newmarketorder/{ActivePortfolioEntry.Id}");}'></MatFAB>
137137

138138

139139
@code
@@ -195,11 +195,13 @@
195195
NavigationManager.NavigateTo($"editorder");
196196
}
197197

198-
async void DeletePortfolio(MouseEventArgs e)
198+
async void DeletePortfolio(MarketOrder order)
199199
{
200200
var result = await MatDialogService.ConfirmAsync("Do you really wish to delete this market order?");
201201
if (result)
202202
{
203+
// TODO update summary
204+
MarketOrderService.DeleteMarketOrder(order);
203205
}
204206
}
205207
}

0 commit comments

Comments
 (0)