缓存就不说了,查询分类列表,联合查询文章和分类两张表,关联字段为CategoryId,然后分组,计算出对应的数量,在BlogController中添加API。
/// <summary> /// 查询分类列表 /// </summary> /// <returns></returns> [HttpGet] [Route("categories")] public async Task<ServiceResult<IEnumerable<QueryCategoryDto>>> QueryCategoriesAsync() { return await _blogService.QueryCategoriesAsync(); } 标签列表分析:和分类列表差不多,新建模型QueryTagDto.cs继承TagDto。
//QueryTagDto.cs namespace Meowv.Blog.Application.Contracts.Blog { public class QueryTagDto : TagDto { /// <summary> /// 总数 /// </summary> public int Count { get; set; } } }添加查询标签列表接口和缓存接口。
//IBlogCacheService.Tag.cs using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Meowv.Blog.Application.Caching.Blog { public partial interface IBlogCacheService { /// <summary> /// 查询标签列表 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult<IEnumerable<QueryTagDto>>> QueryTagsAsync(Func<Task<ServiceResult<IEnumerable<QueryTagDto>>>> factory); } } //IBlogService.Tag.cs using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System.Collections.Generic; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog { public partial interface IBlogService { /// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> Task<ServiceResult<IEnumerable<QueryTagDto>>> QueryTagsAsync(); } }分别实现这两个接口。
//BlogCacheService.Tag.cs using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System; using System.Collections.Generic; using System.Threading.Tasks; using static Meowv.Blog.Domain.Shared.MeowvBlogConsts; namespace Meowv.Blog.Application.Caching.Blog.Impl { public partial class BlogCacheService { private const string KEY_QueryTags = "Blog:Tag:QueryTags"; /// <summary> /// 查询标签列表 /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult<IEnumerable<QueryTagDto>>> QueryTagsAsync(Func<Task<ServiceResult<IEnumerable<QueryTagDto>>>> factory) { return await Cache.GetOrAddAsync(KEY_QueryTags, factory, CacheStrategy.ONE_DAY); } } } //BlogService.Tag.cs using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog.Impl { public partial class BlogService { /// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> public async Task<ServiceResult<IEnumerable<QueryTagDto>>> QueryTagsAsync() { return await _blogCacheService.QueryTagsAsync(async () => { var result = new ServiceResult<IEnumerable<QueryTagDto>>(); var list = from tags in await _tagRepository.GetListAsync() join post_tags in await _postTagRepository.GetListAsync() on tags.Id equals post_tags.TagId group tags by new { tags.TagName, tags.DisplayName } into g select new QueryTagDto { TagName = g.Key.TagName, DisplayName = g.Key.DisplayName, Count = g.Count() }; result.IsSuccess(list); return result; }); } } }查询标签列表需要联合查询tags和post_tags,根据TagId进行关联,然后分组从而获取标签下文章的总数,在BlogController中添加API。
/// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> [HttpGet] [Route("tags")] public async Task<ServiceResult<IEnumerable<QueryTagDto>>> QueryTagsAsync() { return await _blogService.QueryTagsAsync(); } 分类名称&文章列表