public async Task <IActionResult> PutQuestionItem( [FromServices] QuestionContext context, long id, [FromBody] QuestionItemDTO questionItemDTO) { if (id != questionItemDTO.Id) { return(BadRequest()); } var questionItem = await context.QuestionItems.FindAsync(id); if (questionItem == null) { return(NotFound()); } questionItem.Question = questionItemDTO.Question; questionItem.Options = questionItemDTO.Options; questionItem.CorrectQuestion = questionItemDTO.CorrectQuestion; questionItem.KnowMore = questionItemDTO.KnowMore; if (ModelState.IsValid) { context.QuestionItems.Add(questionItem); await context.SaveChangesAsync(); } else { return(BadRequest(ModelState)); } try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!QuestionItemExists(context, id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <QuestionItemDTO> > PostQuestionItem( [FromServices] QuestionContext context, [FromBody] QuestionItemDTO questionItemDTO) { var questionItem = new QuestionItem { Id = questionItemDTO.Id, Question = questionItemDTO.Question, Options = questionItemDTO.Options, CorrectQuestion = questionItemDTO.CorrectQuestion, KnowMore = questionItemDTO.KnowMore, }; if (ModelState.IsValid) { context.QuestionItems.Add(questionItem); } else { return(BadRequest(ModelState)); } try { await context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { throw; } return(CreatedAtAction( nameof(GetQuestionItem), new { id = questionItem.Id }, ItemToDTO(questionItem) )); }