Esempio n. 1
0
 private static async Task SavePollResult(Container pollContainer, PollResultDocument pollResult)
 {
     if (pollResult.ETag == null)
     {
         await pollContainer.CreateItemAsync(pollResult);
     }
     else
     {
         await pollContainer.ReplaceItemAsync(pollResult, pollResult.Id, new PartitionKey(pollResult.PartitionKey), new ItemRequestOptions { IfMatchEtag = pollResult.ETag });
     }
 }
Esempio n. 2
0
        private static PollResultDocument CalculateUpdatedResults(PollResultDocument pollResult, PollResponseDocument pollResponse)
        {
            pollResult = pollResult ?? new PollResultDocument(pollResponse.PartitionKey);
            var pollAnswerId = pollResponse.PollAnswerId;

            if (pollResult.PossibleAnswers.ContainsKey(pollResponse.PollAnswerId))
            {
                pollResult.PossibleAnswers[pollAnswerId] = pollResult.PossibleAnswers[pollAnswerId] + 1;
            }
            else
            {
                pollResult.PossibleAnswers.Add(pollAnswerId, 1);
            }
            return(pollResult);
        }
Esempio n. 3
0
        public async Task <Poll> GetPoll(string id)
        {
            var container = await Initialize();

            var          pollDocumentId = new DocumentId(id, id, nameof(PollDocument));
            PollDocument pollDocument   = null;

            try
            {
                pollDocument = (await container.ReadItemAsync <PollDocument>(pollDocumentId.Id, new PartitionKey(pollDocumentId.PartitionKey))).Resource;
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
                return(null);
            }
            var pollResultDocumentId = new DocumentId(id, id, nameof(PollResultDocument));
            PollResultDocument pollResultDocument = null;

            try
            {
                pollResultDocument = (await container.ReadItemAsync <PollResultDocument>(pollResultDocumentId.Id, new PartitionKey(pollResultDocumentId.PartitionKey))).Resource;
            }
            catch (CosmosException ex) when(ex.StatusCode == HttpStatusCode.NotFound)
            {
            }
            var pollAnswerResponses = pollResultDocument?.PossibleAnswers;

            return(new Poll
            {
                Question = pollDocument.Question,
                PossibleAnswers = pollDocument.PossibleAnswers.Select(answer => {
                    var responseCount = 0;
                    if (pollAnswerResponses?.ContainsKey(answer.Id) == true)
                    {
                        responseCount = pollAnswerResponses[answer.Id];
                    }
                    return new PollAnswer(answer.Id, answer.Answer, responseCount);
                }).ToList()
            });
        }