public void confirmOrRemoveFavoritedProduct(int itemId) { string userId = _userService.GetUserId(); var temp = _unitOfWork.FavoritedProductRepository.findByItemIdAndUserId(itemId, userId); if (temp == null) { FavoritedProduct favoritedProduct = new FavoritedProduct() { ItemId = itemId, UserId = userId }; _unitOfWork.FavoritedProductRepository.Create(favoritedProduct); } else { _unitOfWork.FavoritedProductRepository.Delete(temp); } _unitOfWork.SaveChanges(); }
public async Task <IActionResult> Details(int id, int starNumber = 0, int ratingPage = 1) { var userId = _userService.GetUserId(); var menuItemFromDb = await _db.MenuItem.Include(m => m.Category).Include(m => m.SubCategory).Where(m => m.Id == id).FirstOrDefaultAsync(); var listStar = _db.Ratings.Where(a => a.MenuItemId == id).Include(a => a.ApplicationUser).OrderByDescending(p => p.Id).ToList(); StringBuilder param = new StringBuilder(); var listRatingByStarNumber = listStar.ToList(); if (starNumber == 0) { listRatingByStarNumber = listStar.ToList(); } else if (starNumber > 0) { listRatingByStarNumber = listRatingByStarNumber.Where(a => a.RatingStar == starNumber).ToList(); } param.Append("/Customer/Home/Details/" + id + "?starNumber=" + starNumber + "&ratingPage=:"); FavoritedProduct favoritedProduct = new FavoritedProduct(); if (userId != null) { var favor = _db.FavoritedProducts.FirstOrDefault(a => a.ItemId == id && a.UserId == userId); if (favor != null) { favoritedProduct.ItemId = favor.ItemId; favoritedProduct.UserId = favor.UserId; } } ProductStar productStar = new ProductStar(); if (listStar.Count > 0) { productStar.totalOneStar = listStar.Where(a => a.RatingStar == 1).Count(); productStar.totalTwoStar = listStar.Where(a => a.RatingStar == 2).Count(); productStar.totalThreeStar = listStar.Where(a => a.RatingStar == 3).Count(); productStar.totalFourStar = listStar.Where(a => a.RatingStar == 4).Count(); productStar.totalFiveStar = listStar.Where(a => a.RatingStar == 5).Count(); productStar.averageStar = Math.Round(listStar.Average(a => a.RatingStar), 1); } else { productStar.totalOneStar = 0; productStar.totalTwoStar = 0; productStar.totalThreeStar = 0; productStar.totalFourStar = 0; productStar.totalFiveStar = 0; productStar.averageStar = 0; } //Convert Enum Color -> String ViewBag.itemColor = Enum.GetName(typeof(MenuItem.EColor), Convert.ToInt32(menuItemFromDb.Color)); var highLimitPrice = menuItemFromDb.Price + (menuItemFromDb.Price * 25 / 100); var lowLimitPrice = menuItemFromDb.Price - (menuItemFromDb.Price * 25 / 100); var similarPriceProducts = _db.MenuItem.Take(4); MenuItemsAndQuantity menuItemsAndQuantity = new MenuItemsAndQuantity() { Item = menuItemFromDb, Quantity = 1, News = await _db.News.Where(n => n.MenuItemId == id).FirstOrDefaultAsync(), SimilarProducts = similarPriceProducts.ToList(), ExistedRatings = listRatingByStarNumber.Skip((ratingPage - 1) * PageSize).Take(PageSize).ToList(), ProductStar = productStar, FavoritedProduct = favoritedProduct }; var count = listRatingByStarNumber.Count(); menuItemsAndQuantity.PagingInfo = new PagingInfo() { CurrentPage = ratingPage, ItemsPerPage = PageSize, TotalItem = count, urlParam = param.ToString() }; ViewBag.starNumber = starNumber; ViewBag.pageSize = this.PageSize; return(View(menuItemsAndQuantity)); }