示例#1
0
        private void AddReferenceDataToModel(EditQuestionViewModel model)
        {
            if (model == null)
            {
                model = new EditQuestionViewModel();
            }

            model.Categories = _categoryDao.GetAllQuestionCategory().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text  = o.Name,
                Value = o.Id.ToString()
            });
            model.Difficulties = _difficultyDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text  = o.DifficultyLevel,
                Value = o.Id.ToString()
            });
            model.QuestionTypes = _questionTypeDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text  = o.Name,
                Value = o.Id.ToString()
            });
            model.Answers = _answerDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text  = o.Content,
                Value = o.Id.ToString()
            });
        }
        //cette methode prend en parametre le filtre de l'utilisateur et retourne le model associé
        private CreateSurveyViewModel BuildSurveyViewModel(SurveyFilterInfoViewModel surveyFilterInfo = null)
        {
            var model = surveyFilterInfo == null ? new CreateSurveyViewModel()
            {
                CategoryId   = CATEGORY_ID,
                DifficultyId = DIFFICULTY_ID,
                TypeId       = TYPE_ID
            } :
            new CreateSurveyViewModel()
            {
                CategoryId   = surveyFilterInfo.CategoryId,
                DifficultyId = surveyFilterInfo.DifficultyId,
                TypeId       = surveyFilterInfo.TypeId
            };

            //dans le selectlistItem nous avons
            model.Categories = _categoryDao.GetAllQuestionCategory().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text  = o.Name,
                Value = o.Id.ToString(),
                // Put default categoryId qs const
                Selected = o.Id == model.CategoryId
            });
            model.Difficulties = _difficultyDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text     = o.DifficultyLevel,
                Value    = o.Id.ToString(),
                Selected = o.Id == model.DifficultyId
            });
            model.QuestionTypes = _questionTypeDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem
            {
                Text     = o.Name,
                Value    = o.Id.ToString(),
                Selected = o.Id == model.TypeId
            });

            //on recupère les questions depuis la base de donnée en fonction des différents paramètres
            var questions = _questionDao.GetAll();
            var questionListViewModels = new List <QuestionViewModel>();

            if (questions != null && questions.Any())
            {
                foreach (var question in questions.Where(q => surveyFilterInfo == null || (
                                                             (surveyFilterInfo.CategoryId == 0 || q.Category.Id == surveyFilterInfo.CategoryId) &&
                                                             (surveyFilterInfo.TypeId == 0 || q.Type.Id == surveyFilterInfo.TypeId) &&
                                                             (surveyFilterInfo.DifficultyId == 0 || q.Difficulty.Id == surveyFilterInfo.DifficultyId)
                                                             )))
                {
                    questionListViewModels.Add(
                        new QuestionViewModel()
                    {
                        Id              = question.Id,
                        CategoryName    = question.Category.Name,
                        TypeName        = question.Type.Name,
                        DifficultyLevel = question.Difficulty.DifficultyLevel,
                        Content         = question.Content,
                        IsChecked       = surveyFilterInfo != null && surveyFilterInfo.SurveySelectedQuestions != null && surveyFilterInfo.SurveySelectedQuestions.Any() &&
                                          surveyFilterInfo.SurveySelectedQuestions.Any(sq => sq.Id == question.Id && sq.IsChecked)
                    });
                }
            }

            model.SurveySelectedQuestions = questionListViewModels;

            return(model);
        }