Skip to content

Commit 46caf52

Browse files
committed
Added comments to PortfolioDetail.razor
1 parent 2e18180 commit 46caf52

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed
Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
@page "/portfolios/{portfolioId:int}"
2-
@using Model
32
@using Services
43
@using Utils
54
@using CryptoStatsSource
6-
@using CryptoStatsSource.model
7-
@using System.Diagnostics
8-
@inject Microsoft.AspNetCore.Components.NavigationManager NavigationManager
5+
@using Model
6+
@inject NavigationManager NavigationManager
97
@inject IPortfolioService PortfolioService
108
@inject IPortfolioEntryService PortfolioEntryService
119
@inject IMarketOrderService MarketOrderService;
@@ -31,44 +29,35 @@
3129
bottom: 1rem;
3230
right: 1rem;
3331
}
34-
35-
.mat-paper {
36-
display: flex;
37-
flex-direction: column;
38-
justify-content: center;
39-
align-items: center;
40-
padding: 1em;
41-
}
4232
</style>
4333
<div class="mat-layout-grid mat-layout-grid-align-center">
4434
<div class="mat-layout-grid-inner center">
45-
4635
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
4736
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-8">
4837
<MatH5><MatButton Outlined="true" Icon="keyboard_arrow_left" Style="margin-right: 1rem;" OnClick='() => { NavigationManager.NavigateTo($""); }'>Back</MatButton>Portfolio Detail</MatH5>
49-
@if (ActivePortfolio != null)
38+
@if (_activePortfolio != null)
5039
{
5140
<MatCard class="demo-mat-card">
5241
<MatCardContent>
5342
<div class="demo-mat-card-content">
5443
<MatHeadline6 class="clear-margin">
5544
<MatChipSet Style="align-items: center">
56-
<MatH5 Class="clear-margin">@ActivePortfolio.Name</MatH5>
57-
<MatChip Style="vertical-align: center" Label="@CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency)"/>
45+
<MatH5 Class="clear-margin">@_activePortfolio.Name</MatH5>
46+
<MatChip Style="vertical-align: center" Label="@CurrencyUtils.GetCurrencyLabel(_activePortfolio.Currency)"/>
5847
</MatChipSet>
5948
</MatHeadline6>
60-
<span style="padding-left: 4px;">@ActivePortfolio.Description</span>
49+
<span style="padding-left: 4px;">@_activePortfolio.Description</span>
6150
</div>
6251
<MatBody2 class="demo-mat-card-content clear-margin">
6352
<div class="mat-layout-grid">
6453
<div class="mat-layout-grid-inner" style="align-items: center">
65-
@if (PortfolioSummary != null)
54+
@if (_portfolioSummary != null)
6655
{
6756
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-6">
68-
<LabelDecimalValue Label="Market Value" Value="@(CurrencyUtils.Format(PortfolioSummary.MarketValue, ActivePortfolio.Currency))"></LabelDecimalValue>
57+
<LabelDecimalValue Label="Market Value" Value="@(CurrencyUtils.Format(_portfolioSummary.MarketValue, _activePortfolio.Currency))"></LabelDecimalValue>
6958
</div>
7059
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-6" style="text-align: end">
71-
<LabelDecimalValue Positive="PortfolioSummary.RelativeChange >= 0" ValueColorBasedOnValue="true" Label="Total Change" Value="@(DecimalUtils.FormatTwoDecimalPlacesWithPlusSign(PortfolioSummary.RelativeChange * 100m) + "%")"></LabelDecimalValue>
60+
<LabelDecimalValue Positive="_portfolioSummary.RelativeChange >= 0" ValueColorBasedOnValue="true" Label="Total Change" Value="@(DecimalUtils.FormatTwoDecimalPlacesWithPlusSign(_portfolioSummary.RelativeChange * 100m) + "%")"></LabelDecimalValue>
7261
</div>
7362
}
7463
else
@@ -81,13 +70,13 @@
8170
</MatCardContent>
8271
</MatCard>
8372

84-
@if (PortfolioEntryRows == null)
73+
@if (_portfolioEntryRows == null)
8574
{
8675
<MatProgressBar Indeterminate="true"></MatProgressBar>
8776
}
88-
else if (PortfolioEntryRows.Count > 0)
77+
else if (_portfolioEntryRows.Count > 0)
8978
{
90-
<MatTable Items="@PortfolioEntryRows" Striped="true" AllowSelection="true" RowClass="tester" class="mat-elevation-z5" ShowPaging="false" PageSize="9999" SelectionChanged="SelectionChangedEvent">
79+
<MatTable Items="@_portfolioEntryRows" Striped="true" AllowSelection="true" RowClass="tester" class="mat-elevation-z5" ShowPaging="false" PageSize="9999" SelectionChanged="SelectionChangedEvent">
9180
<MatTableHeader>
9281
<th>Coin</th>
9382
<th>Price</th>
@@ -97,18 +86,23 @@
9786
<MatTableRow>
9887
<td>@context.Symbol.ToUpper()</td>
9988
<td>
100-
<div style="min-width: 8rem">@(CurrencyUtils.Format(context.CurrentPrice, ActivePortfolio.Currency))</div>
89+
<div style="min-width: 8rem">@(CurrencyUtils.Format(context.CurrentPrice, _activePortfolio.Currency))</div>
10190
</td>
10291
<td style='color: @(context.RelativeChange >= 0 ? "#17a104" : "#FF0000"); min-width: 7rem;'>
10392
<div style="min-width: 6rem">@DecimalUtils.FormatTwoDecimalPlaces(context.RelativeChange)%</div>
10493
</td>
105-
<td>@(CurrencyUtils.Format(context.MarketValue, ActivePortfolio.Currency)) (@(DecimalUtils.FormatTwoDecimalPlaces(context.Percentage))%)</td>
94+
<td>@(CurrencyUtils.Format(context.MarketValue, _activePortfolio.Currency)) (@(DecimalUtils.FormatTwoDecimalPlaces(context.Percentage))%)</td>
10695
</MatTableRow>
10796
</MatTable>
10897
}
10998
else
11099
{
111-
<MatPaper Elevation="2"><span>No portfolio entries were found.</span><div><MatButton Style="margin-top:1em;" Label="Create a new portfolio entry" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{ActivePortfolio.Id}");}'></MatButton></div></MatPaper>
100+
<MatPaper Elevation="2">
101+
<span>No portfolio entries were found.</span>
102+
<div>
103+
<MatButton Style="margin-top:1em;" Label="Create a new portfolio entry" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{_activePortfolio.Id}"); }'></MatButton>
104+
</div>
105+
</MatPaper>
112106
}
113107
}
114108
else
@@ -119,28 +113,28 @@
119113
<div class="mat-layout-grid-cell mat-layout-grid-cell-span-2"></div>
120114
</div>
121115
</div>
122-
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Settings" Label="Manage portfolio entries" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{ActivePortfolio.Id}"); }'></MatFAB>
116+
<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Settings" Label="Manage portfolio entries" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{_activePortfolio.Id}"); }'></MatFAB>
123117

