public ActionResult Create(QuestionCreateModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = (int) HttpStatusCode.BadRequest;
                return new JsonResult(ModelState.Values.SelectMany(v => v.Errors));
            }

            var question = new Question
            {
                Text = viewModel.Text,
                QuestionType = viewModel.QuestionType,
                Answer = viewModel.Answer,
                Notes = viewModel.Notes,
                CategoryId = viewModel.CategoryId,
                Choices = viewModel.Choices,
                Files = ManageFiles(Request.Files),
                CreatedBy = User.Identity.GetUserId(),
                UpdatedBy = User.Identity.GetUserId()
            };

            _questionRepository.Insert(question);

            _questionRepository.Save();

            return Json(null);
        }
Ejemplo n.º 2
0
        public IActionResult PostQuestion(int id, [FromBody] QuestionCreateModel value)
        {
            const string sql        = "INSERT INTO Question (Text, QuizId) VALUES(@Text, @QuizId); SELECT LAST_INSERT_ROWID();";
            var          questionId = _connection.ExecuteScalar(sql, new { Text = value.Text, QuizId = id });

            return(Created($"/api/quizzes/{id}/questions/{questionId}", null));
        }
Ejemplo n.º 3
0
 public void CreateQuestion(QuestionCreateModel questionCreateModel)
 {
     _questionRepository.CreateQuestion(
         Question.CreateQuestion(
             questionCreateModel.UserId,
             questionCreateModel.Text
             )
         );
 }
Ejemplo n.º 4
0
        public async Task <QuestionModelBase> Insert(QuestionCreateModel model)
        {
            var entity = _mapper.Map <Question>(model);
            await _context.Questions.AddAsync(entity);

            await SaveAsync();

            return(_mapper.Map <QuestionModelBase>(entity));
        }
Ejemplo n.º 5
0
        private DAL.Persistence.Question MapQuestionCreateModelToPersistence(QuestionCreateModel question)
        {
            var answerTuple = ParseAnswers(question);

            return(new DAL.Persistence.Question()
            {
                Id = Guid.NewGuid(),
                Title = question.Title,
                Answers = answerTuple.answers,
                CorrectAnswer = answerTuple.correctAnswer
            });
        }
        public IActionResult Create(Guid?uid, [Bind("UserId,CreatedDate,Text")] QuestionCreateModel questionCreateModel)
        {
            TempData["UId"] = uid;
            if (!ModelState.IsValid)
            {
                return(View(questionCreateModel));
            }

            _service.CreateQuestion(questionCreateModel);

            return(RedirectToAction(nameof(QuestionsPaginated)));
        }
Ejemplo n.º 7
0
        public async Task <IActionResult> Post([FromBody] QuestionCreateModel model)
        {
            if (ModelState.IsValid)
            {
                var question = await _service.Insert(model);

                return(question != null
                    ? (IActionResult)CreatedAtRoute(nameof(GetById), question, question.Id)
                    : Conflict());
            }
            return(BadRequest());
        }
