|
38 | 38 |
|
39 | 39 | <div class="mat-layout-grid-cell mat-layout-grid-cell-span-3"></div>
|
40 | 40 | <div class="mat-layout-grid-cell mat-layout-grid-cell-span-6">
|
41 |
| - @if (activePortfolio != null) |
| 41 | + @if (ActivePortfolio != null) |
42 | 42 | {
|
43 | 43 | <MatCard class="demo-mat-card">
|
44 | 44 | <MatCardContent>
|
45 | 45 | <div class="demo-mat-card-content">
|
46 | 46 | <MatHeadline6 class="clear-margin">
|
47 | 47 | <MatChipSet Style="align-items: center">
|
48 |
| - <MatH5 Class="clear-margin">@activePortfolio.Name</MatH5> |
49 |
| - <MatChip Style="vertical-align: center" Label="@CurrencyUtils.GetCurrencyLabel(activePortfolio.Currency)"/> |
| 48 | + <MatH5 Class="clear-margin">@ActivePortfolio.Name</MatH5> |
| 49 | + <MatChip Style="vertical-align: center" Label="@CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency)"/> |
50 | 50 | </MatChipSet>
|
51 | 51 | </MatHeadline6>
|
52 | 52 | </div>
|
53 | 53 |
|
54 | 54 | <MatBody2 class="demo-mat-card-content clear-margin">
|
55 | 55 | <div class="mat-layout-grid">
|
56 | 56 | <div class="mat-layout-grid-inner" style="align-items: center">
|
57 |
| - <div class="mat-layout-grid-cell mat-layout-grid-cell-span-6"> |
58 |
| - <MatH4 Class="clear-margin">@(CurrencyUtils.Format(portfolioSummary.MarketValue, activePortfolio.Currency))</MatH4> |
59 |
| - </div> |
60 |
| - <div class="mat-layout-grid-cell mat-layout-grid-cell-span-6" style="text-align: end"> |
61 |
| - @(portfolioSummary.RelativeChange * 100m) % |
62 |
| - </div> |
| 57 | + @if (PortfolioSummary != null) |
| 58 | + { |
| 59 | + <div class="mat-layout-grid-cell mat-layout-grid-cell-span-6"> |
| 60 | + |
| 61 | + <MatH4 Class="clear-margin">@(CurrencyUtils.Format(PortfolioSummary.MarketValue, ActivePortfolio.Currency))</MatH4> |
| 62 | + </div> |
| 63 | + <div class="mat-layout-grid-cell mat-layout-grid-cell-span-6" style="text-align: end"> |
| 64 | + @(PortfolioSummary.RelativeChange * 100m) % |
| 65 | + </div> |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + <MatProgressCircle Indeterminate="true"></MatProgressCircle> |
| 70 | + } |
63 | 71 | </div>
|
64 | 72 | </div>
|
65 | 73 | </MatBody2>
|
66 | 74 | </MatCardContent>
|
67 | 75 | </MatCard>
|
68 |
| - <MatTable Items="@portfolioEntryRows" Striped="true" AllowSelection="true" RowClass="tester" class="mat-elevation-z5" ShowPaging="false" PageSize="9999" SelectionChanged="SelectionChangedEvent"> |
69 |
| - <MatTableHeader> |
70 |
| - <th>Coin</th> |
71 |
| - <th>Price</th> |
72 |
| - <th>Change (1h)</th> |
73 |
| - <th>Holdings</th> |
74 |
| - </MatTableHeader> |
75 |
| - <MatTableRow> |
76 |
| - <td>@context.symbol.ToUpper()</td> |
77 |
| - <td>@(CurrencyUtils.Format(context.currentPrice, activePortfolio.Currency))</td> |
78 |
| - <td style='color: @(context.relativeChange >= 0 ? "#17a104" : "#FF0000")'>@context.relativeChange%</td> |
79 |
| - <td>@context.percentage%</td> |
80 |
| - </MatTableRow> |
81 |
| - </MatTable> |
| 76 | + @if (PortfolioEntryRows != null) |
| 77 | + { |
| 78 | + <MatTable Items="@PortfolioEntryRows" Striped="true" AllowSelection="true" RowClass="tester" class="mat-elevation-z5" ShowPaging="false" PageSize="9999" SelectionChanged="SelectionChangedEvent"> |
| 79 | + <MatTableHeader> |
| 80 | + <th>Coin</th> |
| 81 | + <th>Price</th> |
| 82 | + <th>Change (1h)</th> |
| 83 | + <th>Holdings</th> |
| 84 | + </MatTableHeader> |
| 85 | + <MatTableRow> |
| 86 | + <td>@context.symbol.ToUpper()</td> |
| 87 | + <td>@(CurrencyUtils.Format(context.currentPrice, ActivePortfolio.Currency))</td> |
| 88 | + <td style='color: @(context.relativeChange >= 0 ? "#17a104" : "#FF0000")'>@DecimalUtils.FormatTwoDecimalPlaces(context.relativeChange)%</td> |
| 89 | + <td>@context.percentage%</td> |
| 90 | + </MatTableRow> |
| 91 | + </MatTable> |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + <MatProgressBar Indeterminate="true"></MatProgressBar> |
| 96 | + } |
82 | 97 | }
|
83 | 98 | else
|
84 | 99 | {
|
|
88 | 103 | <div class="mat-layout-grid-cell mat-layout-grid-cell-span-3"></div>
|
89 | 104 | </div>
|
90 | 105 | </div>
|
91 |
| -<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new entry" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{activePortfolio.Id}"); }'></MatFAB> |
| 106 | +<MatFAB Class="app-fab--absolute" Icon="@MatIconNames.Add" Label="Add a new entry" OnClick='() => { NavigationManager.NavigateTo($"newportfolioentry/{ActivePortfolio.Id}"); }'></MatFAB> |
92 | 107 |
|
93 | 108 |
|
94 | 109 | @code
|
95 | 110 | {
|
96 | 111 | [Parameter]
|
97 | 112 | public int PortfolioId { get; set; }
|
98 | 113 |
|
99 |
| - protected Portfolio activePortfolio; |
| 114 | + protected Portfolio ActivePortfolio; |
100 | 115 |
|
101 |
| - protected ISummaryService.Summary portfolioSummary = new(1341m, 1.8m, 9982.489m, 1000m); |
| 116 | + protected ISummaryService.Summary PortfolioSummary = null; |
102 | 117 |
|
103 |
| - protected List<PortfolioEntry> activePortfolioEntries = new List<PortfolioEntry>() |
104 |
| - { |
105 |
| - new("btc", 1, 1), |
106 |
| - new("ada", 1, 2), |
107 |
| - new("eth", 1, 3), |
108 |
| - new("ltc", 1, 4), |
109 |
| - new("link", 1, 5), |
110 |
| - }; |
111 |
| - |
112 |
| - protected List<decimal> portfolioHoldings = new() |
113 |
| - { |
114 |
| - 44.8886m, |
115 |
| - 28.18m, |
116 |
| - 10.116m, |
117 |
| - 9.38m, |
118 |
| - 2.70m, |
119 |
| - }; |
120 |
| - |
121 |
| - protected List<PortfolioEntryRow> portfolioEntryRows = new() |
122 |
| - { |
123 |
| - new("btc", 57644.42m, 1.35m, 44.76m), |
124 |
| - new("ada", 1.36m, 0.58m, 28.18m), |
125 |
| - new("eth", 3279.64m, 10.95m, 27.11m), |
126 |
| - new("ltc", 291.55m, 7.20m, 9.38m), |
127 |
| - new("link", 42.20m, -5.19m, 2.70m) |
128 |
| - }; |
| 118 | + protected List<PortfolioEntry> ActivePortfolioEntries; |
| 119 | + |
| 120 | + protected List<PortfolioEntryRow> PortfolioEntryRows; |
129 | 121 |
|
130 | 122 | protected record PortfolioEntryRow(string symbol, decimal currentPrice, decimal relativeChange, decimal percentage);
|
131 | 123 |
|
132 | 124 | protected override void OnInitialized()
|
133 | 125 | {
|
134 |
| - activePortfolio = PortfolioService.GetPortfolio(PortfolioId); |
135 |
| - activePortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId); |
| 126 | + ActivePortfolio = PortfolioService.GetPortfolio(PortfolioId); |
| 127 | + ActivePortfolioEntries = PortfolioEntryService.GetPortfolioEntries(PortfolioId); |
| 128 | + _loadEntryInfo(); |
136 | 129 | }
|
137 | 130 |
|
138 |
| - protected override async Task OnInitializedAsync() |
| 131 | + |
| 132 | + private async void _loadEntryInfo() |
139 | 133 | {
|
140 |
| - var marketEntries = (await CryptoStatsSource.GetMarketEntries( |
141 |
| - CurrencyUtils.GetCurrencyLabel(activePortfolio.Currency).ToLower(), |
142 |
| - await Task.WhenAll(activePortfolioEntries.Select(async entry => (await CryptoNameResolver.Resolve(entry.Symbol)).ToLower())) |
143 |
| - )).ToDictionary(entry => entry.Symbol, entry => entry); |
| 134 | + // resolve names of all portfolio entries |
| 135 | + var portfolioEntriesNames = await Task.WhenAll(ActivePortfolioEntries.Select(async entry => (await CryptoNameResolver.Resolve(entry.Symbol)).ToLower())); |
144 | 136 |
|
| 137 | + // fetch market entries of all entries of the portfolio |
| 138 | + var marketEntries = await CryptoStatsSource.GetMarketEntries( |
| 139 | + CurrencyUtils.GetCurrencyLabel(ActivePortfolio.Currency).ToLower(), portfolioEntriesNames |
| 140 | + ); |
145 | 141 |
|
146 |
| - List<ISummaryService.Summary> summaries = new List<ISummaryService.Summary>(); |
147 |
| - foreach (var portfolioEntry in activePortfolioEntries) |
148 |
| - { |
149 |
| - var marketEntry = marketEntries.GetValueOrDefault(portfolioEntry.Symbol); |
| 142 | + // create a dictionary where a symbol is mapped to a market entry |
| 143 | + var symbolsToMarketEntries = marketEntries.ToDictionary(entry => entry.Symbol, entry => entry); |
150 | 144 |
|
151 |
| - if (marketEntry == null) |
| 145 | + // compute portfolio entry summaries |
| 146 | + var entrySummaries = ActivePortfolioEntries.Select( |
| 147 | + portfolioEntry => |
152 | 148 | {
|
153 |
| - // TODO market entry is null |
| 149 | + // find the evaluation of the entry's asset |
| 150 | + var marketEntry = symbolsToMarketEntries.GetValueOrDefault(portfolioEntry.Symbol); |
| 151 | + |
| 152 | + if (marketEntry == null) |
| 153 | + { |
| 154 | + // TODO market entry is null |
| 155 | + } |
| 156 | + |
| 157 | + // fetch all orders of the currently iterated portfolio entry |
| 158 | + var entryMarketOrders = MarketOrderService.GetPortfolioEntryOrders(portfolioEntry.Id); |
| 159 | + |
| 160 | + // compute the summary of the entry based on market orders |
| 161 | + return SummaryService.GetPortfolioEntrySummary(entryMarketOrders, marketEntry.CurrentPrice); |
154 | 162 | }
|
155 |
| - var entryOrders = MarketOrderService.GetPortfolioEntryOrders(portfolioEntry.Id); |
156 |
| - summaries.Add(SummaryService.GetPortfolioEntrySummary(entryOrders, marketEntry.CurrentPrice)); |
157 |
| - } |
158 |
| - portfolioSummary = SummaryService.GetPortfolioSummary(summaries); |
| 163 | + ).ToList(); |
| 164 | + |
| 165 | + // compute portfolio's summary based on summaries of it's entries |
| 166 | + PortfolioSummary = SummaryService.GetPortfolioSummary(entrySummaries); |
159 | 167 |
|
160 |
| - if (portfolioSummary.Cost == 0) |
| 168 | + // if the cost of the summary is zero, set the relative change to zero |
| 169 | + if (PortfolioSummary.Cost == 0) |
161 | 170 | {
|
162 |
| - portfolioSummary = portfolioSummary with{ |
| 171 | + PortfolioSummary = PortfolioSummary with { |
163 | 172 | RelativeChange = 0
|
164 |
| - }; |
| 173 | + }; |
165 | 174 | }
|
166 | 175 |
|
167 |
| - var totalMarketValue = summaries.Sum(summary => summary.MarketValue); |
168 |
| - |
169 |
| - |
170 |
| - portfolioEntryRows = summaries.Zip(activePortfolioEntries).Select((summary) => new PortfolioEntryRow(summary.Second.Symbol, marketEntries[summary.Second.Symbol].CurrentPrice, new decimal(marketEntries[summary.Second.Symbol].PriceChangePercentage24H), totalMarketValue > 0 ? (summary.First.MarketValue / totalMarketValue) * 100 : 0)).ToList(); |
| 176 | + // compute the total value of the portfolio by summing market values of all entries |
| 177 | + var portfolioTotalMarketValue = entrySummaries.Sum(summary => summary.MarketValue); |
| 178 | + |
| 179 | + // create portfolio entry table rows |
| 180 | + PortfolioEntryRows = entrySummaries.Zip(ActivePortfolioEntries).Select( |
| 181 | + (tuple) => new PortfolioEntryRow( |
| 182 | + // symbol of the portfolio entry |
| 183 | + tuple.Second.Symbol, |
| 184 | + // current price of the entry's asset |
| 185 | + symbolsToMarketEntries[tuple.Second.Symbol].CurrentPrice, |
| 186 | + // asset's price change since the last 24h |
| 187 | + new decimal(symbolsToMarketEntries[tuple.Second.Symbol].PriceChangePercentage24H), |
| 188 | + // percentage within the portfolio |
| 189 | + portfolioTotalMarketValue > 0 ? (tuple.First.MarketValue / portfolioTotalMarketValue) * 100 : 0) |
| 190 | + ).ToList(); |
| 191 | + |
| 192 | + StateHasChanged(); |
171 | 193 | }
|
172 | 194 |
|
173 | 195 | public void SelectionChangedEvent(object row)
|
174 | 196 | {
|
175 |
| - if (row == null) |
176 |
| - { |
177 |
| - } |
178 |
| - else |
| 197 | + if (row != null) |
179 | 198 | {
|
180 | 199 | NavigationManager.NavigateTo($"entrydetail");
|
181 | 200 | }
|
|
0 commit comments