public async Task <IActionResult> AddToWishList(int?id) { if (User.Identity.IsAuthenticated) { if (id == null) { return(NotFound()); } Product product = await _context.Products.FindAsync(id); if (product == null) { return(NotFound()); } List <WishlListVM> viewModel = new List <WishlListVM>(); if (Request.Cookies["wishlist"] == null) { viewModel = new List <WishlListVM>(); } else { viewModel = JsonConvert.DeserializeObject <List <WishlListVM> >(Request.Cookies["wishlist"]); } WishlListVM isExist = viewModel.FirstOrDefault(wp => wp.Id == id && wp.UserName == User.Identity.Name); if (isExist == null) { WishlListVM wl = new WishlListVM() { Id = product.Id, Price = product.Price, Image = product.Image, ProductCode = product.WatchCode, Model = product.Model, StatusCount = product.Count, UserName = User.Identity.Name, }; viewModel.Add(wl); } Response.Cookies.Append("wishlist", JsonConvert.SerializeObject(viewModel)); return(Json(new { icon = "success", message = "Istək siyahısına Əlavə Olundu!" })); } else { return(Json(new { icon = "error", message = "Yalnız sayt istifadəçiləri bu prosesdən istifadə edə bilər..." })); } }
public IActionResult RemoveFromWishList(int?id) { if (id == null) { return(NotFound()); } List <WishlListVM> wishlist = new List <WishlListVM>(); if (Request.Cookies["wishlist"] != null) { wishlist = JsonConvert.DeserializeObject <List <WishlListVM> >(Request.Cookies["wishlist"]); } WishlListVM deletedProduct = wishlist.FirstOrDefault(p => p.Id == id); if (deletedProduct == null) { return(NotFound()); } wishlist.Remove(deletedProduct); Response.Cookies.Append("wishlist", JsonConvert.SerializeObject(wishlist)); return(PartialView("_WishListPartial", wishlist)); }