public JsonResult EventFeedback(FeedbackEvent model) { try { if (model != null && ModelState.IsValid) { using (var context = new FeedbackContext()) { context.Configuration.LazyLoadingEnabled = true; var userId = User.Id(); var ev = context.Events.FirstOrDefault(e => e.Id == model.EventId); if (ev == null) ModelState.AddModelError("Validation", "Invalid event!"); else if (!ev.Active) ModelState.AddModelError("Validation", "Event has been closed! Feedback is no more allowed!"); if (ModelState.IsValid) { var data = context.FeedbackEvents.FirstOrDefault(f => f.EventId == model.EventId && f.FeedbackUserId == userId); if (data == null) { data = new FeedbackEvent { FeedbackUserId = userId, EventId = model.EventId }; context.FeedbackEvents.Add(data); } var user = context.FeedbackUsers.FirstOrDefault(u => u.Id == userId); if (user == null) { user = new FeedbackUser { Id = userId }; context.FeedbackUsers.Add(user); } if (model.FeedbackUser != null) { user.Name = model.FeedbackUser.Name ?? user.Name; user.Email = model.FeedbackUser.Email ?? user.Email; } data.FeedbackUser = user; data.LikedMost = model.LikedMost ?? data.LikedMost; data.PrimaryTechnology = model.PrimaryTechnology ?? data.PrimaryTechnology; data.PrimaryTechnologyOther = model.PrimaryTechnologyOther ?? data.PrimaryTechnologyOther; data.Suggestions = model.Suggestions ?? data.Suggestions; data.WantedTechnologies = model.WantedTechnologies ?? data.WantedTechnologies; context.SaveChanges(); } } } } catch (Exception ex) { ModelState.AddModelError("Exception", ex); } if (!ModelState.IsValid) return Json(new { Success = false, Message = "There was an error while posting your feedback!", Errors = ModelState.ToJson() }, JsonRequestBehavior.AllowGet); else return Json(new { Success = true, Message = "Thanks for your feedback!" }, JsonRequestBehavior.AllowGet); }
public JsonResult VoteEvent(int eventId, int vote) { int? rating = null; try { if (ModelState.IsValid) { using (var context = new FeedbackContext()) { context.Configuration.LazyLoadingEnabled = true; var userId = User.Id(); var ev = context.Events.FirstOrDefault(e => e.Id == eventId); if (ev == null) ModelState.AddModelError("Validation", "Invalid event!"); else if (!ev.Active) ModelState.AddModelError("Validation", "Event has been closed! Rating is no more allowed!"); if (ModelState.IsValid) { var data = context.FeedbackEvents.FirstOrDefault(f => f.EventId == eventId && f.FeedbackUserId == userId); if (data == null) { data = new FeedbackEvent { FeedbackUserId = userId, EventId = eventId }; context.FeedbackEvents.Add(data); } var user = context.FeedbackUsers.FirstOrDefault(u => u.Id == userId); if (user == null) { user = new FeedbackUser { Id = userId }; context.FeedbackUsers.Add(user); } data.FeedbackUser = user; data.Rating = vote; context.SaveChanges(); rating = vote; } } } } catch (Exception ex) { ModelState.AddModelError("Exception", ex); } if (!ModelState.IsValid) return Json(new { Success = false, Message = "There was an error while posting your vote!", Errors = ModelState.ToJson() }, JsonRequestBehavior.AllowGet); else return Json(new { Success = true, Message = "Thanks for your vote!", Rating = rating }, JsonRequestBehavior.AllowGet); }