Пример #1
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);
            }
        }
Пример #2
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
                });
            }
        }
Пример #3
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);
            }
        }