public async Task <IActionResult> CreateLabTestUnit( [FromBody] LabTestUnitForUpdateDto labTestUnitForUpdate) { if (labTestUnitForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); } if (Regex.Matches(labTestUnitForUpdate.LabTestUnitName, @"[a-zA-Z0-9 ]").Count < labTestUnitForUpdate.LabTestUnitName.Length) { ModelState.AddModelError("Message", "Value contains invalid characters (Enter A-Z, a-z, 0-9, space)"); return(BadRequest(ModelState)); } if (_unitOfWork.Repository <LabTestUnit>().Queryable(). Where(l => l.Description == labTestUnitForUpdate.LabTestUnitName) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); } long id = 0; if (ModelState.IsValid) { var newLabTestUnit = new LabTestUnit() { Description = labTestUnitForUpdate.LabTestUnitName }; _labTestUnitRepository.Save(newLabTestUnit); id = newLabTestUnit.Id; var mappedLabTestUnit = await GetLabTestUnitAsync <LabTestUnitIdentifierDto>(id); if (mappedLabTestUnit == null) { return(StatusCode(500, "Unable to locate newly added item")); } return(CreatedAtAction("GetLabTestUnitByIdentifier", new { id = mappedLabTestUnit.Id }, CreateLinksForLabTestUnit <LabTestUnitIdentifierDto>(mappedLabTestUnit))); } return(BadRequest(ModelState)); }
public async Task <IActionResult> UpdateLabTestUnit(long id, [FromBody] LabTestUnitForUpdateDto labTestUnitForUpdate) { if (labTestUnitForUpdate == null) { ModelState.AddModelError("Message", "Unable to locate payload for new request"); } if (Regex.Matches(labTestUnitForUpdate.LabTestUnitName, @"[a-zA-Z0-9 ]").Count < labTestUnitForUpdate.LabTestUnitName.Length) { ModelState.AddModelError("Message", "Value contains invalid characters (Enter A-Z, a-z, 0-9, space)"); return(BadRequest(ModelState)); } if (_unitOfWork.Repository <LabTestUnit>().Queryable(). Where(l => l.Description == labTestUnitForUpdate.LabTestUnitName && l.Id != id) .Count() > 0) { ModelState.AddModelError("Message", "Item with same name already exists"); } var labTestUnitFromRepo = await _labTestUnitRepository.GetAsync(f => f.Id == id); if (labTestUnitFromRepo == null) { return(NotFound()); } if (ModelState.IsValid) { labTestUnitFromRepo.Description = labTestUnitForUpdate.LabTestUnitName; _labTestUnitRepository.Update(labTestUnitFromRepo); await _unitOfWork.CompleteAsync(); return(Ok()); } return(BadRequest(ModelState)); }