예제 #1
0
        public async Task <NewSurveyDto> Create(NewSurveyDto newSurveyDto)
        {
            if (newSurveyDto == null)
            {
                throw new ArgumentNullException();
            }

            var guid = Guid.NewGuid().ToString();

            var newSurvey = new Survey
            {
                Guid             = guid,
                AuthorId         = newSurveyDto.AuthorId,
                AnonymousAnswers = newSurveyDto.AnonymousAnswers,
                Title            = newSurveyDto.Title,
                CreationDate     = DateTime.Now
            };

            var surveyDto = _mapper.Map <NewSurveyDto>(newSurvey);
            var surveyId  = await _repository.Create(newSurvey);

            if (newSurveyDto.Questions != null)
            {
                foreach (var question in newSurveyDto.Questions)
                {
                    var questionId = await _questionsRepository.Create(new Question()
                    {
                        SurveyId     = surveyId,
                        QuestionText = question.QuestionText,
                        Type         = question.Type
                    });

                    if (question.Type == QuestionTypeEnum.MultipleChoice && question.Choices != null)
                    {
                        foreach (var choice in question.Choices)
                        {
                            await _choicesRepository.Create(new Choice()
                            {
                                QuestionId = questionId,
                                ChoiceText = choice.ChoiceText
                            });
                        }
                    }
                }
            }

            return(surveyDto);
        }
        public virtual ActionResult Create(QuestionViewModel model)
        {
            if (ModelState.IsValid)
            {
                var question = new Question()
                {
                    OrderNumber     = model.OrderNumber,
                    QuestionnaireId = model.QuestionnaireId,
                    Text            = model.Text,
                    CategoryId      = model.CategoryId
                };

                _questionsRepository.Create(question);

                return(RedirectToAction(MVC.Admin.Questions.List(model.QuestionnaireId)));
            }

            model.SetCategories(_questionnairesRepository.GetById(model.QuestionnaireId).Categories);

            return(View(model));
        }
예제 #3
0
        public async Task <IActionResult> Create(HomeCreateViewModel data)
        {
            var allQUestions = questionsRepository.GetAll(); //Get all the Question(s)

            //Only 10 Questions are allowed at a time
            if (allQUestions.Count >= 10)
            {
                ModelState.AddModelError("", "Oops only 10 questions can be added at the moment");
            }
            else
            {
                if (ModelState.IsValid)
                {
                    //Create the Question Object that is used to Create the Question in the database
                    var question = new Questions
                    {
                        QuestionText  = data.QuestionText,
                        CorrectOption = data.CorrectOption
                    };

                    var createdQuestion = await questionsRepository.Create(question);//Create the Question

                    //A list containing the options that will be stored in the database
                    List <Options> optionsList = new List <Options>();

                    //Create a list that stores all the Options the User Created for the Question
                    List <string> optionText = new List <string>
                    {
                        data.Option1,
                        data.Option2,
                        data.Option3,
                        data.Option4
                    };

                    //Add items to the Option list
                    for (int i = 0; i < 4; i++)
                    {
                        optionsList.Add(
                            new Options
                        {
                            OptionText = optionText[i],
                            QuestionId = createdQuestion.QuestionId
                        });
                    }

                    await optionsRepository.Create(optionsList);//Create the Options

                    return(RedirectToAction("Index"));
                }
            }

            //Error occured while creating, create and return the Option list again
            data.Options = new List <Option>
            {
                new Option {
                    Id = 1, OptionText = "Option 1"
                },
                new Option {
                    Id = 2, OptionText = "Option 2"
                },
                new Option {
                    Id = 3, OptionText = "Option 3"
                },
                new Option {
                    Id = 4, OptionText = "Option 4"
                }
            };
            ModelState.AddModelError("", "Error occured while creating please create again");
            return(View(data));
        }