public bool AddMCQ(MCQViewModel mcq) { if (mcq == null) { return(false); } var _mcq = new Mcq(); _mapper.Map(mcq, _mcq); _mcq.CreateOn = DateTime.Now; _mcq.IsDeleted = false; _unitOfWork.Repository <Mcq>().Add(_mcq); _unitOfWork.SaveChanges(); foreach (var item in mcq.Questions) { var mcqquestion = new Mcqquestion() { McqquestionId = _mcq.Id, QuestionId = item.Id }; _unitOfWork.Repository <Mcqquestion>().Add(mcqquestion); } _unitOfWork.SaveChanges(); return(true); }
public bool DeleteMCQ(MCQViewModel mcq) { var _mcq = _unitOfWork.Repository <Mcq>().FirstOrDefault(x => x.Id == mcq.Id); _mcq.IsDeleted = true; var _mcqQuestion = _unitOfWork.Repository <Mcqquestion>().Where(x => x.McqquestionId == _mcq.Id); _unitOfWork.Repository <Mcqquestion>().RemoveRange(_mcqQuestion); _unitOfWork.SaveChanges(); return(true); }
public IActionResult deleteMCQ([FromBody] MCQViewModel mcq) { ResponseViewModel response = new ResponseViewModel(); try { response.success = _mcqService.DeleteMCQ(mcq); } catch (Exception e) { response.success = false; response.errMsg = e.ToString(); } return(Json(response)); }
public IActionResult SubmitExam([FromBody] MCQViewModel mcq) { ResponseViewModel response = new ResponseViewModel(); try { var user = _userManager.GetUserAsync(HttpContext.User).Result; var _user = _userService.GetUserByUserName(user.UserName); response.success = true; response.data = _mcqService.SubmitExam(mcq, Int32.Parse(_user.Id)); } catch (Exception ex) { response.success = false; response.errMsg = ex.ToString(); } return(Json(response)); }
public int SubmitExam(MCQViewModel mcq, int userId) { double point = 0.0; var mcqTest = new UserTest() { Mcqid = mcq.Id, UserId = userId, Point = point, CreateOn = DateTime.Now }; _unitOfWork.Repository <UserTest>().Add(mcqTest); _unitOfWork.SaveChanges(); var _mcq = _unitOfWork.Repository <Mcq>().FirstOrDefault(x => x.Id == mcq.Id); _mcq.Attempts += 1; foreach (var question in mcq.Questions) { var mcqhistory = new Mcqhistory() { Mcqid = mcq.Id, UserTestId = mcqTest.Id, CreateOn = DateTime.Now, QuestionId = question.Id }; var answer = question.Answers.FirstOrDefault(x => x.DisplayNumber.Value == question.SelectedValue); if (answer != null) { mcqhistory.QuestionAnswerId = answer.Id; if (question.SelectedValue == question.CorrectAnswer) { point += question.Point.Value; } _unitOfWork.Repository <Mcqhistory>().Add(mcqhistory); } } mcqTest.Point = point; _unitOfWork.SaveChanges(); return(mcqTest.Id); }