public async Task <Result <IndicatorResponse> > Handle(SubmitIndicatorResultsCommand request, CancellationToken cancellationToken)
        {
            using (var transaction = _airUnitOfWork.BeginTransaction(IsolationLevel.ReadCommitted))
            {
                try
                {
                    var reportingPeriod = new ReportingPeriod(request.ReportingFormId, request.ReportingDate, request.CreatedBy);
                    await _airUnitOfWork.Repository <ReportingPeriod>().AddAsync(reportingPeriod);

                    var indicatorResults = request.IndicatorResults.Select(x =>
                                                                           new IndicatorResult(reportingPeriod.Id, x.Id, x.ResultText, x.ResultNumeric, request.CreatedBy));

                    await _airUnitOfWork.Repository <IndicatorResult>().AddRangeAsync(indicatorResults);

                    await _airUnitOfWork.SaveAsync();

                    transaction.Commit();

                    return(Result <IndicatorResponse> .Valid(new IndicatorResponse()
                    {
                        Message = "Indicator results added succesfully",
                        ReportingFormId = request.ReportingFormId
                    }));
                }
                catch (Exception ex)
                {
                    _logger.Error(ex,
                                  $"An error occured while submitting indicator results for period {request.ReportingDate} and ReportId {request.ReportingFormId}");
                    transaction.Rollback();

                    return(Result <IndicatorResponse> .Invalid("An error occured while submittig indicator results"));
                }
            }
        }
        public Task <Result <EditIndicatorResponse> > Handle(EditIndicatorResultsCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var reportingPeriod = _airUnitOfWork.Repository <ReportingPeriod>()
                                      .Get(x => x.Id == request.ReportingPeriodId).SingleOrDefault();
                if (reportingPeriod == null)
                {
                    return(Task.FromResult(Result <EditIndicatorResponse> .Invalid($"Reporting Period Id {request.ReportingPeriodId} not found")));
                }

                reportingPeriod.Update(request.ReportingDate);
                _airUnitOfWork.Repository <ReportingPeriod>().Update(reportingPeriod);

                var indicatorResults = _airUnitOfWork.Repository <IndicatorResult>()
                                       .Get(x => x.ReportingPeriodId == request.ReportingPeriodId).AsEnumerable();

                if (!indicatorResults.Any())
                {
                    return(Task.FromResult(Result <EditIndicatorResponse> .Invalid($"Indicator results for period Id {request.ReportingPeriodId} not found")));
                }

                if (request.IndicatorResults == null || !request.IndicatorResults.Any())
                {
                    return(Task.FromResult(Result <EditIndicatorResponse> .Invalid("Indicator Results to be edited not found")));
                }

                var updatedIndicatorResults = indicatorResults.Where(x =>
                                                                     request.IndicatorResults.Any(i => i.Id == x.IndicatorId && (x.ResultNumeric != i.ResultNumeric || x.ResultText != x.ResultText)))
                                              .AsEnumerable();

                foreach (var result in updatedIndicatorResults)
                {
                    var newResultValue = request.IndicatorResults.SingleOrDefault(x => x.Id == result.IndicatorId);
                    if (newResultValue == null)
                    {
                        var IndicatorResult = new IndicatorResult(reportingPeriod.Id, result.Id, result.ResultText, result.ResultNumeric, reportingPeriod.CreatedBy);

                        _airUnitOfWork.Repository <IndicatorResult>().AddRangeAsync(indicatorResults);
                        _airUnitOfWork.SaveAsync();
                    }
                    else
                    {
                        result.UpdateResult(newResultValue.ResultNumeric, newResultValue.ResultText);
                        _airUnitOfWork.Repository <IndicatorResult>().Update(result);
                    }
                }

                _airUnitOfWork.Save();

                return(Task.FromResult(Result <EditIndicatorResponse> .Valid(new EditIndicatorResponse
                {
                    Message = $"Indicator results for report periodId:   {request.ReportingPeriodId}  and report month of  {request.ReportingDate.Month} and reporting year of " +
                              $" {request.ReportingDate.Year} updated succesfully",
                    ReportingPeriodId = request.ReportingPeriodId
                })));
            }
            catch (Exception ex)
            {
                string message = $"An error occured while editing indicator results for reporting periodId {request.ReportingPeriodId}";

                _logger.Error(ex, message);
                return(Task.FromResult(Result <EditIndicatorResponse> .Invalid(message)));
            }
        }