public async Task <OfferListViewModel> GetOfferListAsync(OfferListPart offerListPart) { if (String.IsNullOrEmpty(offerListPart.PopName)) { return(new OfferListViewModel()); } var offers = await _offersApi.GetOffersAsync(offerListPart.PopName).ConfigureAwait(false); return(OffersToOfferListViewModel(offerListPart, offers)); }
private OfferListViewModel OffersToOfferListViewModel(OfferListPart offerListPart, Offers offers) { var offerListViewModel = new OfferListViewModel { Title = offerListPart.Title }; if (offers == null || offers.Offer == null) { return(offerListViewModel); } var offer = offers.Offer.FirstOrDefault(o => o.ProductOffers != null && o.ProductOffers.ProductOffer != null && o.ProductOffers.ProductOffer.Length > 0) ?? offers.Offer[0]; offerListViewModel.Title = offerListPart.Title; offerListViewModel.Id = offer.Id; offerListViewModel.Name = offer.Name; offerListViewModel.Type = offer.Type; offerListViewModel.Image = offer.Image; offerListViewModel.SalesPitch = offer.SalesPitch; if (offer.ProductOffers == null || offer.ProductOffers.ProductOffer == null) { return(offerListViewModel); } if (offer.ProductOffers.ProductOffer.Length > offerListPart.MaxNProducts) { var limitedProductOffers = new ProductOffer[offerListPart.MaxNProducts]; for (var i = 0; i < offerListPart.MaxNProducts; i++) { limitedProductOffers[i] = offer.ProductOffers.ProductOffer[i]; } offerListViewModel.ProductOfferViewModels = limitedProductOffers.Select(po => ProductOfferToProductOfferViewModel(offer.Id, po)).ToArray(); } else { offerListViewModel.ProductOfferViewModels = offer.ProductOffers.ProductOffer.Select(po => ProductOfferToProductOfferViewModel(offer.Id, po)).ToArray(); } return(offerListViewModel); }