Ejemplo n.º 1
0
        public QuestionPosted AllQuestionVotes(QuestionPosted question, VoteEnum vote)
        {
            var allQuestionVotes = question.Votes;

            allQuestionVotes.Append(vote);
            return(new QuestionPosted(question.QuestionId, question.Title, question.Body, question.Tags, allQuestionVotes.Sum(v => Convert.ToInt32(v)), allQuestionVotes));
        }
        public QuestionPosted VoteUpdate(QuestionPosted question, VoteEnum vote)
        {
            var allvotes = question.AllVotes;

            allvotes.Append(vote);
            return(new QuestionPosted(question.QuestionId, question.Question, allvotes.Sum(v => Convert.ToInt32(v)), allvotes));
        }
Ejemplo n.º 3
0
        public QuestionCreated VoteUp1(QuestionCreated question, VoteEnum vote)
        {
            var vot = question.Vot;

            vot.Append(vote);
            return(new QuestionCreated(question.QuefileTi, question.Question, vot.Sum(v => Convert.ToInt32(v)), vot));
        }
Ejemplo n.º 4
0
        public async Task VoteMealAsync(string userId, VoteEnum vote, Guid mealId)
        {
            var existing = await _dbContext.MealVotes
                           .Where(mv => mv.MealId == mealId)
                           .Where(mv => mv.UserId == userId)
                           .SingleOrDefaultAsync();

            if (existing is not null)
            {
                //withdraw vote
                if (vote == existing.Vote)
                {
                    _dbContext.MealVotes.Remove(existing);
                    return;
                }

                //If different, then apply change
                existing.Vote = vote;
                return;
            }

            await _dbContext.MealVotes.AddAsync(new MealVote
            {
                MealId = mealId,
                UserId = userId,
                Vote   = vote
            });
        }
Ejemplo n.º 5
0
        public QuestionPosted UpdateVote(QuestionPosted question, VoteEnum vote)
        {
            var votes = question.AllVotes;

            votes.Append(vote);

            return(new QuestionPosted(question.Title, question.Tags, votes.Sum(v => Convert.ToInt32(v)), votes));
        }
Ejemplo n.º 6
0
        public QuestionAdded Update(QuestionAdded result, VoteEnum newVote)                                                                                    //update votes counter on every new vote added to the list
        {
            var totalvotes = result.TotalVotes.ToList();                                                                                                       //get current list of votes

            totalvotes.Add(newVote);                                                                                                                           //add current vote to the list

            return(new QuestionAdded(result.Id, result.Title, result.Text, result.Tags, totalvotes.Sum(nrOfVotes => Convert.ToInt32(nrOfVotes)), totalvotes)); //update question data
        }
Ejemplo n.º 7
0
 public static KeyValuePair <VoteEnum, PollMode>[] GetPollModes(this VoteEnum vote) =>
 vote
 .GetFlags()
 .Select(v => KeyValuePair.Create(v, v.GetPollMode())).Where(_ => _.Value != null)
 .DefaultIfEmpty(KeyValuePair.Create(vote, vote.GetPollMode())).Where(_ => _.Value != null)
 .Select(pair => KeyValuePair.Create(pair.Key, pair.Value.Value))
 .OrderBy(pair => pair.Key.GetAttributes()?.Get <DisplayAttribute>()?.GetOrder())
 .ToArray();
Ejemplo n.º 8
0
            //adds new vote to AllVotes list and then changes the score
            public QuestionPosted Update(QuestionPosted quest, VoteEnum vote)
            {
                var lines = quest.AllVotes.ToList();

                lines.Add(vote);

                return(new QuestionPosted(quest.QuestionId, quest.Title, lines, lines.Sum()));
            }
Ejemplo n.º 9
0
 public VoteGrouping(VoteEnum flag, int order, int displayOrder, string singular, string plural)
 {
     Order          = order;
     DisplayOrder   = displayOrder;
     Singular       = singular;
     Plural         = plural;
     Flag           = flag;
     NestedGrouping = null;
 }
Ejemplo n.º 10
0
        public QuestionPosted UpdateVote(QuestionPosted question, VoteEnum vote)
        {
            var votes = question.AllVotes;

            votes.Append(vote);

            //the score obtained from the votes must correspond to the sum of all the votes of the individual

            return(new QuestionPosted(question.Title, question.Tags, votes.Sum(v => Convert.ToInt32(v)), votes));
        }
