예제 #1
0
        public object Any(Ask request)
        {
            AskResponse response = new AskResponse();

            response.Result = ErrorCode.OK;

            Guid creatorId = UserSession.GetUserId();

            DateTime      dateTime      = DateTime.UtcNow;
            QuestionEntry questionEntry = new QuestionEntry(creatorId, Guid.NewGuid())
            {
                Title        = request.Title,
                Detail       = request.Detail,
                Creation     = dateTime,
                Modification = dateTime,
                Tags         = string.Join(",", request.Tags).ToLowerInvariant()
            };

            TableRepository tableRepository = new TableRepository();

            tableRepository.InsertOrReplace(questionEntry, Tables.Questions);

            IndexHelper.CreateIndex(questionEntry.GetId(), request.Title + " " + questionEntry.Tags, Tables.Questions);

            return(response);
        }
예제 #2
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);
        }