public ActionResult Question(string slug) { if (!UserAuth.IsLogedIn()) { return(RedirectToAction("Login", "Member")); } var result = _memberQuizService.SaveMemberQuizInit(UserAuth.User.UserId, slug); if (result.Success) { ViewBag.MemberQuizList = JsonConvert.SerializeObject(result.MemberQuizAnswerList); ViewBag.MemberResultId = result.MemberResultId; var TotalTimeData = _generalSettingService.GetGeneralSetting("total-time"); ViewBag.TotalTimeMinute = string.IsNullOrWhiteSpace(TotalTimeData) ? "60" : TotalTimeData; ViewBag.diffInSeconds = (DateTime.Now - result.StartTime).TotalSeconds; ViewBag.SortOrder = result.SortOrder; ViewBag.totelQuestion = result.MemberQuizAnswerList.Count; var entity = _quizService.GetQuizBySlug(slug); return(View(entity)); } else { return(HttpNotFound()); } }
public MemberQuizListDto SaveMemberQuizInit(int memberId, string slug) { var result = new MemberQuizListDto(); var quiz = _quizRepository.AsNoTracking.FirstOrDefault(w => w.Slug == slug && w.IsActive); if (quiz == null) { result.Success = false; result.Message = "Test not found."; return(result); } if (!_memberResultRepository.AsNoTracking.Any(a => a.MemberId == memberId && a.QuizId == quiz.Id && a.EndTime == null)) { var entity = new MemberResult() { MemberId = memberId, QuizId = quiz.Id, CreatedAt = DateTime.Now, StartTime = DateTime.Now }; _memberResultRepository.Insert(entity); _unitOfWork.Commit(); var totalNumberQuestionData = _generalSettingService.GetGeneralSetting("total-question-display"); var totalNumberQuestion = string.IsNullOrWhiteSpace(totalNumberQuestionData) ? 30 : Convert.ToInt32(totalNumberQuestionData); var questionList = _quizQuestionRepository.AsNoTracking.Where(x => x.QuizId == entity.QuizId).OrderBy(o => Guid.NewGuid()).Select(s => s.Id).Skip(0).Take(totalNumberQuestion).ToList(); var i = 1; foreach (var questionId in questionList) { var memberQuizEntity = new MemberQuizAnswer() { MemberResultId = entity.Id, QuizQuestionId = questionId, CreatedAt = DateTime.Now, SortOrder = i }; _memberQuizAnswerRepository.Insert(memberQuizEntity); _unitOfWork.Commit(); var quizAnswerList = _quizAnswerRepository.AsNoTracking .Where(w => w.QuizQuestionId == memberQuizEntity.QuizQuestionId) .OrderBy(o => Guid.NewGuid()).Select(s => new { s.Id }).ToList(); var j = 1; foreach (var quizAnswer in quizAnswerList) { var memberQuizAnswerOptions = new MemberQuizAnswerOptions() { MemberQuizAnswerId = memberQuizEntity.Id, QuizAnswerId = quizAnswer.Id, SortOrder = j }; _memberQuizAnswerOptionsRepository.Insert(memberQuizAnswerOptions); j++; } _unitOfWork.Commit(); i++; } } var memberResult = _memberResultRepository.AsNoTracking.FirstOrDefault(a => a.MemberId == memberId && a.QuizId == quiz.Id && a.EndTime == null); result.MemberResultId = memberResult.Id; result.StartTime = memberResult.StartTime; result.Success = true; result.SortOrder = 1; var memberQuiz = _memberQuizAnswerRepository.AsNoTracking.Where(w => w.MemberResultId == memberResult.Id); result.MemberQuizAnswerList = memberQuiz.OrderBy(o => o.SortOrder).ToList(); var quizOrder = memberQuiz.Where(w => w.QuizAnswerId != null).OrderByDescending(o => o.UpdatedAt).FirstOrDefault(); if (quizOrder != null) { result.SortOrder = quizOrder.SortOrder; } return(result); }