Example #1
0
        public object Any(Love request)
        {
            Guid userId = UserSession.GetUserId();

            LoveResponse response = new LoveResponse
            {
                Result = ErrorCode.OK,
            };

            TableRepository tableRepository = new TableRepository();

            UserQuestionEntry userQuestionEntry = tableRepository.Get <UserQuestionEntry>(Tables.UserQuestion, request.QuestionId, userId);

            // Création d'un nouveau vote
            if (userQuestionEntry == null)
            {
                DateTime dateTime = DateTime.UtcNow;
                userQuestionEntry = new UserQuestionEntry(request.QuestionId, userId)
                {
                    Creation     = dateTime,
                    Modification = dateTime
                };
            }
            else
            {
                userQuestionEntry.Modification = DateTime.UtcNow;
            }


            userQuestionEntry.Love = !userQuestionEntry.Love;

            // insert le vote
            tableRepository.InsertOrReplace(userQuestionEntry, Tables.UserQuestion);

            return(response);
        }
Example #2
0
        public object Any(Vote request)
        {
            Guid userId = UserSession.GetUserId();

            VoteResponse response = new VoteResponse
            {
                Result = ErrorCode.OK,
            };

            TableRepository tableRepository = new TableRepository();

            UserQuestionEntry userQuestionEntry = tableRepository.Get <UserQuestionEntry>(Tables.UserQuestion, request.QuestionId, userId);

            // Création d'un nouveau vote
            if (userQuestionEntry == null)
            {
                DateTime dateTime = DateTime.UtcNow;
                userQuestionEntry = new UserQuestionEntry(request.QuestionId, userId)
                {
                    Creation     = dateTime,
                    Modification = dateTime
                };
            }
            else
            {
                userQuestionEntry.Modification = DateTime.UtcNow;
            }

            Guid target = request.VoteTarget == VoteTarget.Question ? request.QuestionId : request.OwnerId;

            HashSet <string> votesUp   = new HashSet <string>(userQuestionEntry.VotesUp?.Split('|') ?? new string[] { });
            HashSet <string> votesDown = new HashSet <string>(userQuestionEntry.VotesDown?.Split('|') ?? new string[] { });

            VoteKind oldValue = VoteKind.None;

            if (votesUp.Contains(target.ToString()))
            {
                oldValue = VoteKind.Up;
            }
            else if (votesDown.Contains(target.ToString()))
            {
                oldValue = VoteKind.Down;
            }

            response.VoteKind = request.VoteKind;

            votesUp.Remove(target.ToString());
            votesDown.Remove(target.ToString());

            if (response.VoteKind == oldValue)
            {
                response.VoteKind = VoteKind.None;
            }
            else
            {
                switch (response.VoteKind)
                {
                case VoteKind.Up:
                    votesUp.Add(target.ToString());
                    break;

                case VoteKind.Down:
                    votesDown.Add(target.ToString());
                    break;
                }
            }

            userQuestionEntry.VotesUp   = votesUp.Join("|");
            userQuestionEntry.VotesDown = votesDown.Join("|");

            if (request.VoteTarget == VoteTarget.Answer)
            {
                AnswerEntry answerEntry = tableRepository.Get <AnswerEntry>(Tables.Answers, request.QuestionId, request.OwnerId);
                answerEntry.Votes -= (int)oldValue;
                answerEntry.Votes += (int)response.VoteKind;
                tableRepository.InsertOrMerge(answerEntry, Tables.Answers);
                response.VoteValue = answerEntry.Votes;

                // badges
                if (response.VoteKind == VoteKind.Up)
                {
                    switch (answerEntry.Votes)
                    {
                    case 1: AllBadges.Approved.CreateIfNotExist(tableRepository, answerEntry.GetAnswerOwnerId()); break;

                    case 10: AllBadges.NiceAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break;

                    case 25: AllBadges.GoodAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break;

                    case 100: AllBadges.GreatAnswer.CreateForQuestion(tableRepository, answerEntry.GetAnswerOwnerId(), request.QuestionId); break;
                    }
                }
            }
            else
            {
                QuestionEntry questionEntry = tableRepository.Get <QuestionEntry>(Tables.Questions, request.OwnerId, request.QuestionId);
                questionEntry.Votes -= (int)oldValue;
                questionEntry.Votes += (int)response.VoteKind;
                tableRepository.InsertOrMerge(questionEntry, Tables.Questions);
                response.VoteValue = questionEntry.Votes;

                // badges
                if (response.VoteKind == VoteKind.Up)
                {
                    switch (questionEntry.Votes)
                    {
                    case 1: AllBadges.Padawan.CreateIfNotExist(tableRepository, questionEntry.GetOwnerId()); break;

                    case 10: AllBadges.NiceQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break;

                    case 25: AllBadges.GoodQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break;

                    case 100: AllBadges.GreatQuestion.CreateForQuestion(tableRepository, questionEntry.GetOwnerId(), request.QuestionId); break;
                    }
                }
            }

            // insert le vote
            tableRepository.InsertOrReplace(userQuestionEntry, Tables.UserQuestion);


            // badges
            if (response.VoteKind == VoteKind.Up)
            {
                AllBadges.Supporter.CreateIfNotExist(tableRepository, userId);
            }
            else if (response.VoteKind == VoteKind.Down)
            {
                AllBadges.Critic.CreateIfNotExist(tableRepository, userId);
            }

            return(response);
        }
