public async Task <ActionResult <CreateExamVM> > Create([FromBody] CreateExamVM createExamViewModel) { if (ModelState.IsValid) { try { var exam = _mapper.Map <Exam>(createExamViewModel); exam.TeacherId = User.GetLoggedInUserId <long>(); for (int i = 0; i < exam.Questions.Count; i++) { exam.Questions[i].QuestionNumInExam = i + 1; for (int j = 0; j < exam.Questions[i].Answers.Count; j++) { exam.Questions[i].Answers[j].AnswerNumInQuestion = j + 1; } } await _unitOfWork.Exams.AddAsync(exam); await _unitOfWork.SaveAsync(); } catch (Exception) { throw; } } else { return(BadRequest(ModelState)); } return(View()); }
public IActionResult Create(CreateExamVM model) { Wired wired = Wired.GetWireds().FirstOrDefault(f => f.ID == model.wired); if (ModelState.IsValid) { try { Test Test = new Test { ReleaseDate = DateTime.Now, title = wired.title, content = wired.content }; repoTest.Add(Test); for (int i = 0; i < model.Question.Length; i++) { Question question = new Question { name = model.Question[i], TestID = Test.ID }; repoQuestion.Add(question); repoOption.Add(new Option { answer = model.A[i], pIndex = 1, QuestionID = question.ID, correct = model.correct[i] == 1 ? true : false }); repoOption.Add(new Option { answer = model.B[i], pIndex = 2, QuestionID = question.ID, correct = model.correct[i] == 2 ? true : false }); repoOption.Add(new Option { answer = model.C[i], pIndex = 3, QuestionID = question.ID, correct = model.correct[i] == 3 ? true : false }); repoOption.Add(new Option { answer = model.D[i], pIndex = 4, QuestionID = question.ID, correct = model.correct[i] == 4 ? true : false }); } return(RedirectToAction(nameof(Index))); } catch (Exception) { return(View(model)); } } else { return(View()); } }