Example #1
0
        public async Task <ChapterProperty> GetChapterProperty(Guid currentMemberId, Guid id)
        {
            ChapterProperty property = await _chapterRepository.GetChapterProperty(id);

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

            await AssertMemberIsChapterAdmin(currentMemberId, property.ChapterId);

            return(property);
        }
Example #2
0
 public async Task UpdateChapterProperty(ChapterProperty property)
 {
     await Context
     .Update <ChapterProperty>()
     .Set(x => x.DisplayOrder, property.DisplayOrder)
     .Set(x => x.HelpText, property.HelpText)
     .Set(x => x.Hidden, property.Hidden)
     .Set(x => x.Label, property.Label)
     .Set(x => x.Name, property.Name)
     .Set(x => x.Required, property.Required)
     .Set(x => x.Subtitle, property.Subtitle)
     .Where(x => x.Id).EqualTo(property.Id)
     .ExecuteAsync();
 }
Example #3
0
        public async Task <IReadOnlyCollection <ChapterProperty> > UpdateChapterPropertyDisplayOrder(Guid currentMemberId, Guid propertyId, int moveBy)
        {
            ChapterProperty property = await GetChapterProperty(currentMemberId, propertyId);

            IReadOnlyCollection <ChapterProperty> properties = await _chapterRepository.GetChapterProperties(property.ChapterId);

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

            ChapterProperty switchWith;

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

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

            property = properties.First(x => x.Id == property.Id);

            int displayOrder = switchWith.DisplayOrder;

            switchWith.DisplayOrder = property.DisplayOrder;
            property.DisplayOrder   = displayOrder;

            await _chapterRepository.UpdateChapterProperty(property);

            await _chapterRepository.UpdateChapterProperty(switchWith);

            _cacheService.RemoveVersionedCollection <ChapterProperty>(property.ChapterId);

            return(properties.OrderBy(x => x.DisplayOrder).ToArray());
        }
Example #4
0
        private async Task ValidateChapterProperty(ChapterProperty property)
        {
            if (string.IsNullOrEmpty(property.Name) ||
                string.IsNullOrEmpty(property.Label) ||
                !Enum.IsDefined(typeof(DataType), property.DataType) || property.DataType == DataType.None)
            {
                throw new OdkServiceException("Some required fields are missing");
            }

            IReadOnlyCollection <ChapterProperty> properties = await _chapterRepository.GetChapterProperties(property.ChapterId);

            if (properties.Any(x => x.Name.Equals(property.Name, StringComparison.OrdinalIgnoreCase) && x.Id != property.Id))
            {
                throw new OdkServiceException("Name already exists");
            }
        }
Example #5
0
        public async Task CreateChapterProperty(Guid currentMemberId, Guid chapterId, CreateChapterProperty property)
        {
            await AssertMemberIsChapterAdmin(currentMemberId, chapterId);

            IReadOnlyCollection <ChapterProperty> existing = await _chapterRepository.GetChapterProperties(chapterId);

            int             displayOrder = existing.Count > 0 ? existing.Max(x => x.DisplayOrder) + 1 : 1;
            ChapterProperty create       = new ChapterProperty(Guid.Empty, chapterId, property.DataType, property.Name?.ToLowerInvariant(),
                                                               property.Label, displayOrder, property.Required, property.Subtitle, property.HelpText, property.Hidden);

            await ValidateChapterProperty(create);

            await _chapterRepository.AddChapterProperty(create);

            _cacheService.RemoveVersionedCollection <ChapterProperty>(chapterId);
        }
Example #6
0
        public async Task UpdateChapterProperty(Guid currentMemberId, Guid propertyId, UpdateChapterProperty property)
        {
            ChapterProperty update = await GetChapterProperty(currentMemberId, propertyId);

            update.HelpText = property.HelpText;
            update.Hidden   = property.Hidden;
            update.Label    = property.Label;
            update.Name     = property.Name?.ToLowerInvariant();
            update.Required = property.Required;
            update.Subtitle = property.Subtitle;

            await ValidateChapterProperty(update);

            await _chapterRepository.UpdateChapterProperty(update);

            _cacheService.RemoveVersionedCollection <ChapterProperty>(update.ChapterId);
        }
Example #7
0
        public async Task DeleteChapterProperty(Guid currentMemberId, Guid id)
        {
            ChapterProperty property = await GetChapterProperty(currentMemberId, id);

            await _chapterRepository.DeleteChapterProperty(id);

            IReadOnlyCollection <ChapterProperty> properties = await _chapterRepository.GetChapterProperties(property.ChapterId, true);

            int displayOrder = 1;

            foreach (ChapterProperty reorder in properties.OrderBy(x => x.DisplayOrder))
            {
                if (reorder.DisplayOrder != displayOrder)
                {
                    reorder.DisplayOrder = displayOrder;
                    await _chapterRepository.UpdateChapterProperty(reorder);
                }

                displayOrder++;
            }

            _cacheService.RemoveVersionedCollection <ChapterProperty>(property.ChapterId);
        }
Example #8
0
        public async Task <ChapterPropertyApiResponse> GetProperty(Guid id)
        {
            ChapterProperty property = await _chapterAdminService.GetChapterProperty(GetMemberId(), id);

            return(_mapper.Map <ChapterPropertyApiResponse>(property));
        }
Example #9
0
 public async Task AddChapterProperty(ChapterProperty property)
 {
     await Context
     .Insert(property)
     .ExecuteAsync();
 }