Example #3
0
        public object Any(Answers request)
        {
            TableRepository tableRepository = new TableRepository();
            CloudTable      questionTable   = tableRepository.GetTable(Tables.Questions);

            Guid questionId = Guid.Parse(request.Id);

            TableQuery <QuestionEntry> questionQuery = questionTable.CreateQuery <QuestionEntry>();

            QuestionEntry questionEntry = (from k in questionQuery
                                           where k.RowKey == questionId.ToString()
                                           select k).SingleOrDefault();

            if (questionEntry == null)
            {
                throw HttpError.NotFound("Such question do not exist");
            }

            questionEntry.Views++;
            tableRepository.InsertOrMerge(questionEntry, Tables.Questions);

            CloudTable answerTable = tableRepository.GetTable(Tables.Answers);
            TableQuery <AnswerEntry> answerQuery = answerTable.CreateQuery <AnswerEntry>();

            AnswerEntry[] answerEntries = (from k in answerQuery
                                           where k.PartitionKey == questionId.ToString()
                                           select k).ToArray();


            UserQuestionEntry userQuestionEntry = UserSession.IsAuthenticated
                                            ? tableRepository.Get <UserQuestionEntry>(Tables.UserQuestion, questionId, UserSession.GetUserId())
                                            : null;

            HashSet <string> votesUp   = new HashSet <string>(userQuestionEntry?.VotesUp?.Split('|') ?? new string[] { });
            HashSet <string> votesDown = new HashSet <string>(userQuestionEntry?.VotesDown?.Split('|') ?? new string[] { });

            Func <Guid, VoteKind> getVoteKind = ownerId =>
            {
                if (votesUp.Contains(ownerId.ToString()))
                {
                    return(VoteKind.Up);
                }

                if (votesDown.Contains(ownerId.ToString()))
                {
                    return(VoteKind.Down);
                }

                return(VoteKind.None);
            };

            AnswersResponse answersResponse = new AnswersResponse
            {
                Id             = questionEntry.GetId(),
                Creation       = questionEntry.Creation,
                Owner          = CreateUserCard(tableRepository, questionEntry.GetOwnerId()),
                Detail         = questionEntry.Detail,
                Tags           = questionEntry.Tags.SplitAndTrimOn(new char[] { ',' }),
                Title          = questionEntry.Title,
                Views          = questionEntry.Views,
                Votes          = questionEntry.Votes,
                SelectedAnswer = questionEntry.SelectedAnswer,
                Answers        = answerEntries.Select(k => new AnswerResult
                {
                    Owner    = CreateUserCard(tableRepository, k.GetAnswerOwnerId()),
                    Creation = k.Creation,
                    Content  = k.Content,
                    Votes    = k.Votes,
                    VoteKind = getVoteKind(k.GetAnswerOwnerId())
                }).ToArray()
            };

            // quest user vote for this question
            answersResponse.VoteKind = getVoteKind(questionId);
            answersResponse.Love     = userQuestionEntry?.Love ?? false;

            return(answersResponse);
        }