コード例 #1
0
        public ActionResult PutAnswerById(AnswerInputModel answerModel)
        {
            Mapper           mapper    = new Mapper();
            AnswerDTO        answerdto = mapper.ConvertAnswerInputModelToAnswerDTO(answerModel);
            AuthorDataAccess answers   = new AuthorDataAccess();
            var answer = answers.GetAnswerById(answerModel.ID);

            if (answer == null)
            {
                return(BadRequest("Ответа не существует"));
            }
            var question = answers.GetQuestionById(answerModel.QuestionID);

            if (question == null)
            {
                return(BadRequest("Вопроса не существует"));
            }
            if (string.IsNullOrWhiteSpace(answerModel.Value))
            {
                return(BadRequest("Введите ответ"));
            }
            if (answerModel.Correct == null)
            {
                return(BadRequest("Введите корректный ответ или нет"));
            }
            answers.UpdateAnswerById(answerdto);
            return(Ok("Успешно изменено!"));
        }
コード例 #2
0
        public async Task AddQuestionShouldAddQuestionInQuizCorrectly()
        {
            var model = new IndexQuestionViewModel();

            model.QuestionInputModel = new QuestionInputModel()
            {
                QuestionText = "A??",
                QuizId       = "123",
            };

            var firstAnswer = new AnswerInputModel()
            {
                AnswerText      = "true",
                IsCorrectAnswer = true,
            };
            var secondAnswer = new AnswerInputModel()
            {
                AnswerText      = "false",
                IsCorrectAnswer = false,
            };

            model.AnswersInputModel = new List <AnswerInputModel>();
            model.AnswersInputModel.Add(firstAnswer);
            model.AnswersInputModel.Add(secondAnswer);

            var questionIdResult = await this.Service.AddQuestionAsync(model);

            var questionFromDb = this.DbContext.Questions.FirstOrDefault();

            Assert.NotNull(questionFromDb);
            Assert.Equal(questionFromDb.Id, questionIdResult);
        }
コード例 #3
0
        public async Task UpdateQuestionShouldUpdateQuestionInQuizCorrectly()
        {
            var quizId = "123";
            var model  = new IndexQuestionViewModel();

            model.QuestionInputModel = new QuestionInputModel()
            {
                QuestionText = "A??",
                QuizId       = quizId,
            };


            var firstAnswer = new AnswerInputModel()
            {
                AnswerText      = "true",
                IsCorrectAnswer = true,
            };
            var secondAnswer = new AnswerInputModel()
            {
                AnswerText      = "false",
                IsCorrectAnswer = false,
            };

            model.AnswersInputModel = new List <AnswerInputModel>();
            model.AnswersInputModel.Add(firstAnswer);
            model.AnswersInputModel.Add(secondAnswer);

            var questionIdResult = await this.Service.AddQuestionAsync(model);

            var editModel = new IndexQuestionViewModel();

            editModel.QuestionInputModel = new QuestionInputModel()
            {
                QuestionText = "B??",
                QuizId       = quizId,
            };
            var firstNewAnswer = new AnswerInputModel()
            {
                AnswerText      = "false",
                IsCorrectAnswer = false,
            };
            var secondNewAnswer = new AnswerInputModel()
            {
                AnswerText      = "true",
                IsCorrectAnswer = true,
            };

            editModel.AnswersInputModel = new List <AnswerInputModel>();
            editModel.AnswersInputModel.Add(firstNewAnswer);
            editModel.AnswersInputModel.Add(secondNewAnswer);

            var quizIdResult = await this.Service.UpdateQuestionAsync(editModel, questionIdResult);

            var updatedQuestion = this.DbContext.Questions.FirstOrDefault();

            Assert.Equal(quizId, quizIdResult);
            Assert.NotEqual(model.QuestionInputModel.QuestionText, updatedQuestion.QuestionText);
            Assert.NotEqual(firstAnswer.AnswerText, updatedQuestion.Answers.FirstOrDefault().AnswerText);
            Assert.NotEqual(firstAnswer.IsCorrectAnswer, updatedQuestion.Answers.FirstOrDefault().IsCorrectAnswer);
        }
コード例 #4
0
        public async Task AnswerInputsToModelsShouldCorrectlyBindProperties()
        {
            var question = new Question()
            {
                QuestionText = "Question test",
            };

            var input       = new List <AnswerInputModel>();
            var answerInput = new AnswerInputModel()
            {
                AnswerText      = "QQQQ",
                QuestionId      = question.Id,
                IsCorrectAnswer = false,
            };

            var anotherAnswerInput = new AnswerInputModel()
            {
                AnswerText      = "BBB",
                QuestionId      = question.Id,
                IsCorrectAnswer = true,
            };

            input.Add(answerInput);
            input.Add(anotherAnswerInput);
            var answers = this.Service.AnswerInputsToModels(input, question.Id).ToList();

            Assert.IsType <Answer>(answers[0]);
            Assert.IsType <Answer>(answers[1]);
        }
