예제 #1
0
        private async Task ValidateTestAsync(int id)
        {
            var user = await Context.GetCurrentUserAsync();

            var test = await _repositoryTest.FindFirstAsync(new TestsById(id)) ??
                       throw ExceptionHelper.NotFound <DatabaseTest>(id);

            var specification =
                new TestsById(id) &
                new TestsForStudents() &
                new TestsByStudentId(user.Id);

            if (specification.IsSatisfiedBy(test) == false)
            {
                throw ExceptionHelper.CreatePublicException("Указанный тест недоступен.");
            }
        }
예제 #2
0
        public async Task <Question> ProcessTestQuestionAsync(int id, Question question)
        {
            var user = await Context.GetCurrentUserAsync();

            if (user.IsNotStudent())
            {
                throw ExceptionHelper.NoAccess();
            }

            var test = await _repositoryTest.FindFirstAsync(new TestsById(id)) ??
                       throw ExceptionHelper.NotFound <DatabaseTest>(id);

            var specification =
                new TestsById(id) &
                new TestsForStudents() &
                new TestsByStudentId(user.Id);

            if (specification.IsSatisfiedBy(test) == false)
            {
                throw ExceptionHelper.CreatePublicException("Указанный тест недоступен.");
            }

            var model = await _repositoryQuestion.FindFirstAsync(new QuestionsById(question.Id)) ??
                        throw ExceptionHelper.NotFound <DatabaseQuestion>(question.Id);

            var result = await _questionValidatorFactory
                         .GetQuestionValidator(model.Type)
                         .ValidateAsync(question);

            if (result.Right != true)
            {
                return(result);
            }

            model.QuestionStudents.Add(new DatabaseQuestionStudent
            {
                Passed    = true,
                StudentId = user.Id
            });

            await _repositoryQuestion.UpdateAsync(model, true);

            return(result);
        }