コード例 #1
0
        public async Task <ActionResult> Index()
        {
            var      viewModel   = new QuoteAndAuthorAnswersDTO();
            QuoteDTO randomQuote = await this.quotesService.GetRandomQuote();

            viewModel.Quote = randomQuote;
            var authorAnswers = new List <string>();

            viewModel.QuizModeType = await this.modesService.GetSelectedMode();

            if (viewModel.QuizModeType == QuizModeType.Binary)
            {
                var randomAuthor = await this.authorsService.GetRandomAuthor();

                authorAnswers.Add(randomAuthor.Name);
            }
            else
            {
                while (authorAnswers.Count < GlobalConstants.MultipleChoiceModeDefaultAuthorsCount)
                {
                    var randomAuthor = await this.authorsService.GetRandomAuthor();

                    if (authorAnswers.Contains(randomAuthor.Name))
                    {
                        continue;
                    }

                    authorAnswers.Add(randomAuthor.Name);
                }

                if (!authorAnswers.Contains(randomQuote.Author))
                {
                    int randomPosition =
                        StaticRandomizer.RandomNumber(0, GlobalConstants.MultipleChoiceModeDefaultAuthorsCount);
                    authorAnswers[randomPosition] = randomQuote.Author;
                }
            }

            viewModel.AuthorAnswers = authorAnswers;

            return(this.View(viewModel));
        }
コード例 #2
0
        public async Task <AuthorDTO> GetRandomAuthor()
        {
            int authorsCount = this.authorsRepository.GetAll().Count();

            if (authorsCount == 0)
            {
                throw new UnpopulatedDbException(
                          "No authors in the database. Please populate db with authors first.");
            }

            int randomAuthorId = StaticRandomizer.RandomNumber(1, authorsCount + 1);

            var randomAuthor = await this.authorsRepository
                               .GetAll()
                               .Where(x => x.Id == randomAuthorId)
                               .Select(AuthorDTO.MapToDTO)
                               .FirstOrDefaultAsync();

            return(randomAuthor);
        }
コード例 #3
0
        public async Task <QuoteDTO> GetRandomQuote()
        {
            int quotesCount = this.quotesRepository.GetAll().Count();

            if (quotesCount == 0)
            {
                throw new UnpopulatedDbException(
                          "No quotes in the database. Please populate db with quotes first.");
            }

            int randomQuoteId = StaticRandomizer.RandomNumber(1, quotesCount + 1);

            var randomQuote = await this.quotesRepository
                              .GetAll()
                              .Include(x => x.Author)
                              .Where(x => x.Id == randomQuoteId)
                              .Select(QuoteDTO.MapToDTO)
                              .FirstOrDefaultAsync();

            return(randomQuote);
        }