private void ValidateRouteArgs(RouteArgs args)
        {
            if (args == null)
            {
                return;
            }

            // ensure referenced image exists
            if (args.Image != null && !_mediaIndex.IsImage(args.Image.Value))
            {
                ModelState.AddModelError(nameof(args.Image),
                                         ErrorMessages.ImageNotFound(args.Image.Value));
            }

            // ensure referenced audio exists
            if (args.Audio != null && !_mediaIndex.IsAudio(args.Audio.Value))
            {
                ModelState.AddModelError(nameof(args.Audio),
                                         ErrorMessages.AudioNotFound(args.Audio.Value));
            }

            // ensure referenced exhibits exist
            if (args.Exhibits != null)
            {
                var invalidIds = args.Exhibits
                                 .Where(id => !_entityIndex.Exists(ResourceType.Exhibit, id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Exhibits),
                                             ErrorMessages.ContentNotFound(ResourceType.Exhibit, id));
                }
            }

            // ensure referenced tags exist
            if (args.Tags != null)
            {
                var invalidIds = args.Tags
                                 .Where(id => !_entityIndex.Exists(ResourceType.Tag, id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Tags),
                                             ErrorMessages.ContentNotFound(ResourceType.Tag, id));
                }
            }
        }
Exemple #2
0
        private void ValidateExhibitPageArgs(ExhibitPageArgs2 args)
        {
            if (args == null)
            {
                return;
            }

            // constrain properties Image, Images and HideYearNumbers to their respective page types
            if (args.Image != null && args.Type != PageType.Image_Page)
            {
                ModelState.AddModelError(nameof(args.Image),
                                         ErrorMessages.FieldNotAllowedForPageType(nameof(args.Image), args.Type));
            }

            if (args.Images != null && args.Type != PageType.Slider_Page)
            {
                ModelState.AddModelError(nameof(args.Images),
                                         ErrorMessages.FieldNotAllowedForPageType(nameof(args.Images), args.Type));
            }

            if (args.HideYearNumbers != null && args.Type != PageType.Slider_Page)
            {
                ModelState.AddModelError(nameof(args.HideYearNumbers),
                                         ErrorMessages.FieldNotAllowedForPageType(nameof(args.HideYearNumbers), args.Type));
            }

            // validate font family
            if (!_exhibitPagesConfig.Value.IsFontFamilyValid(args.FontFamily))
            {
                ModelState.AddModelError(nameof(args.FontFamily), $"Font family must be null/unspecified (which defaults to {_exhibitPagesConfig.Value.DefaultFontFamily}) or one of the following: {string.Join(", ", _exhibitPagesConfig.Value.FontFamilies)}");
            }

            // ensure referenced image exists
            if (args.Image != null && !_mediaIndex.IsImage(args.Image.Value))
            {
                ModelState.AddModelError(nameof(args.Image),
                                         ErrorMessages.ImageNotFound(args.Image.Value));
            }

            // ensure referenced audio exists
            if (args.Audio != null && !_mediaIndex.IsAudio(args.Audio.Value))
            {
                ModelState.AddModelError(nameof(args.Audio),
                                         ErrorMessages.AudioNotFound(args.Audio.Value));
            }

            // ensure referenced slider page images exist
            if (args.Images != null)
            {
                var invalidIds = args.Images
                                 .Select(img => img.Image)
                                 .Where(id => !_mediaIndex.IsImage(id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.Images),
                                             ErrorMessages.ImageNotFound(id));
                }
            }

            // ensure referenced additional info pages exist
            if (args.AdditionalInformationPages != null)
            {
                var invalidIds = args.AdditionalInformationPages
                                 .Where(id => !_entityIndex.Exists(ResourceType.ExhibitPage, id))
                                 .ToList();

                foreach (var id in invalidIds)
                {
                    ModelState.AddModelError(nameof(args.AdditionalInformationPages),
                                             ErrorMessages.ExhibitPageNotFound(id));
                }
            }
        }