public async Task <ActionResult> Index() { VATRatesVM viewModel = new VATRatesVM(); try { viewModel = await _VATRatesService.InitializeAsync(); } catch (Exception) { } return(View(viewModel)); }
public static VATRatesVM MapToViewModel(this VATRatesJsonModel model) { if (model == null) { return(null); } VATRatesVM VATRatesViewModel = new VATRatesVM(); List <RateJsonModel> topThreeRates = new List <RateJsonModel>(); List <RateJsonModel> bottomThreeRates = new List <RateJsonModel>(); GetTopAndBottomRates(ref topThreeRates, ref bottomThreeRates, model); MapThreeRatesToViewModel(VATRatesViewModel, topThreeRates, true); MapThreeRatesToViewModel(VATRatesViewModel, bottomThreeRates, false, true); return(VATRatesViewModel); }
private static void MapThreeRatesToViewModel(VATRatesVM VATRatesViewModel, List <RateJsonModel> threeRates, bool topThreeRates = false, bool bottomThreeRates = false) { foreach (var rate in threeRates) { VATRateVM VATRateViewModel = new VATRateVM(); VATRateViewModel.Country = rate.CountryName; VATRateViewModel.StandardRate = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.Standard).FirstOrDefault()); VATRateViewModel.ReducedRate = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.Reduced).FirstOrDefault()); VATRateViewModel.ReducedRate1 = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.Reduced1).FirstOrDefault()); VATRateViewModel.ReducedRate2 = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.Reduced2).FirstOrDefault()); VATRateViewModel.SuperReducedRate = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.SuperReduced).FirstOrDefault()); VATRateViewModel.ParkingRate = Convert.ToInt64(rate.Periods.Select(p => p.PeriodRates.Parking).FirstOrDefault()); if (topThreeRates && !bottomThreeRates) { VATRatesViewModel.TopThreeRates.Add(VATRateViewModel); } else if (!topThreeRates && bottomThreeRates) { VATRatesViewModel.BottomThreeRates.Add(VATRateViewModel); } } if (topThreeRates && !bottomThreeRates) { VATRatesViewModel.TopThreeRates = VATRatesViewModel.TopThreeRates .OrderByDescending(i => i.StandardRate) .ThenByDescending(i => i.ReducedRate != 0 ? i.ReducedRate : i.ReducedRate1) .ThenByDescending(i => i.ReducedRate2) .ThenByDescending(i => i.SuperReducedRate) .ThenByDescending(i => i.ParkingRate) .ToList(); } else if (!topThreeRates && bottomThreeRates) { VATRatesViewModel.BottomThreeRates = VATRatesViewModel.BottomThreeRates .OrderBy(i => i.StandardRate) .ThenBy(i => i.ReducedRate != 0 ? i.ReducedRate : i.ReducedRate1) .ThenBy(i => i.ReducedRate2) .ThenBy(i => i.SuperReducedRate) .ThenBy(i => i.ParkingRate) .ToList(); } }
public async Task <VATRatesVM> InitializeAsync() { VATRatesVM viewModel = new VATRatesVM(); try { HttpResponseMessage response = await client.GetAsync(ConfigurationManager.AppSettings[URI]); response.EnsureSuccessStatusCode(); var responseJson = await response.Content.ReadAsStringAsync(); var VATRatesJsonModel = JsonConvert.DeserializeObject <VATRatesJsonModel>(responseJson); viewModel = VATRatesJsonModel.MapToViewModel(); } catch (Exception) { throw; } return(viewModel); }