public int?Resolve(DatabaseTest source, Test destination, int?member, ResolutionContext context)
        {
            var user = _context.GetCurrentUser();

            if (user.IsAdmin())
            {
                return(_repositoryQuestion.GetCount(new QuestionsByTestId(source.Id)));
            }

            if (user.IsLecturer())
            {
                var specification =
                    new QuestionsByTestId(source.Id) &
                    new QuestionsByLecturerId(user.Id);

                return(_repositoryQuestion.GetCount(specification));
            }

            if (user.IsStudent())
            {
                var specification =
                    new QuestionsByTestId(source.Id) &
                    new QuestionsForStudents() &
                    new QuestionsByStudentId(user.Id);

                return(_repositoryQuestion.GetCount(specification));
            }

            return(null);
        }
Beispiel #2
0
        public async Task <PagedData <Question> > GetQuestionsAsync(FilterQuestion filter)
        {
            var user = await Context.GetCurrentUserAsync();

            if (user.IsAdmin())
            {
                var specification =
                    new QuestionsByTestId(filter.TestId) &
                    new QuestionsByThemeId(filter.ThemeId);

                var(count, questions) = await _repositoryQuestion.FindPaginatedAsync(specification, filter);

                return(new PagedData <Question>(Mapper.Map <List <Question> >(questions), count));
            }

            if (user.IsLecturer())
            {
                var specification =
                    new QuestionsByTestId(filter.TestId) &
                    new QuestionsByThemeId(filter.ThemeId) &
                    new QuestionsByLecturerId(user.Id);

                var(count, questions) = await _repositoryQuestion.FindPaginatedAsync(specification, filter);

                return(new PagedData <Question>(Mapper.Map <List <Question> >(questions), count));
            }

            if (user.IsStudent())
            {
                var specification =
                    new QuestionsForStudents() &
                    new QuestionsByTestId(filter.TestId) &
                    new QuestionsByThemeId(filter.ThemeId) &
                    new QuestionsByStudentId(user.Id, filter.Passed);

                var(count, questions) = await _repositoryQuestion.FindPaginatedAsync(specification, filter);

                return(new PagedData <Question>(Mapper.Map <List <Question> >(questions), count));
            }

            throw ExceptionHelper.NoAccess();
        }
        public async Task ResetTestProcessAsync(int id)
        {
            var user = await Context.GetCurrentUserAsync();

            if (user.IsStudent() == false)
            {
                throw ExceptionHelper.NoAccess();
            }

            await ValidateTestAsync(id);

            var specification =
                new QuestionsByTestId(id) &
                new QuestionsForStudents() &
                new QuestionsByStudentId(user.Id);

            var questions = await _repositoryQuestion.FindAllAsync(specification);

            questions.ForEach(question =>
            {
                var models = question.QuestionStudents
                             .Where(x => x.StudentId == user.Id)
                             .ToList();

                if (models.IsEmpty())
                {
                    return;
                }

                question.QuestionStudents = question.QuestionStudents
                                            .Except(models)
                                            .ToList();

                _repositoryQuestionStudent.RemoveAsync(models);
            });

            await _repositoryQuestion.UpdateAsync(questions, true);
        }