public IActionResult Index(CreateUnloggedUserOrderViewModel model) { var cartModel = SessionHelper.GetObjectFromJson <List <ShoppingCartProductViewModel> >(HttpContext.Session, "cart"); var cart = this.mapper.Map <List <Product> >(cartModel); if (cart.Count() > 0) { ViewBag.cart = cart; ViewBag.total = cart.Sum(p => p.Quantity * p.Price); } return(this.View(model)); }
public IActionResult Create(CreateUnloggedUserOrderViewModel model) { var cart = this.mapper.Map <List <Product> >(SessionHelper.GetObjectFromJson <List <ShoppingCartProductViewModel> >(HttpContext.Session, "cart")); var userId = GetUserId(); if (userId == null) { //When the user is not logged in he will need to add additional information for the order. var user = this.mapper.Map <ApplicationUser>(model); this.usersService.CreateUser(user); this.usersService.Save(); this.CreateOrder(cart, user.Id); } else { //When the user is logged in he wont need to write additional information like name address. this.CreateOrder(cart, userId); } return(this.RedirectToAction("Index", "ShoppingCart")); }