Пример #1
0
        public async Task <IActionResult> UpdateLabResult(long id,
                                                          [FromBody] LabResultForUpdateDto labResultForUpdate)
        {
            if (labResultForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

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

            var labResultFromRepo = await _labResultRepository.GetAsync(f => f.Id == id);

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

            if (ModelState.IsValid)
            {
                labResultFromRepo.Description = labResultForUpdate.LabResultName;
                labResultFromRepo.Active      = (labResultForUpdate.Active == Models.ValueTypes.YesNoValueType.Yes);

                _labResultRepository.Update(labResultFromRepo);
                await _unitOfWork.CompleteAsync();
            }

            return(Ok());
        }
Пример #2
0
        public async Task <IActionResult> CreateLabResult(
            [FromBody] LabResultForUpdateDto labResultForUpdate)
        {
            if (labResultForUpdate == null)
            {
                ModelState.AddModelError("Message", "Unable to locate payload for new request");
                return(BadRequest(ModelState));
            }

            if (_unitOfWork.Repository <LabResult>().Queryable().
                Where(l => l.Description == labResultForUpdate.LabResultName)
                .Count() > 0)
            {
                ModelState.AddModelError("Message", "Item with same name already exists");
                return(BadRequest(ModelState));
            }

            long id = 0;

            if (ModelState.IsValid)
            {
                var newLabResult = new LabResult()
                {
                    Description = labResultForUpdate.LabResultName,
                    Active      = true
                };

                _labResultRepository.Save(newLabResult);
                id = newLabResult.Id;
            }

            var mappedLabResult = await GetLabResultAsync <LabResultIdentifierDto>(id);

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

            return(CreatedAtAction("GetLabResultByIdentifier",
                                   new
            {
                id = mappedLabResult.Id
            }, CreateLinksForLabResult <LabResultIdentifierDto>(mappedLabResult)));
        }