Exemplo n.º 1
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);
            }
        }
Exemplo n.º 2
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);
            }
        }