public IActionResult CreateQuestion([FromBody] QuestionForCreationDto question) { if (question == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResultTunsBad(ModelState)); } var questionEntity = Mapper.Map <Question>(question); _quizRepository.AddQuestion(questionEntity); if (!_quizRepository.Save()) { throw new Exception("Creating an question failed on save."); } var questionToReturn = Mapper.Map <QuestionDto>(questionEntity); return(CreatedAtRoute("CreateQuestion", new { id = questionToReturn.Id }, questionToReturn)); }
public async Task <IActionResult> CreateQuestion(int valueId, QuestionForCreationDto question) { question.ValueId = valueId; var questionFromMapper = mapper.Map <Question>(question); repository.Add(questionFromMapper); if (await repository.SaveAll()) { var questionToReturn = mapper.Map <QuestionForReturnDto>(questionFromMapper); return(Ok(questionToReturn)); } return(BadRequest("Nie udało sie dodać zapytania")); }
public async Task <IActionResult> CreateQuestions(int quizId) { for (int i = 0; i < 15; i++) { var quizQuestionDto = new QuestionForCreationDto(); quizQuestionDto.QuizId = quizId; var quizQuestion = _mapper.Map <QuizQuestion>(quizQuestionDto); quizQuestion.CorrectAnswer = 1; _repo.AddToRepo(quizQuestion); } if (await _repo.SaveAll()) { return(Ok()); } throw new Exception("Nie udalo sie utworzyc pytań do quizu"); }
public async Task <IActionResult> CreateQuestion([FromBody] QuestionForCreationDto model) { // maps resource to domain model var question = _mapper.Map <Question>(model); try { // create question var questionCreated = await _questionService.Create(question, model.LessonId); // retuns 200ok and the question resource return(Ok(_mapper.Map <QuestionDto>(questionCreated))); } catch (Exception ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }
public static async Task PostWithAllDataNeededOk() { Mock <IQuestionService> moqQuestionService = new Mock <IQuestionService>(); var controller = new QuestionController(moqQuestionService.Object); var creationDto = new QuestionForCreationDto { ImageUrl = "ImageUrl", Choices = new List <ChoiseForCreationDto>(), PublishedAt = DateTimeOffset.Now, Question = "Question", ThumbUrl = "ThumbUrl" }; //Act var result = await controller.Post(creationDto); //Assert Assert.NotNull(result); ProcessAcationResult <OkObjectResult>(result, StatusCodes.Status200OK); }
public async Task <IActionResult> Post([FromBody] QuestionForCreationDto creationDto) { if (creationDto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new Helpers.UnprocessableEntityObjectResult(ModelState)); } var questionDto = new QuestionDto(); questionDto.CopyPropertiesFrom(creationDto); await _questionService.Create(questionDto); return(Ok(questionDto)); }
public static async Task PostWithMissingPublishedAtUnprocessableEntity() { //Arrange Mock <IQuestionService> moqQuestionService = new Mock <IQuestionService>(); var controller = new QuestionController(moqQuestionService.Object); var creationDto = new QuestionForCreationDto { ImageUrl = "ImageUrl", Choices = new List <ChoiseForCreationDto>(), PublishedAt = null, Question = "Question", ThumbUrl = "ThumbUrl" }; controller.ModelState.AddModelError("PublishedAt Null", "PublishedAt Required"); //Act var result = await controller.Post(creationDto); //Assert Assert.NotNull(result); ProcessAcationResult <API.Helpers.UnprocessableEntityObjectResult>(result, StatusCodes.Status422UnprocessableEntity); }
public async Task <IActionResult> CreateQuestionAsync(int examId, QuestionForCreationDto questionForCreation) { var exam = await _repo.ExamRepository.GetExamAsync(examId); if (exam == null) { return(BadRequest("Exam not exists")); } if (exam.AuthorId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var question = _mapper.Map <Question>(questionForCreation); question.ExamId = exam.Id; _repo.QuestionRepository.Add(question); if (await _repo.SaveAllAsync()) { var questionForReturn = _mapper.Map <QuestionForReturnDto>(question); return(CreatedAtRoute("GetQuestionAsync", new { questionId = question.Id }, questionForReturn)); } return(BadRequest("Failed to save a question")); }