124118

125119
@code
126120
{
127121
[Parameter]
128122
public int PortfolioId { get; set; }
129123

130-
protected Portfolio ActivePortfolio;
124+
private Portfolio _activePortfolio;
131125

132-
protected ISummaryService.Summary PortfolioSummary = null;
126+
private ISummaryService.Summary _portfolioSummary;
133127

134-
protected List<PortfolioEntry> ActivePortfolioEntries;
128+
private List<PortfolioEntry> _activePortfolioEntries;
135129

136-
protected List<PortfolioEntryRow> PortfolioEntryRows;
130+
private List<PortfolioEntryRow> _portfolioEntryRows;
137131

138132
protected record PortfolioEntryRow(string Symbol, decimal CurrentPrice, decimal RelativeChange, decimal Percentage, decimal AbsoluteChange, decimal MarketValue, int EntryId);
139133

140134
protected override void OnInitialized()
141135
{
142-
ActivePortfolio = PortfolioService.GetPortfolio(PortfolioId);
143-
ActivePortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
136+
_activePortfolio = PortfolioService.GetPortfolio(PortfolioId);
137+
_activePortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId);
144138
_loadEntryInfo();
145139
}
146140

@@ -150,55 +144,49 @@
150144
// resolve names of all portfolio entries
151145
await CryptocurrencyResolver.Refresh();
152146
var portfolioCryptocurrencyEntries = await Task.WhenAll(
153-
ActivePortfolioEntries.Select(
147+
_activePortfolioEntries.Select(
154148
async entry => (await CryptocurrencyResolver.Resolve(entry.Symbol)))
155149
);
156150

157151
// fetch market entries of all entries of the portfolio
158152
var marketEntries = await CryptoStatsSource.GetMarketEntries(
159-
CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency).ToLower(), portfolioCryptocurrencyEntries.Select(c => c.Id).ToArray()
153+
CurrencyUtils.GetCurrencyLabel(_activePortfolio.Currency).ToLower(), portfolioCryptocurrencyEntries.Select(c => c.Id).ToArray()
160154
);
161155

