예제 #1
0
        public List <Question> GetAll()
        {
            var questions = _repo.GetAll().ToList();

            if (questions == null)
            {
                throw new Exception("We've learned everything we need to know. There are no questions.");
            }

            return(questions);
        }
예제 #2
0
        public async Task <Question> UpdateQuestionAsync(long questionId, string value, string userId)
        {
            var question = await _questionsRepository
                           .GetAll()
                           .Where(a => a.Id == questionId)
                           .Where(a => !a.IsDeleted)
                           .SingleOrDefaultAsync();

            if (question == null)
            {
                return(null);
            }

            var access = await _quizzesRepository.HaveWriteAccessToQuiz(userId, question.QuizId);

            if (!access)
            {
                return(null);
            }

            var questionVersion = new QuestionVersion {
                CreationTime = CurrentTime,
                Value        = value
            };

            question.Versions.Add(questionVersion);
            _questionsRepository.Update(question);
            var result = await Context.SaveChangesAsync() > 0;

            if (!result)
            {
                return(null);
            }

            question.FlatVersionProps();
            return(question);
        }
 public List <Question> GetQuestions()
 {
     return(_repo.GetAll().ToList());
 }
예제 #4
0
 internal IEnumerable <Question> GetAll()
 {
     return(_repo.GetAll());
 }
        public async Task <object> NextQuestionAsync(long learningQuizId, string userId)
        {
            var learningQuiz = await _learningQuizzesRepository.GetById(learningQuizId);

            if (learningQuiz?.QuizId == null || learningQuiz.UserId != userId)
            {
                return(null);
            }

            var isPublic = await _quizzesRepository.IsPublicAsync(learningQuiz.QuizId.Value);

            if (!isPublic)
            {
                var access = await _quizzesRepository.HaveReadAccessToQuizAsync(userId, learningQuiz.QuizId.Value);

                if (!access)
                {
                    return(null);
                }
            }

            var maxVersionTime = learningQuiz.CreationTime;
            var reoccurrences  = _learningQuizQuestionsRepository.GetAllByLearningQuizId(learningQuiz.Id);

            if (reoccurrences == null)
            {
                return(null);
            }

            var remainingReoccurrences = reoccurrences.Where(q => q.Reoccurrences > 0);

            if (!remainingReoccurrences.Any())
            {
                return(new { IsFinished = true });
            }

            var randomQuestionReoccurrences = remainingReoccurrences.RandomElement();

            var randomQuestion = await _questionsRepository
                                 .GetAll()
                                 .Where(q => q.Id == randomQuestionReoccurrences.QuestionId)
                                 .Where(q => q.CreationTime <= maxVersionTime)
                                 .Include(q => q.Versions)
                                 .SingleOrDefaultAsync();

            randomQuestion.FlatVersionProps(maxVersionTime);

            var answers = await _answersRepository
                          .GetAllByQuestionId(randomQuestion.Id, maxVersionTime, true) //TODO MIN VERSION TIME
                          .Shuffle()
                          .Include(a => a.Versions)
                          .Select(a => new {
                a.Id,
                a.Versions
                .Where(b => b.CreationTime <= maxVersionTime)
                .OrderByDescending(b => b.CreationTime)
                .FirstOrDefault()
                .Value
            })
                          .ToListAsync();

            return(new {
                IsFinished = false,
                randomQuestionReoccurrences.Reoccurrences,
                Question = randomQuestion,
                Answers = answers
            });
        }