public void SlugifyTaxonomy_replaces_hashtag_with_letter_s() { var expected = "cs"; var actual = BlogUtil.SlugifyTaxonomy("c#", CategoryService.SLUG_MAXLEN); Assert.Equal(expected, actual); }
public void SlugifyTaxonomy_trucates_long_slug_and_keeps_only_24_chars() { var expected = "this-is-a-really-long-ca"; var actual = BlogUtil.SlugifyTaxonomy("this is a really long category title", CategoryService.SLUG_MAXLEN); Assert.Equal(expected, actual); }
/// <summary> /// Creates a new <see cref="Tag"/>. /// </summary> /// <param name="tag">The tag with data to be created.</param> /// <exception cref="MokException">If tag is empty or title exists already.</exception> /// <returns>Created tag.</returns> public async Task <Tag> CreateAsync(Tag tag) { if (tag == null || tag.Title.IsNullOrEmpty()) { throw new MokException($"Invalid tag to create."); } // prep title tag.Title = PrepareTitle(tag.Title); // make sure it is unique var allTags = await GetAllAsync(); if (allTags.Any(t => t.Title.Equals(tag.Title, StringComparison.CurrentCultureIgnoreCase))) { throw new MokException($"'{tag.Title}' already exists."); } // prep slug, description and count tag.Slug = BlogUtil.SlugifyTaxonomy(tag.Title, SLUG_MAXLEN, allTags.Select(c => c.Slug)); // slug is based on title tag.Description = Util.CleanHtml(tag.Description); tag.Count = tag.Count; // create tag = await tagRepository.CreateAsync(tag); // remove cache await cache.RemoveAsync(BlogCache.KEY_ALL_TAGS); await cache.RemoveAsync(BlogCache.KEY_POSTS_INDEX); logger.LogDebug("Created {@Tag}", tag); return(tag); }
/// <summary> /// Updates an existing <see cref="Category"/>. /// </summary> /// <param name="category">The category with data to be updated.</param> /// <exception cref="MokException">If category is invalid or title exists.</exception> /// <returns>Updated category.</returns> public async Task <Category> UpdateAsync(Category category) { if (category == null || category.Id <= 0 || category.Title.IsNullOrEmpty()) { throw new MokException($"Invalid category to update."); } // prep title category.Title = PrepareTitle(category.Title); // make sure it is unique var allCats = await GetAllAsync(); allCats.RemoveAll(c => c.Id == category.Id); // remove selft if (allCats.Any(c => c.Title.Equals(category.Title, StringComparison.CurrentCultureIgnoreCase))) { throw new MokException($"'{category.Title}' already exists."); } // prep slug, description and count var entity = await categoryRepository.GetAsync(category.Id); entity.Title = category.Title; // assign new title entity.Slug = BlogUtil.SlugifyTaxonomy(category.Title, SLUG_MAXLEN, allCats.Select(c => c.Slug)); // slug is based on title entity.Description = Util.CleanHtml(category.Description); entity.Count = category.Count; // update await categoryRepository.UpdateAsync(category); // remove cache await cache.RemoveAsync(BlogCache.KEY_ALL_CATS); await cache.RemoveAsync(BlogCache.KEY_POSTS_INDEX); // raise nav updated event await mediator.Publish(new NavUpdated { Id = category.Id, Type = ENavType.BlogCategory }); // return entity logger.LogDebug("Updated {@Category}", entity); return(entity); }
/// <summary> /// Updates an existing <see cref="Tag"/>. /// </summary> /// <param name="tag">The tag with data to be updated.</param> /// <exception cref="FanException">If tag is invalid or title exists already.</exception> /// <returns>Updated tag.</returns> public async Task <Tag> UpdateAsync(Tag tag) { if (tag == null || tag.Id <= 0 || tag.Title.IsNullOrEmpty()) { throw new FanException($"Invalid tag to update."); } // prep title tag.Title = PrepareTitle(tag.Title); // make sure it is unique var allTags = await GetAllAsync(); allTags.RemoveAll(t => t.Id == tag.Id); // remove self if (allTags.Any(t => t.Title.Equals(tag.Title, StringComparison.CurrentCultureIgnoreCase))) { throw new FanException($"'{tag.Title}' already exists."); } // prep slug, description and count var entity = await _tagRepo.GetAsync(tag.Id); entity.Title = tag.Title; // assign new title entity.Slug = BlogUtil.SlugifyTaxonomy(tag.Title, SLUG_MAXLEN, allTags.Select(c => c.Slug)); // slug is based on title entity.Description = Util.CleanHtml(tag.Description); entity.Count = tag.Count; // update await _tagRepo.UpdateAsync(entity); // remove cache await _cache.RemoveAsync(BlogCache.KEY_ALL_TAGS); await _cache.RemoveAsync(BlogCache.KEY_POSTS_INDEX); // return entity _logger.LogDebug("Updated {@Tag}", entity); return(entity); }
/// <summary> /// Creates a new <see cref="Category"/>. /// </summary> /// <param name="category">The category with data to be created.</param> /// <exception cref="MokException">If title is empty or exists already.</exception> /// <returns>Created category.</returns> public async Task <Category> CreateAsync(string title, string description = null) { if (title.IsNullOrEmpty()) { throw new MokException($"Category title cannot be empty."); } // prep title title = PrepareTitle(title); // make sure unique var allCats = await GetAllAsync(); if (allCats.Any(t => t.Title.Equals(title, StringComparison.CurrentCultureIgnoreCase))) { throw new MokException($"'{title}' already exists."); } // prep slug, desc and count var category = new Category { Title = title, Slug = BlogUtil.SlugifyTaxonomy(title, SLUG_MAXLEN, allCats.Select(c => c.Slug)), Description = Util.CleanHtml(description), Count = 0, }; // create category = await categoryRepository.CreateAsync(category); // remove cache await cache.RemoveAsync(BlogCache.KEY_ALL_CATS); await cache.RemoveAsync(BlogCache.KEY_POSTS_INDEX); logger.LogDebug("Created {@Category}", category); return(category); }
public void SlugifyTaxonomy_returns_unique_slug_by_append_counter_to_duplicate(string input, string expected, IEnumerable <string> existing = null) { Assert.Equal(expected, BlogUtil.SlugifyTaxonomy(input, CategoryService.SLUG_MAXLEN, existing)); }
public void SlugifyTaxonomy_produces_8chars_string_when_input_is_non_english() { var actual = BlogUtil.SlugifyTaxonomy("你好", CategoryService.SLUG_MAXLEN); Assert.True(actual.Length == 6); }