public ActionResult Edit(int fromId, EditHypothermiaViewModel hypothermiaViewModel) { if (!ModelState.IsValid) { return(View(hypothermiaViewModel)); } //Check that the tracking is not closed if (_patientService.IsClosed(fromId)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var updated = _hypothermiaService.Update(hypothermiaViewModel); //Update failed because of a wrong id if (!updated) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } //Update successful, log operation and redirect to view hypothermias _hypothermiaService.Log(OperationType.HypothermiaUpdate, User.Identity.GetUserId <int>(), patientId: fromId, data: "HypothermiaID: " + hypothermiaViewModel.Id); return(RedirectToAction("View", "Hypothermia", new { id = fromId })); }
public void Create_RepeatedTimeSlot() { //Arrange var viewModel = new EditHypothermiaViewModel { TimeSlot = TimeSlot.H72, CnsUs = CnsUltrasound.Edema, AEeg = AElectroencephalogram.Convulsion, Eeg = Electroencephalogram.Slow, Convulsion = false, Cr = CerebralResonance.CorpusCallosumInjury, CnsExploration = new CnsExplorationViewModel(), Analysis = new AnalysisViewModel(), ModelState = new ModelStateDictionary() }; var patientId = _dataContext.Patients.First().Id; _hypothermiaService.Create(viewModel, patientId); //Act _hypothermiaService.Create(viewModel, patientId); var hypothermias = _hypothermiaService.FindByPatientId(patientId); //Assert Assert.That(viewModel.ModelState.IsValid, Is.False); Assert.That(hypothermias.Count, Is.EqualTo(1)); }
public ActionResult Create(int fromId, EditHypothermiaViewModel hypothermiaViewModel) { if (!ModelState.IsValid) { return(View(hypothermiaViewModel)); } //Check that the tracking is not closed if (_patientService.IsClosed(fromId)) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } hypothermiaViewModel.ModelState = ModelState; _hypothermiaService.Create(hypothermiaViewModel, fromId); //Check validation again for repeated time slots if (!ModelState.IsValid) { return(View(hypothermiaViewModel)); } _hypothermiaService.Log(OperationType.HypothermiaCreate, User.Identity.GetUserId <int>(), fromId); return(RedirectToAction("View", "Hypothermia", new { id = fromId })); }
public void Update() { //Arrange var hypothermia = _dataContext.Hypothermias .Include(h => h.CnsExploration) .Include(h => h.Analysis) .First(); var viewModel = new EditHypothermiaViewModel(hypothermia) { Id = hypothermia.Id, Convulsion = true, CnsExploration = { Reflexes = Reflexes.Irregular }, Analysis = { Cpk = 20 } }; //Act var updated = _hypothermiaService.Update(viewModel); hypothermia = _hypothermiaService.FindById(viewModel.Id); //Assert Assert.That(updated, Is.True); Assert.That(hypothermia.Convulsion, Is.True); Assert.That(hypothermia.CnsExploration.Reflexes, Is.EqualTo(Reflexes.Irregular)); Assert.That(hypothermia.Analysis.Cpk, Is.EqualTo(20)); }
public void POST_Edit_Success() { //Arrange var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.NameIdentifier, "123"), }); var principal = new ClaimsPrincipal(identity); var context = new Mock <HttpContextBase>(); context.SetupGet(x => x.User).Returns(principal); var routeData = new RouteData(); _controller.ControllerContext = new ControllerContext(context.Object, routeData, _controller); var viewModel = new EditHypothermiaViewModel(); _patientService.Setup(x => x.IsClosed(It.IsAny <int>())).Returns(false); _hypothermiaService.Setup(x => x.Update(It.IsAny <EditHypothermiaViewModel>())).Returns(true); _hypothermiaService.Setup(x => x.Log(It.IsAny <OperationType>(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string>())); //Act var result = (RedirectToRouteResult)_controller.Edit(1, viewModel); //Assert Assert.That(result.RouteValues["action"], Is.EqualTo("View")); }
/// <summary> /// Save a new Hypothermia and it's related CnsExploration to the database with the data from /// a viewmodel. If the TimeSlot is repeated, an error will be added to the modelstate of the /// viewmodel and the operation will not be completed. /// </summary> /// <param name="viewModel">Viewmodel with the new hypothermia data.</param> /// <param name="patientId">Id of the patient for which the hypothermia is being created.</param> public void Create(EditHypothermiaViewModel viewModel, int patientId) { //Check that the time slot for the hypothermia is not repeated var repeated = Repository.Hypothermias .Where(e => e.PatientId == patientId) .Any(e => e.TimeSlot == viewModel.TimeSlot); //If repeated show error to the user if (repeated) { viewModel.ModelState.AddModelError("TimeSlot", Strings.ErrorRepeated); } else { //Create a new Hypothermia from the viewmodel var hypothermia = viewModel.ToNewModel(); hypothermia.PatientId = patientId; //Create the associated CnsExploration var explorationId = _cnsExplorationService.Create(viewModel.CnsExploration); hypothermia.CnsExplorationId = explorationId; //Create the associated Analysis var analysisId = _analysisService.Create(viewModel.Analysis); hypothermia.AnalysisId = analysisId; Repository.Hypothermias.Add(hypothermia); Save(); } }
public void POST_Create_Closed() { //Arrange var viewModel = new EditHypothermiaViewModel(); _patientService.Setup(x => x.IsClosed(It.IsAny <int>())).Returns(true); //Act var result = (HttpStatusCodeResult)_controller.Create(1, viewModel); //Assert Assert.That(result.StatusCode, Is.EqualTo((int)HttpStatusCode.BadRequest)); }
public void POST_Create_InvalidModel() { //Arrange var viewModel = new EditHypothermiaViewModel(); _controller.ModelState.AddModelError("Error", @"ModelError"); //Act var result = (ViewResult)_controller.Create(1, viewModel); //Assert Assert.That(result.ViewName, Is.Empty); }
public void Update_NotFound() { //Arrange var viewModel = new EditHypothermiaViewModel { Id = -123 }; //Act var updated = _hypothermiaService.Update(viewModel); //Assert Assert.That(updated, Is.False); }
/// <summary> /// Update the Hypothermia with the data from the given viewmodel. /// </summary> /// <param name="viewModel">Viewmodel with the modified data.</param> /// <returns>bool indicating if the update was successful.</returns> public bool Update(EditHypothermiaViewModel viewModel) { var hypothermia = Repository.Hypothermias.SingleOrDefault(e => e.Id == viewModel.Id); if (hypothermia == null) { return(false); } //Update the hypothermia with the viewmodel data viewModel.ToModel(hypothermia); Repository.Entry(hypothermia).State = EntityState.Modified; //Update associated CnsExploration _cnsExplorationService.Update(viewModel.CnsExploration); //Update associated Analysis _analysisService.Update(viewModel.Analysis); Save(); return(true); }