public async Task <OperationResult> DeleteAnswerByMicrotingUid(int microtingUid)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                using (var sdkContext = core.DbContextHelper.GetDbContext())
                {
                    var answer = await AnswerHelper.GetAnswerQueryByMicrotingUidForDelete(microtingUid, sdkContext)
                                 .FirstOrDefaultAsync();

                    if (answer == null)
                    {
                        return(new OperationResult(
                                   false,
                                   _localizationService.GetString("AnswerNotFound")));
                    }

                    var answersValues = await AnswerHelper.GetAnswerValuesQueryByAnswerIdForDelete(answer.Id, sdkContext)
                                        .ToListAsync();

                    if (answer.WorkflowState == Constants.WorkflowStates.Removed)
                    {
                        return(new OperationResult(
                                   false,
                                   _localizationService.GetString("AnswerRemoved")));
                    }

                    if (answersValues == null)
                    {
                        return(new OperationResult(
                                   false,
                                   _localizationService.GetString("AnswerValuesNotFound")));
                    }

                    foreach (var answersValue in answersValues)
                    {
                        await answersValue.Delete(sdkContext);
                    }

                    await answer.Delete(sdkContext);

                    return(new OperationResult(
                               true,
                               _localizationService.GetString("AnswerAndAnswerValuesHasBeenRemoved")));
                }
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                _logger.LogError(e.Message);
                return(new OperationResult(
                           false,
                           _localizationService.GetString("ErrorWhileRemovingAnswerAndAnswerValues")));
            }
        }
Exemplo n.º 2
0
        public async Task Delete_Answer()
        {
            var answerBeforeDelete = await AnswerHelper.GetAnswerQueryByMicrotingUidForDelete(MicrotingUid, DbContext)
                                     .FirstOrDefaultAsync();

            var answerForBackup = await AnswerHelper.GetAnswerQueryByMicrotingUidForDelete(MicrotingUid, DbContext)
                                  .AsNoTracking()
                                  .FirstOrDefaultAsync();

            var answersValuesBeforeDelete = await AnswerHelper.GetAnswerValuesQueryByAnswerIdForDelete(1, DbContext)
                                            .ToListAsync();

            var answerValuesForBackup = await AnswerHelper.GetAnswerValuesQueryByAnswerIdForDelete(1, DbContext)
                                        .AsNoTracking()
                                        .ToListAsync();

            Assert.IsNotEmpty(answersValuesBeforeDelete);

            foreach (var answersValue in answersValuesBeforeDelete)
            {
                await answersValue.Delete(DbContext);
            }

            Assert.AreNotEqual(answerBeforeDelete, default);
            await answerBeforeDelete.Delete(DbContext);

            var answerAfterDelete = await AnswerHelper.GetAnswerQueryByMicrotingUidForDelete(MicrotingUid, DbContext)
                                    .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed).AsNoTracking()
                                    .FirstOrDefaultAsync();

            var answersValuesAfterDelete = await AnswerHelper.GetAnswerValuesQueryByAnswerIdForDelete(1, DbContext)
                                           .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed).AsNoTracking()
                                           .ToListAsync();

            Assert.IsEmpty(answersValuesAfterDelete);
            Assert.AreEqual(answerAfterDelete, default);

            answerBeforeDelete.Version       = answerForBackup.Version;
            answerBeforeDelete.WorkflowState = answerForBackup.WorkflowState;
            answerBeforeDelete.UpdatedAt     = answerForBackup.UpdatedAt;
            DbContext.Answers.Update(answerBeforeDelete);
            for (var i = 0; i < answersValuesBeforeDelete.Count; i++)
            {
                answersValuesBeforeDelete[i].Version       = answerValuesForBackup[i].Version;
                answersValuesBeforeDelete[i].WorkflowState = answerValuesForBackup[i].WorkflowState;
                answersValuesBeforeDelete[i].UpdatedAt     = answerValuesForBackup[i].UpdatedAt;
                DbContext.AnswerValues.Update(answersValuesBeforeDelete[i]);
            }

            await DbContext.SaveChangesAsync();
        }