public IActionResult FinishOrder() { var shoppingCartId = this.HttpContext.Session.GetShoppingCartId(); var items = this.shoppingCartManager.GetItems(shoppingCartId); var itemsWithDetails = this.GetCartItems(items); var order = new Order { UserId = this.users.GetUserId(User), TotalPrice = itemsWithDetails.Sum(i => i.Price * i.Quantity) }; foreach (var item in itemsWithDetails) { order.Items.Add(new OrderItem { SupplementId = item.SupplementId, SupplementPrice = item.Price, Quantity = item.Quantity }); } this.db.Add(order); this.db.SaveChanges(); shoppingCartManager.Clear(shoppingCartId); return(RedirectToAction(nameof(HomeController.Index), "Home")); }
public IActionResult FinishOrder() { var shoppingCartId = this.HttpContext.Session.GetShoppingCartId(); var items = this.shoppingCartManager.GetItems(shoppingCartId); var itemsWithDetails = this.GetCartItems(items); var customer = this.shoppingCartManager.CurrentUser(this.userManager.GetUserId(User)); var order = new Order { CustomerId = customer.Id, Address = customer.Address, TotalPrice = itemsWithDetails.Sum(i => i.Price * i.Quantity) }; foreach (var item in itemsWithDetails) { order.Products.Add(new ProductOrder { ProductId = item.ProductId, ProductPrice = item.Price, Quantity = item.Quantity, }); } this.db.Add(order); this.db.SaveChanges(); shoppingCartManager.Clear(shoppingCartId); return(RedirectToAction(nameof(HomeController.Index), "Home")); }
public async Task <IActionResult> FinishOrderConfirm(OrderFormModel model) { if (!ModelState.IsValid) { return(View(model)); } var shoppingCartId = this.HttpContext.Session.GetShoppingCartId(); var items = this.shoppingCartManager.GetItems(shoppingCartId); if (items.Count() == 0) { return(BadRequest()); } var itemsWithDetails = await this.GetCartItems(items); var userId = this.userManager.GetUserId(User); await this.orders.CreateOrderAsync(userId, model.Address, itemsWithDetails); shoppingCartManager.Clear(shoppingCartId); TempData.AddSuccessMessage($"Your order has been successfully listed!"); return(RedirectToPage("/Orders/UserOrders", new { id = userId })); }
public async Task <IActionResult> OnPost(string returnUrl = null) { await signInManager.SignOutAsync(); logger.LogInformation("User logged out."); var shoppingCartId = this.HttpContext.Session.GetShoppingCartId(); shoppingCartManager.Clear(shoppingCartId); if (returnUrl != null) { return(LocalRedirect(returnUrl)); } else { return(Page()); } }
public IActionResult FinishOrder(OrderFormModel model) { var shoppingCartId = this.HttpContext.Session.GetShoppingCartId(); var items = this.shoppingCartManager.GetItems(shoppingCartId); if (!items.Any()) { TempData.AddErrorMessage("За да направите покупка, първо добавете продукти в количката."); return(RedirectToAction("Items", nameof(ShoppingCart))); } var userId = this.userManager.GetUserId(User); this.orders.Create(model.Address, model.Comment, items, userId); shoppingCartManager.Clear(shoppingCartId); TempData.AddSuccessMessage("Успешна покупка."); return(RedirectToAction("List")); }