public async Task <Result <PatientChronicIllness> > Handle(AddPatientChronicIllnessCommand request, CancellationToken cancellationToken)
        {
            using (_unitOfWork)
            {
                try
                {
                    List <PatientChronicIllness> patientChronicIllness = new List <PatientChronicIllness>();

                    List <PatientChronicIllness> patientChronicIllnessExists = _unitOfWork
                                                                               .Repository <PatientChronicIllness>().Get(x =>
                                                                                                                         x.PatientId == request.PatientChronicIllnesses[0].PatientId && !x.DeleteFlag)
                                                                               .ToList();
                    if (request.PatientChronicIllnesses.Count > 0)
                    {
                        foreach (var item in request.PatientChronicIllnesses)
                        {
                            bool itemExist = patientChronicIllnessExists.Exists(x =>
                                                                                x.PatientId == item.PatientId && x.ChronicIllness == item.ChronicIllness &&
                                                                                x.OnsetDate.ToString() == item.OnsetDate.ToString());
                            if (!itemExist)
                            {
                                PatientChronicIllness patientChronic = new PatientChronicIllness()
                                {
                                    Active               = 0,
                                    ChronicIllness       = item.ChronicIllness,
                                    CreateBy             = item.CreateBy,
                                    DeleteFlag           = item.DeleteFlag,
                                    Dose                 = item.Dose,
                                    Duration             = item.Duration,
                                    OnsetDate            = item.OnsetDate,
                                    Treatment            = item.Treatment,
                                    PatientId            = item.PatientId,
                                    PatientMasterVisitId = item.PatientMasterVisitId,
                                };

                                patientChronicIllness.Add(patientChronic);
                            }
                        }

                        await _unitOfWork.Repository <PatientChronicIllness>().AddRangeAsync(patientChronicIllness);

                        await _unitOfWork.SaveAsync();

                        return(Result <PatientChronicIllness> .Valid(request.PatientChronicIllnesses[0]));
                    }
                    else
                    {
                        return(Result <PatientChronicIllness> .Valid(new PatientChronicIllness()));
                    }
                }
                catch (Exception e)
                {
                    Log.Error(e.Message);
                    return(Result <PatientChronicIllness> .Invalid(e.Message + e.InnerException));
                }
            }
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] AddPatientChronicIllnessCommand 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));
        }