Ejemplo n.º 11
0
        public static IEnumerable <VoteEnum> GetFlags(VoteEnum vote)
        {
            var processed = VoteEnum.None;

            foreach (var possibleVote in Enums.GetValues <VoteEnum>(EnumMemberSelection.DisplayOrder | EnumMemberSelection.Distinct))
            {
                if (vote.HasAllFlags(possibleVote) && !processed.HasAllFlags(possibleVote))
                {
                    processed |= possibleVote;
                    yield return(possibleVote);
                }
            }
        }
Ejemplo n.º 12
0
 public static TextBuilder Format(this VoteEnum vote, TextBuilder builder) =>
 GetFlags(vote).Aggregate(builder, (b, v) => b.Sanitize(v.AsString(EnumFormat.DisplayName)));
Ejemplo n.º 13
0
 public static string Description(this VoteEnum vote) =>
 (vote.RemoveFlags(VoteEnum.Modifiers) is var voteWithoutModifiers && voteWithoutModifiers.HasAnyFlags() ?
Ejemplo n.º 14
0
 // TODO: check for overflow
 public static VoteEnum IncreaseVotesCount(this VoteEnum vote, int diff) => vote
 .RemoveFlags(VoteEnum.Plus)
 .CombineFlags((VoteEnum)((((int)vote.CommonFlags(VoteEnum.Plus) >> FirstPlusBit) + diff) << FirstPlusBit));
Ejemplo n.º 15
0
 public static PollMode?GetPollMode(this VoteEnum vote) =>
 ourVoteToPollMode.TryGetValue(vote, out var pollMode) ? pollMode : default;
Ejemplo n.º 16
0
 public void AddVote(VoteEnum vote)
 {
     Votes.Add(vote);
 }
Ejemplo n.º 17
0
        public AskQuestionResult.QuestionAdded Update(AskQuestionResult.QuestionAdded question, VoteEnum vote)
        {
            var votes = question.Votes.ToList();

            votes.Add(vote);

            return(new AskQuestionResult.QuestionAdded(question.Id, question.Title, question.Text, question.Tags, votes.Sum(score => Convert.ToInt32(score)), votes));
        }
Ejemplo n.º 18
0
 public VoteGrouping(VoteEnum flag, int order, int displayOrder, string singular, string plural, params VoteGrouping[] nestedGrouping) :
     this(flag, order, displayOrder, singular, plural)
 {
     NestedGrouping = nestedGrouping;
 }
Ejemplo n.º 19
0
        public ActionResult Vote(int postId, VoteEnum type)
        {
            if (cService.GetPost(postId).UserId == _currentUserId)
            {
                return(Json(new { result = "", message = "Bạn không thể tự bình chọn cho chính mình." }));
            }
            Vote vote = new Vote();

            vote.PostId    = postId;
            vote.VotedDate = DateTime.Now;
            vote.Type      = type;
            vote.UserId    = _currentUserId;
            bool check = qService.Vote(vote);

            if (check && type == VoteEnum.VoteUp)
            {
                if (vote.Post.UserId != _currentUserId)
                {
                    //new Thread(() =>
                    //{
                    Question question;
                    NotificationSettingEnum notiType;
                    int userId;
                    if (vote.Post is Question)
                    {
                        question = qService.GetQuestion(vote.PostId);
                        notiType = NotificationSettingEnum.VotedQuestion;
                        userId   = question.UserId;
                    }
                    else
                    {
                        question = qService.GetQuestion(((Answer)vote.Post).QuestionId);
                        notiType = NotificationSettingEnum.VotedAnswer;
                        userId   = vote.Post.UserId;
                    }

                    Notification notification = new Notification();
                    notification.AuthorId    = _currentUserId;
                    notification.CreatedDate = DateTime.Now;
                    notification.Content     = question.Title;
                    notification.Seen        = false;
                    notification.Type        = notiType;
                    notification.UserId      = userId;
                    notification.Link        = Url.Action("Detail", "Question", new { id = question.Id });
                    cService.AddNotification(notification);

                    using (RealTimeService rService = new RealTimeService(new CPMathHubModelContainer(), notification.UserId))
                    {
                        IEnumerable <string> connectionIds = RealTimeHub.Connections.GetConnections(notification.UserId);
                        foreach (string conId in connectionIds)
                        {
                            _hub.Clients.Client(conId).notifyNewActivity(rService.CountNewActivityNotification());
                        }
                    }
                    //}
                    //).Start();
                }
                return(Json(new { result = "up" }));
            }
            else if (check && type == VoteEnum.VoteDown)
            {
                return(Json(new { result = "down" }));
            }
            else
            {
                return(Json(new { result = "", message = "Bạn không thể bình chọn nhiều hơn 1 lần." }));
            }
        }