Ejemplo n.º 8
0
        public async Task <QuestionGetModel> AskQuestionAsync(QuestionCreateModel questionModel)
        {
            var tags = await _tagService.GetTagsAsync(questionModel.TagIds);

            var user = await _userRepository.GetByIdAsync(questionModel.UserId);

            var question = Question.Create(user, questionModel.Title, questionModel.Body, tags, _limits);
            await _questionRepository.AddAsync(question);

            await _uow.SaveAsync();

            return(_mapper.Map <QuestionGetModel>(question));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> PostQuestion([FromBody] QuestionCreateModel model)
        {
            if (ModelState.IsValid)
            {
                var item = await _service.Insert(model);

                if (item != null)
                {
                    return(CreatedAtRoute(nameof(Get), item, item.Id));
                }
                return(Conflict());
            }
            return(BadRequest());
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Post([FromBody] QuestionCreateModel model)
        {
            if (ModelState.IsValid)
            {
                var question = await _service.Insert(model);

                if (question != null)
                {
                    return(Created($"/api/Questions/{question.Id}", question.Id));
                }
                return(Conflict());
            }
            return(BadRequest());
        }
Ejemplo n.º 11
0
        public async Task InsertNewQuestion()
        {
            var question = new QuestionCreateModel
            {
                Description = "VisualStudio 2019"
            };


            var result = await _service.Insert(question);


            result.Should().NotBeNull();
            result.Should().BeAssignableTo <QuestionModelBase>();
            result.Id.Should().NotBe(0);
        }
Ejemplo n.º 12
0
        public IActionResult PostQuestion(int id, [FromBody] QuestionCreateModel value)
        {
            const string quizSql = "SELECT * FROM Quiz WHERE Id = @Id;";
            var          quiz    = _connection.QuerySingleOrDefault <Quiz>(quizSql, new { Id = id });

            if (quiz == null)
            {
                return(NotFound());
            }

            const string sql        = "INSERT INTO Question (Text, QuizId) VALUES(@Text, @QuizId); SELECT LAST_INSERT_ROWID();";
            var          questionId = _connection.ExecuteScalar(sql, new { Text = value.Text, QuizId = id });

            return(Created($"/api/quizzes/{id}/questions/{questionId}", null));
        }
        public async Task Insert()
        {
            //Arrange
            var question = new QuestionCreateModel
            {
                Text        = "Some Order",
                Description = "Description"
            };
            //Act
            var result = await _service.Insert(question);

            //Assert
            result.Should().NotBeNull();
            result.Should().BeAssignableTo <QuestionModelBase>();
            result.Should().NotBe(0);
        }
Ejemplo n.º 14
0
        private (IEnumerable <Answer> answers, Answer correctAnswer) ParseAnswers(QuestionCreateModel question)
        {
            var    answers       = new List <Answer>();
            Answer correctAnswer = null;

            foreach (var createModel in question.Answers)
            {
                var parsedAnswer = MapAnswerCreateModelToPersistence(createModel);
                answers.Add(parsedAnswer);
                if (createModel.IsCorrect)
                {
                    correctAnswer = parsedAnswer;
                }
            }
            return(answers, correctAnswer);
        }
        public async Task InsertNewQuestion()
        {
            // Arrange
            var question = new QuestionCreateModel
            {
                Text        = "In what programming language are we writing right now",
                Description = "Languages are important to understand"
            };

            // Act
            var result = await _service.Insert(question);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeAssignableTo <QuestionModelBase>();
            result.Id.Should().NotBe(0);
        }
Ejemplo n.º 16
0
        public async Task InsertNewQuestion()
        {
            // Arrange
            var question = new QuestionCreateModel
            {
                QuestionDate = DateTime.Today,
                Venue        = "VisualStudio 2019"
            };

            // Act
            var result = await _service.Insert(question);

            // Assert
            result.Should().NotBeNull();
            result.Should().BeAssignableTo <QuestionModelBase>();
            result.Id.Should().NotBe(0);
        }
Ejemplo n.º 17
0
        public IActionResult Create(QuestionCreateModel question, IFormFile picOfQuestion, IFormFile picOfAnswer)
        {
            if (ModelState.IsValid)
            {
                var questionEntity = Mapper.Map <QuestionCreateModel, Question>(question);

                questionEntity.QuestionImageUrl = picOfQuestion.SavePictureFileUpload(_env);
                questionEntity.AnswerImageUrl   = picOfAnswer.SavePictureFileUpload(_env);

                questionEntity.CreatedDate = DateTime.Now;
                questionEntity.Status      = Status.New.ToString();
                questionEntity.AppUserId   = HttpContext.User.FindFirst(UserClaimTypes.UserId).Value;

                _questionRepository.Create(questionEntity);
            }
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 18
0
        public void CreateQuestion_WhenCalled_ExpectToCreateAQuestion()
        {
            // Arrange
            var sut = CreateSut();
            var questionCreateModel = new QuestionCreateModel
            {
                UserId = new Guid(),
                Topic  = "New topic",
                Text   = "New text"
            };

            // Act
            sut.CreateQuestion(questionCreateModel);

            // Assert
            Assert.AreEqual(Mock.Questions.Count, 6);
        }
        public async Task AQuizDoesNotExists_WhenPostingAQuestion_ReturnsNotFound()
        {
            const string QuizApiEndPoint = "/api/quizzes/999/questions";

            using (var testHost = new TestServer(new WebHostBuilder()
                                                 .UseStartup <Startup>()))
            {
                var        client   = testHost.CreateClient();
                const long quizId   = 999;
                var        question = new QuestionCreateModel("The answer to everything is what?");
                var        content  = new StringContent(JsonConvert.SerializeObject(question));
                content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                var response = await client.PostAsync(new Uri(testHost.BaseAddress, $"{QuizApiEndPoint}"), content);

                Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
            }
        }
Ejemplo n.º 20
0
        public async Task <ActionResult> Create(QuestionCreateModel createModel)
        {
            if (ModelState.IsValid)
            {
                if (!await _manager.CheckName(createModel.Name))
                {
                    ModelState.AddModelError("Name", "已有同名的题目。");
                }
                else
                {
                    await _manager.Create(createModel);

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

            return(View(createModel));
        }
Ejemplo n.º 21
0
        public async Task Create(QuestionCreateModel model)
        {
            var question = Mapper.Map <Question>(model);

            _db.Entry(question).State = EntityState.Added;

            if (question.QuestionType == QuestionTypes.Process2Drive)
            {
                question.Process2JudgeCode = new Process2JudgeCode
                {
                    Code          = model.Source,
                    Language      = model.Language,
                    MemoryLimitMb = model.MemoryLimitMb,
                    TimeLimitMs   = model.TimeLimit,
                    RunTimes      = model.RunTimes,
                    UpdateTime    = DateTime.Now,
                };
            }
            await _db.SaveChangesAsync();
        }
Ejemplo n.º 22
0
        public QuestionEditModel Question([FromBody] QuestionCreateModel createModel)
        {
            var result = _questionBackofficeService.Create(createModel);

            return(result);
        }
        public ActionResult Edit(QuestionCreateModel model)
        {
            if (!ModelState.IsValid)
            {
                Response.StatusCode = (int) HttpStatusCode.BadRequest;
                return new JsonResult(ModelState.Values.SelectMany(v => v.Errors));
            }

            if (model.DeletableFile != null)
            {
                foreach (var file in model.DeletableFile)
                {
                    _fileRepository.Delete(file.Id);
                    FileHelper.DeleteFile(file);
                }
                _fileRepository.Save();
            }

            var choices = _choiceRepository.FindAll(x => x.QuestionId == model.Id).ToList();

            if (choices.Count > 0)
            {
                foreach (var choice in choices)
                {
                    _choiceRepository.Delete(choice);
                }
                _choiceRepository.Save();
            }

            var entity = _questionRepository.Find(model.Id);

            entity.Text = model.Text;
            entity.Answer = model.Answer;
            entity.CategoryId = model.CategoryId;
            entity.Notes = model.Notes;
            entity.QuestionType = model.QuestionType;
            entity.Files = ManageFiles(Request.Files);
            entity.Choices = model.Choices;

            _questionRepository.Update(entity);
            _questionRepository.Save();

            return Json(null);
        }
Ejemplo n.º 24
0
        public QuestionEditModel Create(QuestionCreateModel question)
        {
            Guid id = _questionRepository.Add(MapQuestionCreateModelToPersistence(question));

            return(QuizBackofficeService.MapQuestionPersistnenceToEditModel(_questionRepository.Get(id)));
        }