Exemplo n.º 1
0
 public TaxonomyValidator(IEnumerable <string> existingTitles, ETaxonomyType type)
 {
     RuleFor(c => c.Title)
     .NotEmpty()
     .Length(1, TAXONOMY_TITLE_SLUG_MAXLEN)
     .Must(title => !existingTitles.Contains(title, StringComparer.CurrentCultureIgnoreCase))
     .WithMessage(c => $"{type} '{c.Title}' is not available, please choose a different one.");
 }
Exemplo n.º 2
0
        /// <summary>
        /// Prepares a category or tag for create or update, making sure its title and slug are valid.
        /// </summary>
        /// <param name="tax">A category or tag.</param>
        /// <param name="createOrUpdate"></param>
        /// <returns></returns>
        private async Task <ITaxonomy> PrepTaxonomyAsync(ITaxonomy tax, ECreateOrUpdate createOrUpdate)
        {
            // get existing titles and slugs
            List <string> existingTitles = null;
            List <string> existingSlugs  = null;
            ETaxonomyType type           = ETaxonomyType.Category;
            ITaxonomy     origTax        = tax;

            if (tax is Category cat)
            {
                if (cat.Id != 0)
                {
                    origTax = await _catRepo.GetAsync(cat.Id);
                }
                var allCats = await GetCategoriesAsync();

                existingTitles = allCats.Select(c => c.Title).ToList();
                existingSlugs  = allCats.Select(c => c.Slug).ToList();
            }
            else
            {
                var tag = (Tag)tax;
                if (tag.Id != 0)
                {
                    origTax = await _tagRepo.GetAsync(tag.Id);
                }
                var allTags = await GetTagsAsync();

                existingTitles = allTags.Select(c => c.Title).ToList();
                existingSlugs  = allTags.Select(c => c.Slug).ToList();
                type           = ETaxonomyType.Tag;
            }

            // remove self if it is update
            if (createOrUpdate == ECreateOrUpdate.Update)
            {
                existingTitles.Remove(origTax.Title);
                existingSlugs.Remove(origTax.Slug);
            }

            // html encode title and description
            tax.Title       = Util.CleanHtml(tax.Title);
            tax.Description = Util.CleanHtml(tax.Description);

            // validator
            var validator           = new TaxonomyValidator(existingTitles);
            ValidationResult result = await validator.ValidateAsync(tax);

            if (!result.IsValid)
            {
                throw new FanException($"Failed to {createOrUpdate.ToString().ToLower()} {type}.", result.Errors);
            }

            // slug always updated according to title
            origTax.Slug        = BlogUtil.FormatTaxonomySlug(tax.Title, existingSlugs);
            origTax.Title       = tax.Title;
            origTax.Description = tax.Description;
            origTax.Count       = tax.Count;

            _logger.LogDebug(createOrUpdate + " {@Taxonomy}", origTax);
            return(origTax);
        }