public async Task <IActionResult> UserRateAsync(int?id) { if (id == null) { return(NotFound()); } var workShop = await _context.WorkShops.FindAsync(id); if (workShop == null) { return(NotFound()); } var userRate = new WorkshopRate { WorkShopId = workShop.Id }; return(PartialView("_UserRatePartial", userRate)); }
public async Task <IActionResult> UserRate(WorkshopRate WorkshopRate) { var workShop = await _context.WorkShops.FindAsync(WorkshopRate.WorkShopId); try { var userID = _httpContext.HttpContext.User.FindFirstValue(ClaimTypes.NameIdentifier); var userprofile = _context.UserProfiles.Where(p => p.UserId == userID).FirstOrDefault(); WorkshopRate.UserProfileId = userprofile.Id; _context.WorkshopRates.Add(WorkshopRate); //calculate rate var Rates = await _context.WorkshopRates.Where(w => w.WorkShopId == WorkshopRate.WorkShopId).ToListAsync(); double rate = 0; foreach (var item in Rates) { rate += (double)item.Rate; } double total = rate / Rates.Count; workShop.Rate = (double)total; await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { id = workShop.Id })); } catch (Exception) { return(RedirectToAction("Details", new { id = workShop.Id })); } }