public ActionResult GetLawQuestionsDetails(int lawId) { QuestionsService service = new QuestionsService(); LawQuestionsModel model = service.GetQuestionsForLaw(lawId, User.Identity.GetUserId()); return(View("LawQuestionsDetails", model)); }
public LawQuestionsModel GetQuestionsForLaw(int lawID, string userID) { var auService = new AnonymousUserService(); using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create()) { var law = context.Laws .Where(x => x.LawID == lawID) .Include("Questions.UserRepresentativeQuestions.Representative.Party") .FirstOrDefault(); if (law == null) { return(null); } var result = new LawQuestionsModel { LawID = lawID, Law = law, Questions = new List <QuestionModel>(), }; result.TotalQuestionsMade = law.Questions.SelectMany(x => x.UserRepresentativeQuestions).Count(); if (result.TotalQuestionsMade == 0) { return(result); } var lawQuestionCounts = law.Questions .Where(x => x.Verified) .SelectMany(x => x.UserRepresentativeQuestions) .GroupBy(x => x.QuestionID) .Select(g => new { QuestionId = g.Key, Count = g.Count() }) .ToList(); var representativeQuestionCount = law.Questions .Where(x => x.Verified) .SelectMany(x => x.UserRepresentativeQuestions) .GroupBy(x => new { x.RepresentativeID, x.QuestionID }) .Select(g => new { g.Key.QuestionID, g.Key.RepresentativeID, Count = g.Count() }) .ToList(); var representativeIDs = law.Questions .Where(x => x.Verified) .SelectMany(x => x.UserRepresentativeQuestions) .Select(x => x.RepresentativeID) .ToList(); var questionIDs = law.Questions .Where(x => x.Verified) .Select(x => x.QuestionID).ToList(); var questionLikeCounts = context.QuestionLikes .Where(x => questionIDs.Contains(x.QuestionID)) .GroupBy(x => new { x.QuestionID, x.Vote }) .Select(g => new { QuestionID = g.Key.QuestionID, Vote = g.Key.Vote, Count = g.Count() }) .ToList(); var userQuestionLikes = context.QuestionLikes .Where(x => questionIDs.Contains(x.QuestionID) && x.ApplicationUserID == userID) .ToList(); var answers = context.Answers .Where(x => representativeIDs.Contains(x.RepresentativeID) && questionIDs.Contains(x.QuestionID)) .ToList(); var answerIDs = answers.Select(x => x.AnswerID).ToList(); var answerLikesCounts = context.AnswerLikes .Where(x => answerIDs.Contains(x.AnswerID)) .GroupBy(x => new { x.AnswerID, x.Vote }) .Select(g => new { AnswerID = g.Key.AnswerID, Vote = g.Key.Vote, Count = g.Count() }) .ToList(); var userAnswerLikes = context.AnswerLikes .Where(x => x.ApplicationUserID == userID && answerIDs.Contains(x.AnswerID)) .ToList(); foreach (var question in law.Questions.Where(x => x.Verified)) { QuestionModel qmodel = PopulateQuestionModel(question); qmodel.AskedCount = lawQuestionCounts.Where(x => x.QuestionId == question.QuestionID).Select(x => x.Count).FirstOrDefault(); qmodel.LikesCount = questionLikeCounts.Where(x => x.QuestionID == question.QuestionID && x.Vote).Select(x => x.Count).FirstOrDefault(); qmodel.DislikesCount = questionLikeCounts.Where(x => x.QuestionID == question.QuestionID && !x.Vote).Select(x => x.Count).FirstOrDefault(); var userQuestionLike = userQuestionLikes.Where(x => x.ApplicationUserID == userID && x.QuestionID == question.QuestionID).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(userID)) { qmodel.UserLiked = userQuestionLike == null ? (bool?)null : userQuestionLike.Vote; } else { qmodel.UserLiked = auService.GetUserQuestionLike(question.QuestionID); } qmodel.Representatives = new List <QustionRepresentativeModel>(); foreach (var rep in question.UserRepresentativeQuestions.Select(x => x.Representative)) { // Collection has item per question so it has to be added only once if (qmodel.Representatives.Any(x => x.ID == rep.RepresentativeID)) { continue; } QustionRepresentativeModel repModel = PopulateQuestionRepresentativeModel(rep); repModel.AskedCount = representativeQuestionCount .Where(x => x.QuestionID == question.QuestionID && x.RepresentativeID == rep.RepresentativeID) .Select(x => x.Count) .First(); var answer = answers.Where(x => x.RepresentativeID == rep.RepresentativeID && x.QuestionID == question.QuestionID).FirstOrDefault(); if (answer != null) { repModel.Answered = true; repModel.Answer = PopulateAnswerModel(answer); repModel.Answer.LikesCount = answerLikesCounts.Where(x => x.AnswerID == answer.AnswerID && x.Vote).Select(x => x.Count).FirstOrDefault(); repModel.Answer.DislikesCount = answerLikesCounts.Where(x => x.AnswerID == answer.AnswerID && !x.Vote).Select(x => x.Count).FirstOrDefault(); if (!string.IsNullOrWhiteSpace(userID)) { var userAnswer = userAnswerLikes.Where(x => x.AnswerID == answer.AnswerID && x.ApplicationUserID == userID).FirstOrDefault(); repModel.Answer.UserLiked = userAnswer == null ? (bool?)null : userAnswer.Vote; } else { repModel.Answer.UserLiked = auService.GetUserAnswerLike(answer.AnswerID); } } qmodel.Representatives.Add(repModel); } qmodel.AnswersCount = qmodel.Representatives.Count(x => x.Answered); result.Questions.Add(qmodel); } return(result); } }