public async Task <IActionResult> Index() { BasketDto basket; var viewModel = new ShoppingCartViewModel(); //var cartExists = await _IRestClient.GetAsync<CartExistModel>($"{baseUrl}/BasketExists/{_cookieLogic.GetBasketId()}"); //var data = cartExists.Data; //if (!data.CartExists) //{ // return RedirectToAction("Index", "Home"); //} var response = await _IRestClient.GetAsync <BasketDto>($"{baseUrl}/Basket/{_cookieLogic.GetBasketId()}"); basket = response.Data; if (basket.CartItems.Count == 0) { // Cart is empty, remove cart, and navigate to home _cookieLogic.RemoveBasketId(); return(RedirectToAction("Index", "Home")); } ViewBag.CartCount = basket.ItemCount; ViewBag.CartSummary = string.Join("\n", basket.CartItems.Select(c => c.Name).Distinct()); viewModel.CartItems = basket.CartItems; viewModel.CartTotal = basket.CartTotal; // Return the view return(View(viewModel)); }
public async Task <IViewComponentResult> InvokeAsync() { BasketDto basket; var shoppingCartId = _cookieLogic.GetBasketId(); if (!string.IsNullOrEmpty(shoppingCartId)) { var response = await _IRestClient.GetAsync <BasketSummaryDto>($"{baseUrl}/BasketSummary/{_cookieLogic.GetBasketId()}"); if (response.Data != null) { //var response = // await _IRestClient.GetAsync<BasketDto>($"{baseUrl}/Basket/{_cookieLogic.GetBasketId()}"); ViewBag.CartCount = response.Data.ItemCount; ViewBag.CartSummary = response.Data.ProductNames; } else { // If shopping basket ID from cookie has no shopping cart, remove the basket ID _cookieLogic.RemoveBasketId(); } } //basket = await _cartService.GetCart() ?? new BasketDto(); return(View()); }
public async Task <IActionResult> AddressAndPayment( [FromForm] CheckoutDto checkoutDto, CancellationToken requestAborted) { if (!ModelState.IsValid) { return(View(checkoutDto)); } var formCollection = await HttpContext.Request.ReadFormAsync(); try { //if (string.Equals(formCollection["PromoCode"].FirstOrDefault(), PromoCode, StringComparison.OrdinalIgnoreCase) == false) return View(checkoutDto); // Abbreviate user first and last name to create the username checkoutDto.Username = $"{checkoutDto.FirstName.Substring(0, 2)}-{checkoutDto.LastName.Substring(0, 3)}"; checkoutDto.BasketId = _cookieLogic.GetBasketId(); var response = await _IRestClient.PostAsync <BasketSummaryDto>($"{baseUrl}/checkout", checkoutDto); if (response.HttpResponseMessage.IsSuccessStatusCode) { // Order is successful remove shopping basket _cookieLogic.RemoveBasketId(); } //await _IRestClient.DeleteAsync($"{baseUrl}/{checkoutDto.BasketId}"); //_cookieLogic.SetBasketId(); _logger.LogInformation($"User {checkoutDto.Username} started checkout of {checkoutDto.OrderId}."); TempData[ToastrMessage.Success] = "Thank you for your order"; TempData["BuyerEmail"] = checkoutDto.Email; TempData["BasketId"] = response.Data.BasketId; TempData["CorrelationId"] = response.Data.CorrelationId; //return RedirectToAction("index", "Home"); return(View("OrderPlaced")); } catch { ModelState.AddModelError("", "An error occured whil processing order"); //Invalid - redisplay with errors return(View(checkoutDto)); } }