public async Task <BasketViewModel> GetBasket(string userId) { var vm = new BasketViewModel() { BuyerId = userId, Items = new List <BasketItemViewModel>(), }; //sepet yoksa boş bir listeli sepet döndür. if (!await _basketService.BasketExistsAsync(userId)) { return(vm); } var basket = await _basketRepository.FirstOrDefaultAsync(new BasketWithItemsSpecification(userId)); var productIds = basket.Items.Select(x => x.ProductId).ToArray(); var basketProducts = await _productRepository.ListAsync(new ProductSpecification(productIds)); vm.Id = basket.Id; vm.Items = basket.Items.Select(x => { var product = basketProducts.First(p => p.Id == x.ProductId); return(new BasketItemViewModel() { Id = x.Id, ProductId = x.ProductId, Quantity = x.Quantity, UnitPrice = x.UnitPrice, ProductName = product.Name, PictureUri = product.PictureUri }); }).ToList(); return(vm); }
public async Task <IActionResult> AddToBasket(int id, int quantity = 1) { // get product details var product = await _basketViewModelService.GetProductDetails(id); // get/create user id string userId = GetOrCreateUserId(); // create basket if not exists and get basket id int basketId; if (!await _basketService.BasketExistsAsync(userId)) { basketId = await _basketService.CreateBasketAsync(userId); } else { basketId = await _basketService.GetBasketIdAsync(userId); } // add item to the basket int count = await _basketService.AddItemToBasketAsync(basketId, product.Id, product.Price, quantity); return(Json(new { BasketItemCount = count })); }