示例#1
0
        internal QuizResultsModel GetQuizResults(int id, string userId)
        {
            var auService = new AnonymousUserService();

            using (var context = ApplicationDbContext.Create())
            {
                var quiz = context
                           .Quizes
                           .Include(x => x.Items)
                           .Include("Items.Law")
                           .Include("Items.LawSection.Law")
                           .Where(x => x.QuizId == id)
                           .FirstOrDefault();

                if (quiz == null)
                {
                    return(null);
                }

                var result = new QuizResultsModel
                {
                    QuizId            = quiz.QuizId,
                    TimeCreated       = quiz.TimeCreated,
                    Description       = quiz.Description,
                    Title             = quiz.Title,
                    ImageRelativePath = quiz.ImageRelativePath,
                    Items             = new List <QuizItemResultsModel>()
                };


                foreach (var questionItem in quiz.Items)
                {
                    QuizItemResultsModel item = null;
                    if (questionItem.Type == QuizItemType.Law)
                    {
                        item = PopulateResultsForLawAnswer(userId, auService, context, questionItem);
                    }
                    else if (questionItem.Type == QuizItemType.LawSection)
                    {
                        item = PopulateResultsForLawSectionAnswer(userId, auService, context, questionItem);
                    }
                    result.Items.Add(item);
                }

                return(result);
            }
        }
