Exemplo n.º 1
0
        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 <bool> Handle(ChangeLabTestDetailsCommand message, CancellationToken cancellationToken)
        {
            var patientFromRepo = await _patientRepository.GetAsync(f => f.Id == message.PatientId, new string[] {
                "PatientLabTests.LabTest",
                "PatientLabTests.TestUnit"
            });

            if (patientFromRepo == null)
            {
                throw new KeyNotFoundException($"Unable to locate patient {message.PatientId}");
            }

            LabTestUnit labTestUnitFromRepo = null;

            if (!String.IsNullOrWhiteSpace(message.TestUnit))
            {
                labTestUnitFromRepo = _labTestUnitRepository.Get(u => u.Description == message.TestUnit);
                if (labTestUnitFromRepo == null)
                {
                    throw new KeyNotFoundException($"Unable to locate lab test unit with a description of {message.TestUnit}");
                }
            }

            var labTestToUpdate   = patientFromRepo.PatientLabTests.Single(plt => plt.Id == message.PatientLabTestId);
            var labTestAttributes = await PrepareAttributesWithNewValuesAsync(labTestToUpdate, message.Attributes);

            var userName = _httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            patientFromRepo.ChangeLabTestDetails(message.PatientLabTestId, message.TestDate, message.TestResultCoded, labTestUnitFromRepo, message.TestResultValue, message.ReferenceLower, message.ReferenceUpper);

            _modelExtensionBuilder.ValidateAndUpdateExtendable(labTestToUpdate, labTestAttributes, userName);

            _patientRepository.Update(patientFromRepo);

            _logger.LogInformation($"----- Lab Test {message.PatientLabTestId} details updated");

            return(await _unitOfWork.CompleteAsync());
        }
Exemplo n.º 3
0
        public async Task <PatientLabTestIdentifierDto> Handle(AddLabTestToPatientCommand message, CancellationToken cancellationToken)
        {
            var patientFromRepo = await _patientRepository.GetAsync(f => f.Id == message.PatientId, new string[] {
                "PatientLabTests.LabTest",
                "PatientLabTests.TestUnit"
            });

            if (patientFromRepo == null)
            {
                throw new KeyNotFoundException($"Unable to locate patient {message.PatientId}");
            }

            var labTestFromRepo = await _labTestRepository.GetAsync(lt => lt.Description == message.LabTest);

            if (labTestFromRepo == null)
            {
                throw new KeyNotFoundException($"Unable to locate lab test with a description of {message.LabTest}");
            }

            LabTestUnit labTestUnitFromRepo = null;

            if (!String.IsNullOrWhiteSpace(message.TestUnit))
            {
                labTestUnitFromRepo = _labTestUnitRepository.Get(u => u.Description == message.TestUnit);
                if (labTestUnitFromRepo == null)
                {
                    throw new KeyNotFoundException($"Unable to locate lab test unit with a description of {message.TestUnit}");
                }
            }

            var labTestDetail = await PrepareLabTestDetailAsync(message.Attributes);

            if (!labTestDetail.IsValid())
            {
                labTestDetail.InvalidAttributes.ForEach(element => throw new DomainException(element));
            }

            var newPatientLabTest = patientFromRepo.AddLabTest(
                message.TestDate,
                message.TestResultCoded,
                labTestFromRepo,
                labTestUnitFromRepo,
                message.TestResultValue,
                message.ReferenceLower,
                message.ReferenceUpper
                );

            _modelExtensionBuilder.UpdateExtendable(newPatientLabTest, labTestDetail.CustomAttributes, "Admin");

            _patientRepository.Update(patientFromRepo);

            await _unitOfWork.CompleteAsync();

            _logger.LogInformation($"----- Lab Test {message.LabTest} created");

            var mappedPatientLabTest = _mapper.Map <PatientLabTestIdentifierDto>(newPatientLabTest);

            CreateLinks(mappedPatientLabTest);

            return(mappedPatientLabTest);
        }