| | | 1 | | using Microsoft.AspNetCore.Mvc.ViewFeatures; |
| | | 2 | | using Services.PriceСlculationServices; |
| | | 3 | | |
| | | 4 | | namespace OzonOrdersWeb.WebServices.Currency; |
| | | 5 | | |
| | | 6 | | public sealed class CurrencyRatesViewBagService : ICurrencyRatesViewBagService |
| | | 7 | | { |
| | | 8 | | private readonly ICurrencyRateFetcher _currencyRateFetcher; |
| | | 9 | | |
| | 0 | 10 | | public CurrencyRatesViewBagService(ICurrencyRateFetcher currencyRateFetcher) |
| | | 11 | | { |
| | 0 | 12 | | _currencyRateFetcher = currencyRateFetcher; |
| | 0 | 13 | | } |
| | | 14 | | |
| | | 15 | | public CurrencyRateFetchTasks StartFetch() => |
| | 0 | 16 | | new( |
| | 0 | 17 | | _currencyRateFetcher.GetUSDRateAsync(), |
| | 0 | 18 | | _currencyRateFetcher.GetEURRateAsync(), |
| | 0 | 19 | | _currencyRateFetcher.GetBYNRateAsync()); |
| | | 20 | | |
| | | 21 | | public Task<CurrencyRatesSnapshot> GetRatesAsync(CancellationToken cancellationToken = default) => |
| | 0 | 22 | | StartFetch().ToSnapshotAsync(); |
| | | 23 | | |
| | | 24 | | public void ApplyToViewData(ViewDataDictionary viewData, CurrencyRatesSnapshot rates) |
| | | 25 | | { |
| | 0 | 26 | | viewData["RateUSD"] = rates.RateUsd; |
| | 0 | 27 | | viewData["RateEUR"] = rates.RateEur; |
| | 0 | 28 | | viewData["RateBYN"] = rates.RateByn; |
| | 0 | 29 | | } |
| | | 30 | | |
| | | 31 | | public async Task SetViewBagAsync(ViewDataDictionary viewData, CancellationToken cancellationToken = default) |
| | | 32 | | { |
| | 0 | 33 | | var rates = await GetRatesAsync(cancellationToken); |
| | 0 | 34 | | ApplyToViewData(viewData, rates); |
| | 0 | 35 | | } |
| | | 36 | | } |