示例#2
0
        public RepresentativeModel GetRepresentative(int representativeID, string userId = null)
        {
            Dictionary <int, bool> userLikedQuestions = null;
            Dictionary <int, bool> userLikedAnswers   = null;

            var auService = new AnonymousUserService();

            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var result = new RepresentativeModel
                {
                    Laws = new List <RepresentativeLawModel>()
                };

                var representative = context.Representatives
                                     .Where(x => x.RepresentativeID == representativeID)
                                     .Include(x => x.Party)
                                     .Include("ParliamentHouse.Parliament.ParliamentHouses")
                                     .Include(x => x.ExternalLinks)
                                     .Include(x => x.Assignments)
                                     .FirstOrDefault();

                if (representative == null)
                {
                    return(null);
                }

                var questions = context.UserRepresentativeQuestions
                                .Where(x => x.RepresentativeID == representativeID)
                                .Where(x => x.Question.Verified)
                                .GroupBy(x => new
                {
                    RepID        = x.RepresentativeID,
                    QuestionID   = x.QuestionID,
                    QuestionText = x.Question.Text,
                    LawID        = x.Question.LawID,
                    AskedTimeUtc = x.Question.CreateTimeUtc
                })
                                .Select(g => new QuestionHelper
                {
                    QuestionID   = g.Key.QuestionID,
                    QuestionText = g.Key.QuestionText,
                    RepID        = g.Key.RepID,
                    LawID        = g.Key.LawID,
                    AskedTimeUtc = g.Key.AskedTimeUtc,
                    Count        = g.Count()
                })
                                .ToList();

                var lawIDs      = questions.Select(x => x.LawID).ToList();
                var questionIDs = questions.Select(x => x.QuestionID).ToList();

                var laws = context.Laws
                           .Where(x => lawIDs.Contains(x.LawID))
                           .ToList();

                var answers = context.Answers
                              .Where(a => a.RepresentativeID == representativeID)
                              .Where(x => questionIDs.Contains(x.QuestionID))
                              .ToList();

                var answerIDs = answers.Select(x => x.AnswerID).ToList();

                var lawVotingResults = context.LawVotes
                                       .Where(x => lawIDs.Contains(x.LawID))
                                       .GroupBy(x => x.Vote)
                                       .Select(g => new { Key = g.Key, Count = g.Count() })
                                       .ToList();

                var questionLikeCounts = context.QuestionLikes
                                         .Where(x => questionIDs.Contains(x.QuestionID))
                                         .GroupBy(x => new { x.QuestionID, x.Vote })
                                         .Select(g => new LikeHelper {
                    ObjectID = g.Key.QuestionID, Vote = g.Key.Vote, Count = g.Count()
                })
                                         .ToList();

                var answerLikesCounts = context.AnswerLikes
                                        .Where(x => answerIDs.Contains(x.AnswerID))
                                        .GroupBy(x => new { x.AnswerID, x.Vote })
                                        .Select(g => new LikeHelper {
                    ObjectID = g.Key.AnswerID, Vote = g.Key.Vote, Count = g.Count()
                })
                                        .ToList();

                if (userId != null)
                {
                    userLikedAnswers = context.AnswerLikes
                                       .Where(x => x.ApplicationUserID == userId)
                                       .Where(x => x.Answer.RepresentativeID == representativeID)
                                       .ToDictionary(k => k.AnswerID, v => v.Vote);

                    userLikedQuestions = context.QuestionLikes
                                         .Where(x => x.Question.UserRepresentativeQuestions.Select(y => y.RepresentativeID).Contains(representativeID))
                                         .Where(x => x.ApplicationUserID == userId)
                                         .ToDictionary(k => k.QuestionID, v => v.Vote);
                }
                else
                {
                    userLikedQuestions = auService.GetUserQuestionLikes();
                    userLikedAnswers   = auService.GetUserAnswerLikes();
                }

                result.TotalQuestions = questionIDs.Distinct().Count();
                result.TotalAnswers   = answerIDs.Distinct().Count();
                CalculateFlagsForRepresentative(result);

                // local vars
                List <JavnaRasprava.WEB.Models.Representative.RepresentativeQuestionModel> questionModelList = null;
                IEnumerable <QuestionHelper> questionsToIterate = null;

                // Process laws to get questions and answers on laws
                foreach (var law in laws)
                {
                    questionModelList = new List <Models.Representative.RepresentativeQuestionModel>();
                    var lawModel = new RepresentativeLawModel
                    {
                        ID        = law.LawID,
                        Title     = law.Title,
                        Questions = new List <Models.Representative.RepresentativeQuestionModel>()
                    };
                    var unsafeResult = lawVotingResults.Where(x => x.Key.HasValue && x.Key.Value).FirstOrDefault();
                    lawModel.VotesUp = unsafeResult == null ? 0 : unsafeResult.Count;

                    unsafeResult       = lawVotingResults.Where(x => x.Key.HasValue && !x.Key.Value).FirstOrDefault();
                    lawModel.VotesDown = unsafeResult == null ? 0 : unsafeResult.Count;

                    lawModel.VotesDownPercentage = Infrastructure.Math.Percentage(lawModel.VotesDown, lawModel.VotesDown + lawModel.VotesUp);
                    lawModel.VotesUpPercentage   = Infrastructure.Math.Percentage(lawModel.VotesUp, lawModel.VotesDown + lawModel.VotesUp);

                    questionsToIterate = questions.Where(x => x.LawID == law.LawID);
                    questionModelList  = PopulateQuestionsModelInternal(questionsToIterate, answers, questionLikeCounts, answerLikesCounts, userLikedQuestions, userLikedAnswers);

                    lawModel.Questions.AddRange(questionModelList);
                    lawModel.LatestAnswerTime = questionModelList.First().AnswerTime;
                    result.Laws.Add(lawModel);
                }

                // process questions asked directly to rep
                questionsToIterate = questions.Where(x => x.LawID == null);
                questionModelList  = PopulateQuestionsModelInternal(questionsToIterate, answers, questionLikeCounts, answerLikesCounts, userLikedQuestions, userLikedAnswers);
                result.Questions   = new List <Models.Representative.RepresentativeQuestionModel>(questionModelList.OrderByDescending(x => x.AnswerTime));

                result.Representative          = representative;
                result.IsSingleHouseParliament = representative.ParliamentHouse.Parliament.ParliamentHouses.Count() == 1;
                result.Laws = result.Laws.OrderByDescending(x => x.LatestAnswerTime).ToList();

                return(result);
            }
        }
