示例#1
0
        public async Task <IActionResult> Post([FromBody] ResultViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (model.Id != 0)
            {
                ModelState.AddModelError("id", "Add new result the id should not be set.");
                return(BadRequest(ModelState));
            }
            if (!_context.Quizzes.Any(q => q.Id == model.QuizId))
            {
                return(NotFound(@"no quiz id = {model.quizId} found"));
            }

            var result = new Result
            {
                QuizId   = model.QuizId,
                Text     = model.Text,
                MinValue = model.MinValue,
                MaxValue = model.MaxValue
            };

            await _context.AddAsync(result);

            await _context.SaveChangesAsync();

            return(Json(result.Adapt <ResultViewModel>()));
        }
示例#2
0
        public void Put_WhenErrorDuringProcessing_ShouldReturnStatusCode500()
        {
            var resultId    = 1;
            var resultModel = new Result
            {
                Id   = resultId,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.UpdateResult(It.Is <Result>(r => r.Id == resultId))).Throws(new Exception());

            var result = _sut.Put(resultModel.Adapt <ResultViewModel>()).Result as StatusCodeResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.StatusCode, 500);
        }
示例#3
0
        public void Put_WhenCorrectViewModelGiven_ShouldReturnJsonViewModel()
        {
            var resultId    = 1;
            var resultModel = new Result
            {
                Id   = resultId,
                Text = "Text1"
            };

            _mockRepo.Setup(x => x.UpdateResult(It.Is <Result>(r => r.Id == resultId))).Returns(Task.FromResult(resultModel));

            var result = _sut.Put(resultModel.Adapt <ResultViewModel>()).Result as JsonResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.GetObjectFromJsonResult <ResultViewModel>().Text, resultModel.Text);
            Assert.AreEqual(result.GetObjectFromJsonResult <ResultViewModel>().Id, resultModel.Id);
        }
 protected virtual Result <TModel> MapToModel(Result <TEntity> entities)
 {
     return(entities.Adapt <Result <TModel> >());
 }