Exemplo n.º 1
0
 private void ValidateChapterQuestion(ChapterQuestion question)
 {
     if (string.IsNullOrWhiteSpace(question.Name) ||
         string.IsNullOrWhiteSpace(question.Answer))
     {
         throw new OdkServiceException("Some required fields are missing");
     }
 }
Exemplo n.º 2
0
 public async Task UpdateChapterQuestion(ChapterQuestion question)
 {
     await Context
     .Update <ChapterQuestion>()
     .Set(x => x.Answer, question.Answer)
     .Set(x => x.DisplayOrder, question.DisplayOrder)
     .Set(x => x.Name, question.Name)
     .Where(x => x.Id).EqualTo(question.Id)
     .ExecuteAsync();
 }
Exemplo n.º 3
0
        public async Task UpdateChapterQuestion(Guid currentMemberId, Guid questionId, CreateChapterQuestion question)
        {
            ChapterQuestion update = await GetChapterQuestion(currentMemberId, questionId);

            update.Answer = question.Answer;
            update.Name   = question.Name;

            ValidateChapterQuestion(update);

            await _chapterRepository.UpdateChapterQuestion(update);

            _cacheService.RemoveVersionedCollection <ChapterQuestion>(update.ChapterId);
        }
Exemplo n.º 4
0
        public async Task <ChapterQuestion> GetChapterQuestion(Guid currentMemberId, Guid questionId)
        {
            ChapterQuestion question = await _chapterRepository.GetChapterQuestion(questionId);

            if (question == null)
            {
                throw new OdkNotFoundException();
            }

            await AssertMemberIsChapterAdmin(currentMemberId, question.ChapterId);

            return(question);
        }
Exemplo n.º 5
0
        public async Task CreateChapterQuestion(Guid currentMemberId, Guid chapterId, CreateChapterQuestion question)
        {
            await AssertMemberIsChapterAdmin(currentMemberId, chapterId);

            IReadOnlyCollection <ChapterQuestion> existing = await _chapterRepository.GetChapterQuestions(chapterId);

            int             displayOrder = existing.Count > 0 ? existing.Max(x => x.DisplayOrder) + 1 : 1;
            ChapterQuestion create       = new ChapterQuestion(Guid.Empty, chapterId, question.Name, question.Answer, displayOrder, 0);

            ValidateChapterQuestion(create);

            await _chapterRepository.CreateChapterQuestion(create);

            _cacheService.RemoveVersionedItem <ChapterQuestion>(chapterId);
        }
Exemplo n.º 6
0
        public async Task <IReadOnlyCollection <ChapterQuestion> > UpdateChapterQuestionDisplayOrder(Guid currentMemberId, Guid questionId, int moveBy)
        {
            ChapterQuestion question = await GetChapterQuestion(currentMemberId, questionId);

            IReadOnlyCollection <ChapterQuestion> questions = await _chapterRepository.GetChapterQuestions(question.ChapterId);

            if (moveBy == 0)
            {
                return(questions);
            }

            ChapterQuestion switchWith;

            if (moveBy > 0)
            {
                switchWith = questions
                             .Where(x => x.DisplayOrder > question.DisplayOrder)
                             .OrderBy(x => x.DisplayOrder)
                             .FirstOrDefault();
            }
            else
            {
                switchWith = questions
                             .Where(x => x.DisplayOrder < question.DisplayOrder)
                             .OrderByDescending(x => x.DisplayOrder)
                             .FirstOrDefault();
            }

            if (switchWith == null)
            {
                return(questions);
            }

            question = questions.First(x => x.Id == question.Id);

            int displayOrder = switchWith.DisplayOrder;

            switchWith.DisplayOrder = question.DisplayOrder;
            question.DisplayOrder   = displayOrder;

            await _chapterRepository.UpdateChapterQuestion(question);

            await _chapterRepository.UpdateChapterQuestion(switchWith);

            _cacheService.RemoveVersionedCollection <ChapterQuestion>(question.ChapterId);

            return(questions.OrderBy(x => x.DisplayOrder).ToArray());
        }
Exemplo n.º 7
0
        public async Task DeleteChapterQuestion(Guid currentMemberId, Guid id)
        {
            ChapterQuestion question = await GetChapterQuestion(currentMemberId, id);

            await _chapterRepository.DeleteChapterQuestion(id);

            IReadOnlyCollection <ChapterQuestion> questions = await _chapterRepository.GetChapterQuestions(question.ChapterId);

            int displayOrder = 1;

            foreach (ChapterQuestion reorder in questions.OrderBy(x => x.DisplayOrder))
            {
                if (reorder.DisplayOrder != displayOrder)
                {
                    reorder.DisplayOrder = displayOrder;
                    await _chapterRepository.UpdateChapterQuestion(reorder);
                }

                displayOrder++;
            }

            _cacheService.RemoveVersionedCollection <ChapterQuestion>(question.ChapterId);
        }
Exemplo n.º 8
0
        public async Task <ChapterQuestionApiResponse> GetQuestion(Guid id)
        {
            ChapterQuestion question = await _chapterAdminService.GetChapterQuestion(GetMemberId(), id);

            return(_mapper.Map <ChapterQuestionApiResponse>(question));
        }
Exemplo n.º 9
0
 public async Task <Guid> CreateChapterQuestion(ChapterQuestion question)
 {
     return(await Context
            .Insert(question)
            .GetIdentityAsync());
 }