示例#3
0
        private static QuizItemResultsModel PopulateResultsForLawAnswer(string userId, AnonymousUserService auService, ApplicationDbContext context, QuizItem questionItem)
        {
            // Populate general law data
            var item = new QuizItemResultsModel
            {
                LawId               = questionItem.Law.LawID,
                LawTitle            = questionItem.Law.Title,
                QuestionDescription = questionItem.Law.Text,
                ImageRelativePath   = questionItem.Law.ImageRelativePath,
                QuestionType        = QuizItemType.Law
            };

            // This is copied from Law service. Look to make it generic with expression trees
            var lawVotingResults = context.LawVotes
                                   .Where(x => x.LawID == questionItem.Law.LawID)
                                   .GroupBy(x => x.Vote)
                                   .Select(g => new { Key = g.Key, Count = g.Count() })
                                   .ToList();

            var unsafeResult = lawVotingResults.Where(x => x.Key.HasValue && x.Key.Value).FirstOrDefault();

            item.VotesUp = unsafeResult == null ? 0 : unsafeResult.Count;

            unsafeResult   = lawVotingResults.Where(x => x.Key.HasValue && !x.Key.Value).FirstOrDefault();
            item.VotesDown = unsafeResult == null ? 0 : unsafeResult.Count;

            item.VotesDownPercentage = Infrastructure.Math.Percentage(item.VotesDown, item.VotesDown + item.VotesUp);
            item.VotesUpPercentage   = Infrastructure.Math.Percentage(item.VotesUp, item.VotesDown + item.VotesUp);

            if (userId != null)
            {
                var userVote = context.LawVotes
                               .Include(x => x.LawCustomVote)
                               .Where(x => x.ApplicationUserID == userId && x.LawID == questionItem.Law.LawID)
                               .FirstOrDefault();

                if (userVote != null)
                {
                    item.UserVote = userVote.Vote;
                    if (userVote.LawCustomVote != null)
                    {
                        item.CustomUserVoteText = userVote.LawCustomVote.Text;
                    }
                }
            }
            else
            {
                item.UserVote           = auService.GetUserLawVoteBool(questionItem.Law.LawID);
                item.CustomUserVoteText = auService.GetUserLawVoteCustomText(questionItem.Law.LawID);
            }

            return(item);
        }
示例#4
0
        public QuizAnsweringModel GetQuizAnsweringModel(int id, int?itemOrderId, string userId)
        {
            using (var context = ApplicationDbContext.Create())
            {
                var result = new QuizAnsweringModel
                {
                    QuizId = id
                };

                var quiz = context
                           .Quizes
                           .Include(x => x.Items)
                           .Include("Items.Law")
                           .Include("Items.LawSection.Law")
                           .Where(x => x.QuizId == id)
                           .FirstOrDefault();

                if (quiz == null)
                {
                    return(null);
                }

                var item = quiz.Items.SingleOrDefault(x => x.Order == itemOrderId);
                if (itemOrderId.HasValue && item == null)
                {
                    return(null);
                }

                // populate quiz general data
                result.Title        = quiz.Title;
                result.Description  = quiz.Description;
                result.QuestionType = itemOrderId.HasValue ? item.Type : (QuizItemType?)null;



                // populate next item
                if (quiz.Items.Count > 0)                   // if no items in list quiz is already over.
                {
                    var orderedItems = quiz.Items.OrderBy(x => x.Order).ToList();
                    if (!itemOrderId.HasValue)
                    {
                        result.NextQuestionId = orderedItems.First().Order;
                    }
                    else
                    {
                        int currentItemIndex = orderedItems.FindIndex(x => x.Order == itemOrderId);
                        if (orderedItems.Count > currentItemIndex + 1)
                        {
                            result.NextQuestionId = orderedItems[currentItemIndex + 1].Order;
                        }
                        else
                        {
                            result.NextQuestionId = -1;
                        }
                    }

                    result.ProgressPercentage = JavnaRasprava.WEB.Infrastructure.Math.Percentage(itemOrderId.HasValue ? itemOrderId.Value : 0,
                                                                                                 quiz.Items.Count).ToString();
                }

                result.CurrentItemIndex = itemOrderId;
                result.TotalItems       = quiz.Items.Count;


                if (item == null)
                {
                    result.ImageRelativePath = quiz.ImageRelativePath;
                }
                else
                {
                    AnonymousUserService auService = new AnonymousUserService();


                    // Populate law
                    if (item.Type == QuizItemType.Law)
                    {
                        result.LawId               = item.Law.LawID;
                        result.LawTitle            = item.Law.Title;
                        result.QuestionDescription = item.Law.Text;
                        result.LawVotes            = LawService.GetLawCustomVotesListInternal(item.Law.LawID, context);
                        result.ImageRelativePath   = item.Law.ImageRelativePath;

                        if (userId == null)
                        {
                            if (auService.HasVotedLaw(item.Law.LawID))
                            {
                                result.UserVoteId         = auService.GetUserLawVote(item.Law.LawID);
                                result.CustomUserVoteText = auService.GetUserLawVoteCustomText(item.Law.LawID);
                            }
                        }
                        else
                        {
                            var userLawVote = context.LawVotes
                                              .Include(x => x.LawCustomVote)
                                              .Where(x => x.ApplicationUserID == userId)
                                              .Where(x => x.LawID == item.Law.LawID)
                                              .FirstOrDefault();

                            if (userLawVote != null)
                            {
                                if (userLawVote.LawCustomVote != null)
                                {
                                    result.UserVoteId         = -1;
                                    result.CustomUserVoteText = userLawVote.LawCustomVote.Text;
                                }
                                else
                                {
                                    result.UserVoteId = userLawVote.Vote.Value ? -3 : -2;
                                }
                            }
                        }
                    }
                    else
                    {
                        result.LawId               = item.LawSection.LawID;
                        result.LawTitle            = item.LawSection.Law.Title;
                        result.SectionId           = item.LawSection.LawSectionID;
                        result.SectionTitle        = item.LawSection.Title;
                        result.QuestionDescription = item.LawSection.Text;
                        result.SectionVotes        = LawService.GetLawSectionCustomVotesListInternal(item.LawSection.LawID, item.LawSection.LawSectionID, context);
                        result.ImageRelativePath   = item.LawSection.ImageRelativePath;

                        if (userId == null)
                        {
                            if (auService.HasVotedLawSection(item.LawSection.LawSectionID))
                            {
                                result.UserVoteId         = auService.GetUserLawSectionVote(item.LawSection.LawSectionID);
                                result.CustomUserVoteText = auService.GetUserLawSectionVoteCustomText(item.LawSection.LawSectionID);
                            }
                        }
                        else
                        {
                            var userLawSectionVote = context.LawSectionVotes
                                                     .Include(x => x.LawSectionCustomVote)
                                                     .Where(x => x.LawSectionID == item.LawSection.LawSectionID)
                                                     .Where(x => x.ApplicationUserID == userId)
                                                     .FirstOrDefault();

                            if (userLawSectionVote != null)
                            {
                                if (userLawSectionVote.LawSectionCustomVote != null)
                                {
                                    result.UserVoteId         = -1;
                                    result.CustomUserVoteText = userLawSectionVote.LawSectionCustomVote.Text;
                                }
                                else
                                {
                                    result.UserVoteId = userLawSectionVote.Vote.Value ? -3 : -2;
                                }
                            }
                        }
                    }
                }

                return(result);
            }
        }
