Ejemplo n.º 1
0
        /// <summary>
        /// Adds to basket.
        /// </summary>
        /// <param name="laptop">The laptop.</param>
        /// <returns></returns>
        public async Task <BasketViewModel> AddToBasket(BasketItem laptop)
        {
            // Add new laptop and its configuration into basket.
            var basketItems = await _laptopRepository.AddToBasket(laptop);

            // laptops in basket
            var laptops = basketItems.Select(basket => basket.LaptopId).Distinct().ToList();

            // create basket view model
            var laptopList = await _laptopRepository.GetLaptopList();

            var laptopConfigurationList = await _laptopRepository.GetConfigurationList();

            var basketViewModel = new BasketViewModel();

            laptops.ForEach(laptopId =>
            {
                var laptopConfigurations = basketItems.Where(b => b.LaptopId == laptopId)
                                           .Select(basket => basket.LaptopConfigurationId).ToList();
                basketViewModel.BasketItems.Add(new BasketItems
                {
                    Laptop = laptopList.FirstOrDefault(l => l.Id == laptopId),
                    LaptopConfigurations = laptopConfigurationList.Where(lc => laptopConfigurations.Contains(lc.Id))
                                           .ToList()
                });
            });

            return(basketViewModel);
        }