public async Task <ActionResult <SetQuestion> > PutSetQuestion(int setId, int questionId, [FromBody] SetQuestionIM item) { var set = await _context.Sets.FindAsync(setId); if (set == null) { return(NotFound("set not found")); } var @question = await _context.SetQuestions.FindAsync(questionId); if (@question == null) { return(NotFound("question not found")); } if (questionId != item.Id) { return(BadRequest("inconsistent data")); } question.Text = item.Text; question.Description = item.Description; question.Points = item.Points; await _context.SaveChangesAsync(); return(Ok(question)); }
public async Task <ActionResult <SetQuestion> > PostSetQuestion(int setId, int termId, int roleId, [FromBody] SetQuestionIM item) { var set = await _context.Sets.FindAsync(setId); if (set == null) { return(NotFound("set not found")); } var @role = await _context.SetRoles.FindAsync(roleId); if (@role == null) { return(NotFound("role not found")); } var @term = await _context.SetTerms.FindAsync(termId); if (@term == null) { return(NotFound("term not found")); } if (term.SetId != set.Id && role.SetId != set.Id) { return(BadRequest("role or term is outside this set")); } int maxQuestionOrder; try { maxQuestionOrder = _context.SetQuestions.Where(sq => sq.SetRoleId == roleId && sq.SetTermId == termId).Max(sq => sq.Order) + 1; } catch { maxQuestionOrder = 0; } var newQuestion = new SetQuestion { SetTermId = termId, SetRoleId = roleId, Text = item.Text, Description = item.Description, Points = item.Points, Order = maxQuestionOrder }; _context.SetQuestions.Add(newQuestion); await _context.SaveChangesAsync(); return(Ok(newQuestion)); }