コード例 #5
0
        public ActionResult Create(int postId, AnswerInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return View(model);
            }            

            var Answer = new Answer()
            {
                Content = model.Content,
                Title = model.Title,
                PostId = postId
            };

            if (this.User.Identity.IsAuthenticated)
            {
                Answer.AuthorId = this.User.Identity.GetUserId();
            }

            this.answers.Add(Answer);
            this.answers.SaveChanges();

            this.TempData["Notification"] = "Thank you for your answer!";
            return this.Redirect(string.Format("/questions/{0}", postId));
        }
コード例 #6
0
ファイル: AnswerService.cs プロジェクト: vallecbg/E-School
        public async Task <string> Create(AnswerInputModel model)
        {
            var answer = Mapper.Map <OfferedAnswer>(model);

            this.Context.OfferedAnswers.Add(answer);
            await this.Context.SaveChangesAsync();

            return(answer.Id);
        }
コード例 #7
0
 public AnswerDTO ConvertAnswerInputModelToAnswerDTO(AnswerInputModel answermodel)
 {
     return(new AnswerDTO()
     {
         ID = answermodel.ID,
         QuestionID = answermodel.QuestionID,
         Value = answermodel.Value,
         Correct = answermodel.Correct,
     });
 }
コード例 #8
0
        public async Task UpdateAnswersShouldUpdateAnswerCorrectly()
        {
            var question = new Question()
            {
                QuestionText = "Question test",
            };

            var answer = new Answer()
            {
                AnswerText      = "A",
                QuestionId      = question.Id,
                IsCorrectAnswer = true,
            };

            var anotherAnswer = new Answer()
            {
                AnswerText      = "B",
                QuestionId      = question.Id,
                IsCorrectAnswer = false,
            };

            question.Answers.Add(answer);
            question.Answers.Add(anotherAnswer);

            await this.DbContext.Questions.AddAsync(question);

            await this.DbContext.SaveChangesAsync();

            var input = new List <AnswerInputModel>();

            var answerInput = new AnswerInputModel()
            {
                AnswerText      = "QQQQ",
                QuestionId      = question.Id,
                IsCorrectAnswer = false,
            };

            var anotherAnswerInput = new AnswerInputModel()
            {
                AnswerText      = "BBB",
                QuestionId      = question.Id,
                IsCorrectAnswer = true,
            };

            input.Add(answerInput);
            input.Add(anotherAnswerInput);
            await this.Service.UpdateAnswers(input, question.Id);

            var answers = this.DbContext.Answers.Where(x => x.QuestionId == question.Id).ToList();

            Assert.Equal(answerInput.AnswerText, answers[0].AnswerText);
            Assert.Equal(answerInput.IsCorrectAnswer, answers[0].IsCorrectAnswer);
            Assert.Equal(anotherAnswerInput.AnswerText, answers[1].AnswerText);
            Assert.Equal(anotherAnswerInput.IsCorrectAnswer, answers[1].IsCorrectAnswer);
        }
コード例 #9
0
 public ActionResult RegisterAnswer(AnswerInputModel model)
 {
     try
     {
         _answerAppService.RegisterAnswer(model);
         return(Ok());
     }
     catch (Exception ex)
     {
         return(new BadRequestObjectResult(ex.Message));
     }
 }
コード例 #10
0
ファイル: AnswerController.cs プロジェクト: vallecbg/E-School
        public async Task <IActionResult> Create(AnswerInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.ViewData[GlobalConstants.QuestionId] = model.QuestionId;
                return(this.View());
            }

            await this.answerService.Create(model);


            return(this.RedirectToAction("Index", "Home"));
        }
コード例 #11
0
        public FubuContinuation AnswerCommand(AnswerInputModel inputModel)
        {
            var question = GetQuestionById(inputModel.QuestionId);

            question.Answers.Add(new Answer
            {
                Id   = Guid.NewGuid().ToString("D"),
                Body = inputModel.Body
            });

            return(FubuContinuation.RedirectTo(new QuestionInputModel {
                Id = inputModel.QuestionId
            }));
        }
