public IActionResult AddItem(ProductViewInputModel inputModel) { if (!this.ModelState.IsValid) { this.TempData["Message"] = CART_INVALID_ITEM; this.TempData["MessageType"] = AlertMessageTypes.Error; return(this.RedirectToAction("View", $"Products", new { id = inputModel.Id })); } var cart = new SessionCartDto(); if (this.sessionService.TryGet(this.HttpContext.Session, GlobalConstants.SessionCartKey)) { cart = this.sessionService.Get <SessionCartDto>(this.HttpContext.Session, GlobalConstants.SessionCartKey); } var cartProductModel = this.mapper.Map <SessionCartProductDto>(inputModel); cart.Products.Add(cartProductModel); this.sessionService.Set(this.HttpContext.Session, GlobalConstants.SessionCartKey, cart); this.TempData["Message"] = CART_ADD_PRODUCT; this.TempData["MessageType"] = AlertMessageTypes.Success; return(this.RedirectToAction("View", $"Products", new { id = inputModel.Id, })); }
public CartItemsNumberResponseModel GetNumberOfCartItems() { var cart = this.sessionService.Get <SessionCartDto>(this.HttpContext.Session, GlobalConstants.SessionCartKey); if (cart == null) { cart = new SessionCartDto(); } int numberOfItems = cart.Products.Count; var response = new CartItemsNumberResponseModel() { NumberOfItems = numberOfItems, }; return(response); }