public async Task <IActionResult> GetMultipleChoicesAnswerById([FromRoute] int id) { var multipleChoicesAnswer = await _service.MultipleChoicesAnswerService.GetByIdAsync(id); if (multipleChoicesAnswer == null) { return(ErrorResult($"Can not found Multiple Choices Answer with Id: {id}")); } var multipleChoicesAnswerRes = new MultipleChoicesAnswerModel { AnswerId = multipleChoicesAnswer.AnswerId, Id = multipleChoicesAnswer.Id, MultipleChoicesExerciseId = multipleChoicesAnswer.MultipleChoicesExerciseId, Result = multipleChoicesAnswer.Result }; return(SuccessResult(multipleChoicesAnswerRes, "Get Multiple Choices Answer successfully.")); }
public async Task <IActionResult> CreateMultipleChoicesAnswer([FromBody] MultipleChoicesAnswerModel model) { var newMultipleChoicesAnswer = new MultipleChoicesAnswer(); try { if (!ModelState.IsValid) { ValidModel(); } //check if this question has been done yet var existingMultipleChoicesAnswer = await _service.MultipleChoicesAnswerService.CheckExistingBasedOnAnswerIdAndExerciseId(model.AnswerId, model.MultipleChoicesExerciseId); if (existingMultipleChoicesAnswer == true) { return(ErrorResult("You has already done this question.")); } //check if multiple choices answer is existing var multipleChoicesExercise = await _service.MultipleChoicesExerciseService.GetByIdAsync(model.MultipleChoicesExerciseId); if (multipleChoicesExercise == null) { return(ErrorResult($"The Exercise with Id :{model.MultipleChoicesExerciseId} does not exist.")); } newMultipleChoicesAnswer = new MultipleChoicesAnswer() { AnswerId = model.AnswerId, MultipleChoicesExerciseId = model.MultipleChoicesExerciseId, Result = model.Result }; await _service.MultipleChoicesAnswerService.AddAsync(newMultipleChoicesAnswer); } catch (Exception e) { return(ErrorResult(e.Message)); } return(SuccessResult(newMultipleChoicesAnswer, "Created Multiple Choices Answer successfully.")); }