コード例 #12
0
        public ActionResult Create(AnswerInputModel input)
        {
            if (input != null && this.ModelState.IsValid)
            {
                var userId = this.User.Identity.GetUserId();
                var answer = new Answer
                {
                    Content  = input.Content,
                    PostId   = input.PostId,
                    AuthorId = userId
                };

                this.Data.Answers.Add(answer);
                this.Data.SaveChanges();

                var post = this.Data.Posts.GetById(answer.PostId);
                post.LastActivity = answer.CreatedOn;

                this.Data.Posts.Update(post);
                this.Data.SaveChanges();

                if (post.AuthorId != userId)
                {
                    var newNotification = new Notification
                    {
                        NotificationType = NotificationType.AnswerPost,
                        ItemId           = answer.Id,
                        SenderId         = userId,
                        ReceiverId       = post.AuthorId
                    };

                    this.Data.Notifications.Add(newNotification);
                    this.Data.SaveChanges();

                    this.UpdateNotificationsCount(post.Author);
                }

                var viewModel = Mapper.Map <AnswerViewModel>(answer);

                return(this.PartialView("_AnswerDetailPartial", viewModel));
            }

            return(this.JsonError("Content is required"));
        }
コード例 #13
0
        public ActionResult Create(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var post = this.Data.Posts.GetById(id);

            if (post == null || post.IsDeleted || post.IsLocked)
            {
                return(this.HttpNotFound());
            }

            var inputModel = new AnswerInputModel {
                PostId = post.Id, Post = post.Title
            };

            return(this.PartialView(inputModel));
        }
コード例 #14
0
        public ActionResult <int> PostAnswer(AnswerInputModel answerModel)
        {
            Mapper           mapper    = new Mapper();
            AnswerDTO        answerDto = mapper.ConvertAnswerInputModelToAnswerDTO(answerModel);
            AuthorDataAccess answer    = new AuthorDataAccess();
            var question = answer.GetQuestionById(answerModel.QuestionID);

            if (question == null)
            {
                return(BadRequest("Вопроса не существует"));
            }
            if (string.IsNullOrWhiteSpace(answerModel.Value))
            {
                return(BadRequest("Введите ответ"));
            }
            if (answerModel.Correct == null)
            {
                return(BadRequest("Введите корректный ответ или нет"));
            }
            return(Ok(answer.AddAnswer(answerDto)));
        }
コード例 #15
0
        public ActionResult Answer(int id, AnswerInputModel input)
        {
            var q = RavenSession.Load <Question>(id);

            if (q == null)
            {
                return(HttpNotFound());
            }

            if (ModelState.IsValid)
            {
                q.Answers.Add(new Answer
                {
                    Comments        = new List <Comment>(),
                    Content         = input.Content,
                    CreatedByUserId = "users/1",                    // again, just a stupid default
                    CreatedOn       = DateTimeOffset.UtcNow,
                    Stats           = new Stats(),
                });
            }

            return(RedirectToAction("View", new { id = id }));
        }
コード例 #16
0
        public void CheckQuizResultShouldReturnTrueIFPassedAndNumberOfCorrectAnswers()
        {
            var quizViewModel = new QuizViewModel();

            quizViewModel.Questions = new List <QuestionViewModel>();

            var question = new QuestionViewModel()
            {
                QuestionText = "Question test",
            };

            var answer = new AnswerViewModel()
            {
                AnswerText      = "A",
                QuestionId      = question.Id,
                IsCorrectAnswer = true,
            };

            var anotherAnswer = new AnswerViewModel()
            {
                AnswerText      = "B",
                QuestionId      = question.Id,
                IsCorrectAnswer = false,
            };

            question.Answers = new List <AnswerViewModel>();
            question.Answers.Add(answer);
            question.Answers.Add(anotherAnswer);

            quizViewModel.Questions.Add(question);

            var questionInput = new QuestionInputModel()
            {
                QuestionText = "B??",
                QuizId       = "123",
            };
            var firstNewAnswer = new AnswerInputModel()
            {
                AnswerText      = "A",
                IsCorrectAnswer = true,
            };
            var secondNewAnswer = new AnswerInputModel()
            {
                AnswerText      = "B",
                IsCorrectAnswer = false,
            };

            questionInput.Answers = new List <AnswerInputModel>();
            questionInput.Answers.Add(firstNewAnswer);
            questionInput.Answers.Add(secondNewAnswer);

            var quizInputModel = new QuizInputModel();

            quizInputModel.Questions = new List <QuestionInputModel>();
            quizInputModel.Questions.Add(questionInput);

            var result = this.Service.CheckQuizResults(quizInputModel, quizViewModel);

            Assert.True(result.Item1);
            Assert.Equal(1, result.Item2);
        }
        public async Task <IActionResult> AddAnswer(AnswerInputModel input)
        {
            await this.service.AddAnswerAsync(input.QuestionId, input.Text);

            return(this.Redirect("/QuestionsAndAnswers/All"));
        }
コード例 #18
0
 public void RegisterAnswer(AnswerInputModel model) =>
 _answerService.RegisterAnswer(_mapper.Map <AnswerInputModel, Answer>(model));