public async Task <IActionResult> UpdateEncounterType(long id, [FromBody] EncounterTypeForUpdateDto encounterTypeForUpdate) { if (encounterTypeForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); return(BadRequest(ModelState)); } if (Regex.Matches(encounterTypeForUpdate.EncounterTypeName, @"[a-zA-Z ']").Count < encounterTypeForUpdate.EncounterTypeName.Length) { ModelState.AddModelError("Message", "EncounterType name contains invalid characters (Enter A-Z, a-z, space)"); return(BadRequest(ModelState)); } if (_unitOfWork.Repository <EncounterType>().Queryable(). Where(l => l.Description == encounterTypeForUpdate.EncounterTypeName && l.Id != id) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); return(BadRequest(ModelState)); } var encounterTypeFromRepo = await _encounterTypeRepository.GetAsync(f => f.Id == id); if (encounterTypeFromRepo == null) { return(NotFound()); } if (ModelState.IsValid) { encounterTypeFromRepo.Description = encounterTypeForUpdate.EncounterTypeName; encounterTypeFromRepo.Help = encounterTypeForUpdate.Help; _encounterTypeRepository.Update(encounterTypeFromRepo); await _unitOfWork.CompleteAsync(); } return(Ok()); }
public async Task <IActionResult> CreateEncounterType( [FromBody] EncounterTypeForUpdateDto encounterTypeForUpdate) { if (encounterTypeForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); } if (Regex.Matches(encounterTypeForUpdate.EncounterTypeName, @"[a-zA-Z ']").Count < encounterTypeForUpdate.EncounterTypeName.Length) { ModelState.AddModelError("Message", "Description contains invalid characters (Enter A-Z, a-z)"); } if (!String.IsNullOrWhiteSpace(encounterTypeForUpdate.Help)) { if (Regex.Matches(encounterTypeForUpdate.Help, @"[a-zA-Z0-9. ']").Count < encounterTypeForUpdate.Help.Length) { ModelState.AddModelError("Message", "Help contains invalid characters (Enter A-Z, a-z, 0-9, period)"); } } if (_unitOfWork.Repository <EncounterType>().Queryable(). Where(l => l.Description == encounterTypeForUpdate.EncounterTypeName) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); } var workPlan = _workPlanRepository.Get(wp => wp.Description == encounterTypeForUpdate.WorkPlanName); if (workPlan == null) { ModelState.AddModelError("Message", "Unable to locate work plan"); } long id = 0; if (ModelState.IsValid) { var newEncounterType = new EncounterType() { Description = encounterTypeForUpdate.EncounterTypeName, Help = encounterTypeForUpdate.Help }; var newEncounterTypeWorkPlan = new EncounterTypeWorkPlan() { CohortGroup = null, EncounterType = newEncounterType, WorkPlan = workPlan }; _encounterTypeRepository.Save(newEncounterType); _encounterTypeWorkPlanRepository.Save(newEncounterTypeWorkPlan); id = newEncounterType.Id; var mappedEncounterType = await GetEncounterTypeAsync <EncounterTypeIdentifierDto>(id); if (mappedEncounterType == null) { return(StatusCode(500, "Unable to locate newly added item")); } return(CreatedAtAction("GetEncounterTypeByIdentifier", new { id = mappedEncounterType.Id }, CreateLinksForEncounterType <EncounterTypeIdentifierDto>(mappedEncounterType))); } return(BadRequest(ModelState)); }