162156
// create a dictionary where a symbol is mapped to a market entry
163157
var symbolsToMarketEntries = marketEntries.ToDictionary(entry => entry.Symbol, entry => entry);
164158

165-
// TODO fix low low low market cap non existing altcoins
166159
// compute portfolio entry summaries
167-
var entrySummaries = ActivePortfolioEntries.Select(
160+
var entrySummaries = _activePortfolioEntries.Select(
168161
portfolioEntry =>
169162
{
170163
// find the evaluation of the entry's asset
171164
var marketEntry = symbolsToMarketEntries.GetValueOrDefault(portfolioEntry.Symbol);
172165

173-
if (marketEntry == null)
174-
{
175-
// TODO market entry is null
176-
}
177-
178166
// fetch all orders of the currently iterated portfolio entry
179167
var entryMarketOrders = MarketOrderService.GetPortfolioEntryOrders(portfolioEntry.Id);
180-
168+
181169
// compute the summary of the entry based on market orders
182170
return SummaryService.GetPortfolioEntrySummary(entryMarketOrders, marketEntry.CurrentPrice);
183171
}
184172
).ToList();
185173

186174
// compute portfolio's summary based on summaries of it's entries
187-
PortfolioSummary = SummaryService.GetPortfolioSummary(entrySummaries);
175+
_portfolioSummary = SummaryService.GetPortfolioSummary(entrySummaries);
188176

189177
// if the cost of the summary is zero, set the relative change to zero
190-
if (PortfolioSummary.Cost == 0)
178+
if (_portfolioSummary.Cost == 0)
191179
{
192-
PortfolioSummary = PortfolioSummary with {
180+
_portfolioSummary = _portfolioSummary with {
193181
RelativeChange = 0
194-
};
182+
};
195183
}
196184

197185
// compute the total value of the portfolio by summing market values of all entries
198186
var portfolioTotalMarketValue = entrySummaries.Sum(summary => summary.MarketValue);
199187

200188
// create portfolio entry table rows
201-
PortfolioEntryRows = entrySummaries.Zip(ActivePortfolioEntries).Select(
189+
_portfolioEntryRows = entrySummaries.Zip(_activePortfolioEntries).Select(
202190
tuple => new PortfolioEntryRow(
203191
// symbol of the portfolio entry
204192
tuple.Second.Symbol,
@@ -216,16 +204,17 @@
216204
tuple.Second.Id
217205
)
218206
).ToList();
219-
207+
208+
// update the UI
220209
StateHasChanged();
221210
}
222211

223212
public void SelectionChangedEvent(object row)
224213
{
225214
if (row != null)
226215
{
216+
// entry row has been clicked, open it's detail
227217
NavigationManager.NavigateTo($"entries/{((PortfolioEntryRow) row).EntryId}");
228218
}
229219
}
230-
231220
}

0 commit comments

Comments
 (0)