Exemple #1
0
        public async Task <Quiz> GetByIdAsync(Guid quizId, string userId)
        {
            var isPublic = await _quizzesRepository.IsPublicAsync(quizId);

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

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

            var quiz = await _quizzesRepository.GetById(quizId);

            var userAccess = await _quizAccessesRepository.GetQuizAccessForUserAsync(userId, quizId);

            if (userAccess != null)
            {
                quiz.IncludeAccess(userAccess.Access);
            }

            var quizWithOwnerNickName = await _auth0UsersService.QuizItemWithOwnerNickName(quiz);

            return(quizWithOwnerNickName);
        }
        public async Task <LearningQuiz> GetLearningQuizByIdAsync(long id, string userId)
        {
            var learningQuiz = await _learningQuizzesRepository
                               .GetAllByUserId(userId)
                               .Where(q => q.Id == id)
                               .Include(q => q.Quiz)
                               .SingleOrDefaultAsync();

            if (learningQuiz?.QuizId == null)
            {
                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);
                }
            }

            await _auth0UsersService.QuizItemWithOwnerNickName(learningQuiz.Quiz);

            return(learningQuiz);
        }
        public async Task <object> GetRandomQuestionsWithQuizByQuizIdAsync(Guid id, string userId)
        {
            var isPublic = await _quizzesRepository.IsPublicAsync(id);

            if (!isPublic)
            {
                var haveReadAccess = await _quizzesRepository.HaveReadAccessToQuizAsync(userId, id);

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

            var quiz = await _quizzesRepository.GetById(id);

            await _auth0UsersService.QuizItemWithOwnerNickName(quiz);

            var randomQuestionsQuery = RandomQuizQuestionsQuery(id);
            var randomQuestions      = await RandomQuizQuestions(randomQuestionsQuery, quiz.QuestionsInSolvingQuiz);

            var questionsInDatabase = await randomQuestionsQuery.CountAsync();

            var creationTime = CurrentTime;

            return(new {
                SolvingQuiz = new {
                    Quiz = quiz,
                    QuestionsInDatabase = questionsInDatabase,
                    QuestionsInSolvingQuiz = randomQuestions.Count,
                    Questions = randomQuestions,
                    CreatedTime = creationTime
                },
                SolvedQuizTemplate = new UserSolvedQuiz {
                    QuizId = quiz.Id,
                    CreatedTime = creationTime,
                    SolvingTime = 0,
                    MaxSolvingTime = quiz.MillisecondsInSolvingQuiz,
                    UserSolvedQuestions = randomQuestions
                                          .Select(q => new UserSolvedQuizQuestion {
                        QuestionId = q.Id
                    })
                                          .ToList()
                }
            });
        }