示例#5
0
        public QuestionModel LikeQuestion(int questionID, string userId, bool value)
        {
            AnonymousUserService auService = new AnonymousUserService();

            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                bool            userLiked = false;
                ApplicationUser user      = null;
                if (userId == null)
                {
                    user      = auService.GetAnonymousUser(context);
                    userLiked = auService.HasLikedQuestion(questionID);
                }
                else
                {
                    user = context.Users.Where(x => x.Id == userId).FirstOrDefault();
                    if (user == null)
                    {
                        return(null);
                    }

                    var like = context.QuestionLikes.Where(x => x.ApplicationUserID == userId && x.QuestionID == questionID).FirstOrDefault();
                    userLiked = like != null;
                }

                var question = context.Questions.Where(x => x.QuestionID == questionID).FirstOrDefault();
                if (question == null)
                {
                    return(null);
                }

                if (!userLiked)
                {
                    context.QuestionLikes.Add(new DomainModels.QuestionLike
                    {
                        QuestionID        = questionID,
                        ApplicationUserID = user.Id,
                        Vote = value,
                    });
                    context.SaveChanges();


                    if (userId == null)
                    {
                        auService.LikeQuestion(questionID, value);
                    }
                }

                var questionLikesCounts = context.QuestionLikes
                                          .Where(x => x.QuestionID == questionID)
                                          .GroupBy(x => new { x.QuestionID, x.Vote })
                                          .Select(g => new { QuestionID = g.Key.QuestionID, Vote = g.Key.Vote, Count = g.Count() })
                                          .ToList();

                var answersCount = context.Answers.Where(x => x.QuestionID == questionID).Count();

                // TODO:
                return(new QuestionModel
                {
                    AnswersCount = answersCount,
                    DislikesCount = questionLikesCounts.Where(x => !x.Vote).Select(x => x.Count).FirstOrDefault(),
                    Id = questionID,
                    LikesCount = questionLikesCounts.Where(x => x.Vote).Select(x => x.Count).FirstOrDefault(),
                    UserLiked = value
                });
            }
        }
