public async Task <IActionResult> GetEssayAnswerById([FromRoute] int id) { var essayAnswer = await _service.EssayAnswerService.GetByIdAsync(id); if (essayAnswer == null) { return(ErrorResult($"Can not found Essay Answer with Id: {id}")); } var essayAnswerRes = new EssayAnswerModel { Id = essayAnswer.Id, AnswerId = essayAnswer.AnswerId, EssayExerciseId = essayAnswer.EssayExerciseId, Result = essayAnswer.Result }; return(SuccessResult(essayAnswerRes, "Get Essay Answer successfully.")); }
public async Task <IActionResult> CreateEssayAnswer([FromBody] EssayAnswerModel model) { var essayAnswer = new EssayAnswer(); try { if (!ModelState.IsValid) { ValidModel(); } //check if this question has been done yet var existingEssayAnswer = await _service.EssayAnswerService.CheckExistingBasedOnAnswerIdAndExerciseId(model.AnswerId, model.EssayExerciseId); if (existingEssayAnswer == true) { return(ErrorResult("You has already done this question.")); } //check if multiple choices answer is existing var essayExercise = await _service.EssayExerciseService.GetByIdAsync(model.EssayExerciseId); if (essayExercise == null) { return(ErrorResult($"The Exercise with Id :{model.EssayExerciseId} does not exist.")); } essayAnswer = new EssayAnswer() { AnswerId = model.AnswerId, EssayExerciseId = model.EssayExerciseId, Result = model.Result }; await _service.EssayAnswerService.AddAsync(essayAnswer); } catch (Exception e) { return(ErrorResult(e.Message)); } return(SuccessResult(essayAnswer, "Created Essay Answer successfully.")); }