public async Task NewNegativeTest(ApplicationUser user, NegativeTest test) { if (!user.AtRisk) { throw new Exception("User not currently at risk"); } test.ApplicationUserId = user.Id; if (test.TestDate > Time.Now()) { throw new ModelException("Test can't be more recent than present date", (ModelState => { ModelState.AddModelError("TestDate", "La fecha de realización del test no puede ser posterior a la fecha actual."); }) ); } if (test.TestDate < user.TimeOfLastCondition) { throw new ModelException("Test should be more recent than last time put into risk", (ModelState => { ModelState.AddModelError("TestDate", "La fecha de realización del test debe ser posterior a la última vez que fue puesto en riesgo."); }) ); } _context.Add(test); await _context.SaveChangesAsync(); await _userInfoManager.UpdateStatus(user, InfectionStatus.Healthy, null); }
public async Task <IActionResult> NegativeTest([Bind("Id,TestDate")] NegativeTest negativeTest) { var currentUser = await _userInfoManager.FindUser(User); try { await _infectionManager.NewNegativeTest(currentUser, negativeTest); } catch (ModelException ex) { ex.UpdateModelState(ModelState); ViewBag.TimeOfLastCondition = currentUser.TimeOfLastCondition; return(View(negativeTest)); } return(RedirectToAction(nameof(Index))); }