public IActionResult CreateComment([Bind("Id,GymId,UserId,CommentText,Rate,PublicationDate")] Comment comment) { comment.PublicationDate = DateTime.Now; if (ModelState.IsValid) { _comRepo.Add(comment); var gym = _gymRepo.Get(comment.GymId); var comments = gym.Comments; int sumRate = 0; for (int i = 0; i < comments.Count; i++) { sumRate += comments[i].Rate; } double rateGym = sumRate / comments.Count; int rate = (int)Math.Round(rateGym); gym.GymRate = rate; try { _gymRepo.Edit(gym); } catch (DbUpdateException e) { Console.WriteLine(e.InnerException.Message); ModelState.AddModelError("0", e.InnerException.Message); return(BadRequest(ModelState)); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); return(BadRequest($"{e.Message}")); } return(RedirectToAction($"{comment.GymId}")); } else if (comment.UserId == null) { return(RedirectToAction("Login", "Account")); } else { return(RedirectToAction($"{comment.GymId}", "GymPage", "error")); } }
public IActionResult Edit(int id, [Bind("Id,GymName,GymRate,GymLocation,Region,MbrshipPrice,GymArea,FoundYear,Facilities,Url,Description,Latitude,Longitude,GymImgUrl")] Gym gym) { if (id != gym.Id) { return(NotFound()); } if (ModelState.IsValid) { try { var file = Request.Form.Files.First(); if (file != null) { var originalFilename = ContentDispositionHeaderValue .Parse(file.ContentDisposition) .FileName .Trim('"'); if (!string.IsNullOrEmpty(originalFilename)) { string filePath = UploadImage(file, originalFilename); gym.GymImgUrl = !string.IsNullOrEmpty(filePath) ? filePath : ""; } } _gymRepo.Edit(gym); } catch (DbUpdateConcurrencyException) { if (!GymExists(gym.Id)) { return(NotFound()); } throw; } return(RedirectToAction("Index")); } ViewData["Regions"] = new SelectList(SelectLookups.Regions); return(View("Views/Admin/Gym/Edit.cshtml", gym)); }