예제 #1
0
 public void AddAnswer(AddAnswerRequest request)
 {
     GeneralHelpers.ThrowIfNull(request.Answer);
     answerRepo.AddAnswer(new Answer
     {
         Description = request.Answer,
         QuestionId  = request.QuestionId
     });
 }
예제 #2
0
        public async Task <IActionResult> CreateAsync(IFormCollection collection, AddQuestion_VM vm)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Validation Error");
                }
                //add question
                Question question = new Question()
                {
                    QuizId       = vm.QuizId,
                    QuestionId   = Guid.NewGuid(),
                    QuestionText = vm.QuestionText
                };

                var result = questionRepo.AddQuestion(question);
                if (result == null)
                {
                    return(Redirect("/Error/400"));
                }

                //add answer
                Answer answer = new Answer()
                {
                    AnswerID   = Guid.NewGuid(),
                    AnswerText = vm.QuestionAnswer,
                    QuestionID = question.QuestionId
                };
                var resultAnswer = answerRepo.AddAnswer(answer);
                if (resultAnswer == null)
                {
                    return(Redirect("/Error/400"));
                }


                //add choice 1
                Choice choiceA = new Choice()
                {
                    ChoiceID   = Guid.NewGuid(),
                    ChoiceText = vm.QuestionChoiceA,
                    QuestionID = question.QuestionId
                };
                var resultChoiceA = choiceRepo.AddChoice(choiceA);
                if (resultChoiceA == null)
                {
                    return(Redirect("/Error/400"));
                }
                //add choice 2
                Choice choiceB = new Choice()
                {
                    ChoiceID   = Guid.NewGuid(),
                    ChoiceText = vm.QuestionChoiceB,
                    QuestionID = question.QuestionId
                };
                var resultChoiceB = choiceRepo.AddChoice(choiceB);
                if (resultChoiceB == null)
                {
                    return(Redirect("/Error/400"));
                }
                //add choice 2
                Choice choiceC = new Choice()
                {
                    ChoiceID   = Guid.NewGuid(),
                    ChoiceText = vm.QuestionChoiceC,
                    QuestionID = question.QuestionId
                };
                var resultChoiceC = choiceRepo.AddChoice(choiceC);
                if (resultChoiceC == null)
                {
                    return(Redirect("/Error/400"));
                }
                return(RedirectToAction(nameof(Index)));
            }
            catch (Exception ex)
            {
                Debug.WriteLine("unable to create" + ex.Message);
                ModelState.AddModelError("", "Create mislukt." + ex.Message);
                return(View(vm));
            }
        }