public ActionResult Create(DebriefingHeaderViewModel dhvm) { // Model level validations if (ModelState.IsValidField("DepartureDate") && ModelState.IsValidField("ReturnDate")) { if (dhvm.ReturnDate.CompareTo(dhvm.DepartureDate) < 0) { // This will prevent ModelState.IsValid from returning true ModelState["ReturnDate"].Errors.Add("Return date can't be before departure date"); } } if (ModelState.IsValid) { // Try and save Debriefing debrief = new Debriefing(); debrief.FillFromHeader(dhvm); // TODO Down the road, this method will only be available to authorized users // For now, add a debug value. debrief.EnteredBy = User.Identity == null ? "*****@*****.**" : User.Identity.Name; debrief.EnteredDate = DateTime.Now; // Add empty evaluations that are appropriate for version and gear code // Do it here instead of FillFromHeader because we'll need access to the database // to come up with the questions. var questions = QuestionsForDebrief(debrief); foreach (var question in questions) { debrief.AddEvaluation(new Evaluation() { QuestionNumber = question.Number }); } // Only start the transaction after we've done what we can without it. using (UnitOfWork uow = DebriefDataService.StartTransaction()) { debrief.FillDependentObjects(dhvm, uow.Session); var writeRepo = new DebriefRepository<Debriefing>(uow.Session); try { writeRepo.Add(debrief); uow.Commit(); } catch (Exception ex) { Flash("Failed to save debriefing. Please contact technical support."); LOGGER.Error("Failed to save debriefing", ex); return View(dhvm); } } return RedirectToAction("Index"); } return View(dhvm); }
public ActionResult Edit(int id, DebriefingHeaderViewModel dhvm) { using (UnitOfWork uow = DebriefDataService.StartTransaction()) { var writeRepo = new DebriefRepository<Debriefing>(uow.Session); var debrief = writeRepo.FindBy(id); if (null == debrief) { return View("NotFound"); } debrief.FillFromHeader(dhvm); // If the version or gear code changes, there's the possibility that any existing // answers are invalid. Either the question numbers have changed or the form types // are different (no way to give PS-1 form feedback for a long line trip!) // This is handled by clearing out any existing evaluations and re-creating the list based // on the new gear code and version. // If there is no existing user input, this happens without any required user interaction. // If there _is_ user input, then we require a positive action to delete the existing data. if (debrief.GearCode != dhvm.GearCode || debrief.Version.Value.ToString() != dhvm.Version) { bool hasAnswers = debrief.HasFilledEvaluation(); if (!hasAnswers || (hasAnswers && "YES".Equals(dhvm.PositiveActionResponse, StringComparison.InvariantCultureIgnoreCase))) { // Delete existing evaluations debrief.Evaluations.Clear(); // Create new set of evaluations var questions = QuestionsForDebrief(debrief); foreach (var question in questions) { debrief.AddEvaluation(new Evaluation() { QuestionNumber = question.Number }); } } else { Flash("Change to gear type and/or version can't be made without a positive action"); return View(dhvm); } } // Fill in full blown objects debrief.FillDependentObjects(dhvm, uow.Session); try { writeRepo.Update(debrief); uow.Commit(); } catch (Exception ex) { Flash("Failed to update briefing. Please contact IT"); LOGGER.Error(String.Format("Failed to update briefing with Id {0}", id), ex); return View(dhvm); } } return RedirectToAction("Index"); }