public void Validate(QuestionCreateCommand request) { if (request.QuestionType == QuestionType.EssayQuestion && request.Choices?.Count > 0) { throw new ArgumentException(ESSAY_QUESTION_WITH_CHOICES); } if (request.QuestionType == QuestionType.SingleChoiceQuestion) { if (request.Choices?.Count < 2) { throw new ArgumentException(CHOICE_QUESTION_WITHOUT_CHOICES); } } if (request.QuestionType == QuestionType.MultipleChoiceQuestion) { if (request.Choices?.Count < 2) { throw new ArgumentException(CHOICE_QUESTION_WITHOUT_CHOICES); } } if (request.Choices?.Count > 0 && !request.Choices.Any(q => q.RightAnswer)) { throw new ArgumentException(CHOICE_QUESTION_WITOUT_RIGHT_ANSWER); } }
public async Task <IActionResult> Post(QuestionCreateCommand command) { try { var id = await _mediator.Send(command); return(Ok(new { Id = id })); } catch (ArgumentException ex) { return(BadRequest(new { Error = ex.Message })); } catch (Exception ex) { return(StatusCode(500, new { Error = ex.Message })); } }