Exemplo n.º 1
0
        // Create/Update/Delete methods

        public async Task <Answer> CreateAnswerAsync(Answer answer, string userId)
        {
            var question = await _questionsRepository.GetById(answer.QuestionId);

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

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

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

            var creationTime = CurrentTime;

            answer.CreationTime = creationTime;

            var version = new AnswerVersion {
                CreationTime = creationTime,
                Value        = answer.Value,
                IsCorrect    = answer.IsCorrect
            };

            answer.Versions.Add(version);
            answer.FlatVersionProps();

            await _answersRepository.Create(answer);

            var result = await Context.SaveChangesAsync() > 0;

            return(result ? answer : null);
        }
Exemplo n.º 2
0
        // Create/Update/Delete methods

        public async Task <Question> CreateQuestionAsync(Question question, string userId)
        {
            var access = await _quizzesRepository.HaveWriteAccessToQuiz(userId, question.QuizId);

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

            var creationTime = CurrentTime;

            question.CreationTime = creationTime;

            var questionVersion = new QuestionVersion {
                CreationTime = creationTime,
                Value        = question.Value
            };

            question.Versions.Add(questionVersion);
            question.FlatVersionProps();

            await _questionsRepository.Create(question);

            return(await Context.SaveChangesAsync() > 0
                ? question
                : null);
        }
Exemplo n.º 3
0
        public async Task <IDictionary <QuizAccessEnum, IList <BasicUser> > > GetQuizUsersAsync(Guid quizId, string userId)
        {
            var access = await _quizzesRepository.HaveWriteAccessToQuiz(userId, quizId);

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

            var quiz = await _quizzesRepository
                       .GetAll()
                       .Where(q => q.Id == quizId)
                       .Include(q => q.Accesses)
                       .SingleOrDefaultAsync();

            return(quiz?.Accesses
                   .Where(a => a.Access != QuizAccessEnum.None)
                   .GroupBy(a => a.Access)
                   .Select(async a => new KeyValuePair <QuizAccessEnum, IList <BasicUser> >(
                               a.Key,
                               (await _auth0UsersService.GetBasicUsersListByIds(a.Select(ac => ac.UserId).ToList())).ToList()
                               ))
                   .Select(a => a.Result)
                   .ToDictionary(a => a.Key, a => a.Value));
        }