コード例 #1
0
ファイル: BlogService.cs プロジェクト: ilkerhalil/Fanray
        /// <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);
        }
コード例 #2
0
ファイル: BlogUtilTest.cs プロジェクト: ilkerhalil/Fanray
 public void FormatTaxonomySlug_Test(string input, string expected, IEnumerable <string> existingSlugs = null)
 {
     Assert.Equal(expected, BlogUtil.FormatTaxonomySlug(input, existingSlugs));
 }