Esempio n. 1
0
        public void DeleteOptionChoises(Guid id)
        {
            var questionOptions = _questionOptionService.GetAllAsync().Result.Where(x => x.OptionChoiseId == id);


            if (questionOptions.Count() > 0)
            {
                foreach (var q_o in questionOptions)
                {
                    var answerList = _surveyStructureService.GetAnsersByQuestion_OptionId(q_o.Id);
                    foreach (var answer in answerList)
                    {
                        _surveyStructureService.RemoveAnswer(answer);
                    }
                    _questionOptionService.Remove(q_o);
                }
                ;
            }
        }
        public async Task <IActionResult> UpdateSurvey([FromBody] SurveyViewModel data)
        {
            try
            {
                var surveyId = _surveySectionService.Update(data.survey).Result;

                if (data.survey.Pages.Count() > 0)
                {
                    List <BaseQuestionModel> questionList = new List <BaseQuestionModel>();
                    foreach (var page in data.survey.Pages)
                    {
                        if (page.State == ControlStates.Created.ToString())
                        {
                            Guid pageId = await _pageService.AddAsync(page, Guid.Parse(data.survey.Id));

                            questionList = _questionService.GetTypedQuestionList(page);
                            if (questionList.Count() > 0)
                            {
                                foreach (var question in questionList)
                                {
                                    _questionService.SaveQuestionByType(question, pageId);
                                }
                            }
                        }
                        else
                        {
                            Guid pageId = await _pageService.UpdateAsync(page, Guid.Parse(data.survey.Id));

                            questionList = _questionService.GetTypedQuestionList(page);
                            if (questionList.Count() > 0)
                            {
                                _questionService.Update(questionList, page.Id);
                            }
                        }
                    }

                    var deleteQuestionList = data.deleteQuestions.Distinct().ToList();
                    if (deleteQuestionList.Count > 0)
                    {
                        foreach (var item in deleteQuestionList)
                        {
                            var questionOptions = _questionOptionService.GetAllAsync().Result.Where(x => x.QuestionId == item);


                            if (questionOptions.Count() > 0)
                            {
                                foreach (var q_o in questionOptions)
                                {
                                    var answerList = _answerService.GetAllAsync().Result.Where(x => x.QuestionOptionId == q_o.Id);
                                    foreach (var answer in answerList)
                                    {
                                        await _answerService.Remove(answer);
                                    }
                                    await _questionOptionService.Remove(q_o);
                                }
                                ;
                            }
                            var childQuestions = await _questionService.GetListByBaseQuestion(item);

                            if (childQuestions.Count() > 0)
                            {
                                foreach (var childQuestion in childQuestions)
                                {
                                    var questionOptionList = _questionOptionService.GetAllAsync().Result.Where(x => x.QuestionId == childQuestion.Id);


                                    if (questionOptionList.Count() > 0)
                                    {
                                        foreach (var q_o in questionOptionList)
                                        {
                                            var answerList = _answerService.GetAllAsync().Result.Where(x => x.QuestionOptionId == q_o.Id);
                                            foreach (var answer in answerList)
                                            {
                                                await _answerService.Remove(answer);
                                            }
                                            await _questionOptionService.Remove(q_o);
                                        }
                                        ;
                                    }
                                    await _questionService.DeleteQuestionById(childQuestion.Id);
                                }
                            }
                            await _questionService.DeleteQuestionById(item);
                        }
                    }
                    var deletePageList = data.deletePages.Distinct().ToList();
                    if (deletePageList.Count > 0)
                    {
                        foreach (var pageId in deletePageList)
                        {
                            var questions = _questionService.GetListByPageId(pageId).Select(x => x.Id).ToList();
                            foreach (var question in questions)
                            {
                                var questionOptions = _questionOptionService.GetAllAsync().Result.Where(x => x.QuestionId == question);
                                if (questionOptions.Count() > 0)
                                {
                                    foreach (var q_o in questionOptions)
                                    {
                                        var answerList = _answerService.GetAllAsync().Result.Where(x => x.QuestionOptionId == q_o.Id);
                                        foreach (var answer in answerList)
                                        {
                                            await _answerService.Remove(answer);
                                        }
                                        await _questionOptionService.Remove(q_o);
                                    }
                                    ;
                                }
                                var childQuestions = await _questionService.GetListByBaseQuestion(question);

                                if (childQuestions.Count() > 0)
                                {
                                    foreach (var childQuestion in childQuestions)
                                    {
                                        await _questionService.DeleteQuestionById(childQuestion.Id);
                                    }
                                }
                                await _questionService.DeleteQuestionById(question);
                            }
                            _pageService.DeletePageById(pageId);
                        }
                    }
                }
                return(Ok());
            }
            catch (Exception ex)
            {
                var check = ex;
                throw;
            }
        }