Exemplo n.º 1
0
        public async Task <IActionResult> Post([FromBody] AnswerViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (model.Id != 0)
            {
                ModelState.AddModelError("id", "Adding an answer, id should not be set");
                return(BadRequest(ModelState));
            }

            if (!await _context.Questions.AnyAsync(q => q.Id == model.QuestionId && q.QuizId == model.QuizId))
            {
                return(NotFound("no question found"));
            }

            var answer = new Answer
            {
                QuizId     = model.QuizId,
                QuestionId = model.QuestionId,
                Value      = model.Value,
                Text       = model.Text
            };

            await _context.AddAsync(answer);

            await _context.SaveChangesAsync();

            return(Ok(answer.Adapt <AnswerViewModel>()));
        }
        public void Post_WhenCorrectViewModelButErrorDuringProcessing_ShouldReturnStatusCode500()
        {
            var answer = new Answer
            {
                Id   = 1,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.CreateAnswer(It.IsAny <Answer>())).Throws(new Exception());

            var result = _sut.Post(answer.Adapt <AnswerViewModel>()).Result as StatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StatusCode, 500);
        }
        public void Put_WhenCorrectViewModelErrorDuringProcessing_ShouldReturnNotFound()
        {
            var answerId = 2;
            var answer   = new Answer
            {
                Id   = answerId,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.UpdateAnswer(It.Is <Answer>(a => a.Id == answerId))).Returns(Task.FromResult((Answer)null));

            var result = _sut.Put(answer.Adapt <AnswerViewModel>()).Result;

            Assert.IsNotNull(result);
            Assert.IsInstanceOf <NotFoundObjectResult>(result);
        }
        public void Put_WhenErrorDuringProcessing_ShouldReturnStatusCode500()
        {
            var answerId = 1;
            var answer   = new Answer
            {
                Id   = answerId,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.UpdateAnswer(It.Is <Answer>(a => a.Id == answerId))).Throws(new Exception());

            var result = _sut.Put(answer.Adapt <AnswerViewModel>()).Result as StatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StatusCode, 500);
        }
        public void Post_WhenCorrectViewModelGiven_ShouldReturnJsonViewModel()
        {
            var answer = new Answer
            {
                Id   = 1,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.CreateAnswer(It.IsAny <Answer>())).Returns(Task.FromResult(answer));

            var result = _sut.Post(answer.Adapt <AnswerViewModel>()).Result as JsonResult;

            Assert.IsNotNull(result);
            var viewModel = result.GetObjectFromJsonResult <AnswerViewModel>();

            Assert.AreEqual(viewModel.Text, answer.Text);
            Assert.AreEqual(viewModel.Id, answer.Id);
        }
Exemplo n.º 6
0
        public IActionResult Put([FromBody] AnswerViewModel model)
        {
            if (model == null)
            {
                // Return a generic HTTP Status 500 (Server Error)
                return(new StatusCodeResult(500));
            }

            var answer = new Answer
            {
                QuestionId       = model.QuestionId,
                Text             = model.Text,
                Notes            = model.Notes,
                CreatedDate      = DateTime.Now,
                LastModifiedDate = DateTime.Now
            };

            _dbContext.Answers.Add(answer);

            return(new JsonResult(answer.Adapt <AnswerViewModel>(), JsonSettings));
        }