Beispiel #1
0
        public async Task <object> AddDeliveredBabyBirthInfo([FromBody] AddDeliveredBabyBirthInformationCommand command)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(command));
            }

            var response = await _mediator.Send(command, HttpContext.RequestAborted);

            if (response.IsValid)
            {
                return(Ok(response.Value));
            }

            return(BadRequest(response));
        }
        public async Task <Result <DeliveredBabyBirthInfoResult> > Handle(AddDeliveredBabyBirthInformationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (request.PatientDeliveryInformationId == default(int))
                {
                    var patientDeliveryInfo = _maternityUnitOfWork.Repository <PatientDeliveryInformationView>()
                                              .Get(x => x.PatientMasterVisitId == request.PatientMasterVisitId).FirstOrDefault();

                    request.PatientDeliveryInformationId = patientDeliveryInfo != null ? patientDeliveryInfo.Id : 0;
                }

                var deliveredBabyBirthInformation = _mapper.Map <DeliveredBabyBirthInformation>(request);

                await _maternityUnitOfWork.Repository <DeliveredBabyBirthInformation>().AddAsync(deliveredBabyBirthInformation);

                if (request.ApgarScores != null)
                {
                    var apgarScores = request.ApgarScores
                                      .Select(x => new DeliveredBabyApgarScore(x.ApgarScoreId, deliveredBabyBirthInformation.Id, x.Score)).ToList();

                    await _maternityUnitOfWork.Repository <DeliveredBabyApgarScore>().AddRangeAsync(apgarScores);
                }

                await _maternityUnitOfWork.SaveAsync();

                return(Result <DeliveredBabyBirthInfoResult> .Valid(new DeliveredBabyBirthInfoResult
                {
                    DeliveredBabyBirthInfoId = deliveredBabyBirthInformation.Id,
                    PatientDeliveryInformationId = request.PatientDeliveryInformationId
                }));
            }

            catch (Exception ex)
            {
                string errorMessage = $"An error occured while adding delivered baby birth info for patientmastervisitId {request.PatientMasterVisitId}";
                logger.Error(ex, errorMessage);

                return(Result <DeliveredBabyBirthInfoResult> .Invalid(errorMessage));
            }
        }