Esempio n. 1
0
        // POST /
        public async Task <SavePollResponse> SaveAsync(Poll poll)
        {
            try
            {
                await _pollRepository.AddAsync(poll);

                await _unitOfWork.CompleteAsync();

                return(new SavePollResponse(poll));
            }
            catch (Exception ex)
            {
                return(new SavePollResponse($"Could not save poll: {ex.Message}"));
            }
        }
        private async Task SeedPollsAndPollAnswers()
        {
            var id1   = ObjectId.GenerateNewId().ToString();
            var id2   = ObjectId.GenerateNewId().ToString();
            var polls = new List <Poll>
            {
                new Poll
                {
                    CreatorUserId       = _userModels[1].UserId,
                    StartDate           = DateTime.Now,
                    ExpireDate          = DateTime.MaxValue,
                    Title               = "title",
                    EligibleGroupsNames = _groupModels.Select(g => g.Name).ToList(),
                    AreStatsPublic      = false,
                    IsAllowedToReanswer = true,
                    Questions           = new List <PollQuestion>
                    {
                        new PollQuestion
                        {
                            Id            = id1,
                            QuestionText  = "?",
                            AnswerOptions = new List <AnswerOption>
                            {
                                new AnswerOption {
                                    QuestionText = "1"
                                },
                                new AnswerOption {
                                    QuestionText = "2"
                                },
                                new AnswerOption {
                                    QuestionText = "3"
                                },
                            },
                            AnswerType = Types.GetPollAnswerTypeAsString(PollAnswerType.Multiselection),
                        },
                        new PollQuestion
                        {
                            Id            = id2,
                            QuestionText  = "?",
                            AnswerOptions = new List <AnswerOption>
                            {
                                new AnswerOption {
                                    QuestionText = "1"
                                },
                                new AnswerOption {
                                    QuestionText = "2",
                                }
                            },
                            AnswerType = Types.GetPollAnswerTypeAsString(PollAnswerType.Singleselection),
                        },
                    }
                },
            };

            foreach (var poll in polls)
            {
                await _pollRepository.AddAsync(poll);
            }
            var pollAnswersUnits = new List <PollAnswersUnit>
            {
                new PollAnswersUnit()
                {
                    CreatorUserId = _userModels[2].UserId,
                    PollBaseId    = polls[0].Id,
                    AnswerDate    = DateTime.Now,
                    Answers       = new List <PollAnswer>()
                    {
                        new PollAnswer
                        {
                            QuestionId      = id1,
                            SelectedOptions = new List <int> {
                                0, 1
                            },
                        },
                        new PollAnswer
                        {
                            QuestionId      = id2,
                            SelectedOptions = new List <int> {
                                0
                            },
                        }
                    }
                },
                new PollAnswersUnit()
                {
                    CreatorUserId = _userModels[0].UserId,
                    PollBaseId    = polls[0].Id,
                    AnswerDate    = DateTime.Now,
                    Answers       = new List <PollAnswer>()
                    {
                        new PollAnswer
                        {
                            QuestionId      = id1,
                            SelectedOptions = new List <int> {
                                0, 2
                            },
                        },
                        new PollAnswer
                        {
                            QuestionId      = id2,
                            SelectedOptions = new List <int> {
                                1
                            },
                        }
                    }
                }
            };

            foreach (var pollAnswersUnit in pollAnswersUnits)
            {
                await _pollRepository.AddAnswersUnitAsync(pollAnswersUnit);
            }
        }