示例#6
0
        public AnswerModel LikeAnswer(int answerID, string userId, bool value)
        {
            AnonymousUserService auService = new AnonymousUserService();

            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                bool            userLiked = false;
                ApplicationUser user      = null;
                if (userId == null)
                {
                    user      = auService.GetAnonymousUser(context);
                    userLiked = auService.HasLikedAnswer(answerID);
                }
                else
                {
                    user = context.Users.Where(x => x.Id == userId).FirstOrDefault();
                    if (user == null)
                    {
                        return(null);
                    }

                    var like = context.AnswerLikes.Where(x => x.ApplicationUserID == user.Id && x.AnswerID == answerID).FirstOrDefault();
                    userLiked = like != null;
                }

                var answer = context.Answers
                             .Where(x => x.AnswerID == answerID)
                             .Include(x => x.Question)
                             .FirstOrDefault();
                if (answer == null)
                {
                    return(null);
                }

                if (!userLiked)
                {
                    context.AnswerLikes.Add(new DomainModels.AnswerLike
                    {
                        AnswerID          = answerID,
                        ApplicationUserID = user.Id,
                        Vote = value,
                    });

                    context.SaveChanges();

                    if (userId == null)
                    {
                        auService.LikeAnswer(answerID, value);
                    }
                }

                var answerLikesCounts = context.AnswerLikes
                                        .Where(x => x.AnswerID == answerID)
                                        .GroupBy(x => new { x.AnswerID, x.Vote })
                                        .Select(g => new { AnswerID = g.Key.AnswerID, Vote = g.Key.Vote, Count = g.Count() })
                                        .ToList();

                var result = PopulateAnswerModel(answer);
                result.LikesCount    = answerLikesCounts.Where(x => x.AnswerID == answer.AnswerID && x.Vote).Select(x => x.Count).FirstOrDefault();
                result.DislikesCount = answerLikesCounts.Where(x => x.AnswerID == answer.AnswerID && !x.Vote).Select(x => x.Count).FirstOrDefault();

                result.UserLiked = value;

                return(result);
            }
        }
示例#7
0
        public AnswersListModel GetLatestAnswersForLaw(int lawID, string userID = null, int count = 3)
        {
            AnonymousUserService auService = new AnonymousUserService();

            using (var context = JavnaRasprava.WEB.DomainModels.ApplicationDbContext.Create())
            {
                var result = new AnswersListModel
                {
                    LawID   = lawID,
                    Answers = new List <AnswerModel>()
                };

                var answersDictionary = new Dictionary <int, AnswerModel>();

                ApplicationUser user = null;
                if (string.IsNullOrWhiteSpace(userID))
                {
                    user = auService.GetAnonymousUser(context);
                }
                else
                {
                    user = context.Users.Where(x => x.Id == userID).FirstOrDefault();
                    if (user == null)
                    {
                        return(null);
                    }
                }

                var law = context.Laws.Where(x => x.LawID == lawID).FirstOrDefault();
                if (law == null)
                {
                    return(null);
                }

                context.Questions
                .Where(x => x.LawID == lawID)
                .Where(x => x.Verified)
                .SelectMany(x => x.Answers)
                .Include(x => x.Question)
                .Include(x => x.Representative.Party)
                .OrderByDescending(x => x.AnsweredTimeUtc)
                .Take(count)
                .ToList()
                .ForEach(x => answersDictionary[x.AnswerID] = PopulateAnswerModel(x));

                var answerIDs = answersDictionary.Keys.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();

                foreach (var ual in answerLikesCounts)
                {
                    if (ual.Vote)
                    {
                        answersDictionary[ual.AnswerID].LikesCount = ual.Count;
                    }
                    else
                    {
                        answersDictionary[ual.AnswerID].DislikesCount = ual.Count;
                    }
                }

                if (userID != null)
                {
                    var userAnswerLikes = context.AnswerLikes
                                          .Where(x => x.ApplicationUserID == userID && answerIDs.Contains(x.AnswerID))
                                          .ToList();

                    foreach (var ual in userAnswerLikes)
                    {
                        answersDictionary[ual.AnswerID].UserLiked = ual.Vote;
                    }
                }
                else
                {
                    foreach (var answer in answersDictionary.Values)
                    {
                        answer.UserLiked = auService.GetUserAnswerLike(answer.ID);
                    }
                }

                result.Answers = answersDictionary.Values.ToList();
                result.Count   = result.Answers.Count;

                return(result);
            }
        }
示例#8
0
        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);
            }
        }