Beispiel #1
0
        public async Task <IActionResult> CreateLabTest(
            [FromBody] LabTestForUpdateDto labTestForUpdate)
        {
            if (labTestForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
            }

            if (Regex.Matches(labTestForUpdate.LabTestName, @"[a-zA-Z0-9 ]").Count < labTestForUpdate.LabTestName.Length)
            {
                ModelState.AddModelError("Message", "Value contains invalid characters (Enter A-Z, a-z, 0-9, space)");
                return(BadRequest(ModelState));
            }

            if (_unitOfWork.Repository <LabTest>().Queryable().
                Where(l => l.Description == labTestForUpdate.LabTestName)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
            }

            long id = 0;

            if (ModelState.IsValid)
            {
                var newLabTest = new LabTest()
                {
                    Description = labTestForUpdate.LabTestName,
                    Active      = true
                };

                _labTestRepository.Save(newLabTest);
                id = newLabTest.Id;

                var mappedLabTest = await GetLabTestAsync <LabTestIdentifierDto>(id);

                if (mappedLabTest == null)
                {
                    return(StatusCode(500, "Unable to locate newly added item"));
                }

                return(CreatedAtAction("GetLabTestByIdentifier",
                                       new
                {
                    id = mappedLabTest.Id
                }, CreateLinksForLabTest <LabTestIdentifierDto>(mappedLabTest)));
            }

            return(BadRequest(ModelState));
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateLabTest(long id,
                                                        [FromBody] LabTestForUpdateDto labTestForUpdate)
        {
            if (labTestForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
            }

            if (Regex.Matches(labTestForUpdate.LabTestName, @"[a-zA-Z0-9 ]").Count < labTestForUpdate.LabTestName.Length)
            {
                ModelState.AddModelError("Message", "Value contains invalid characters (Enter A-Z, a-z, 0-9, space)");
                return(BadRequest(ModelState));
            }

            if (_unitOfWork.Repository <LabTest>().Queryable().
                Where(l => l.Description == labTestForUpdate.LabTestName && l.Id != id)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
            }

            var labTestFromRepo = await _labTestRepository.GetAsync(f => f.Id == id);

            if (labTestFromRepo == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                labTestFromRepo.Description = labTestForUpdate.LabTestName;
                labTestFromRepo.Active      = (labTestForUpdate.Active == Models.ValueTypes.YesNoValueType.Yes);

                _labTestRepository.Update(labTestFromRepo);
                await _unitOfWork.CompleteAsync();

                return(Ok());
            }

            return(BadRequest(ModelState));
        }