分别实现这两个接口。
//BlogCacheService.Post.cs ... public partial class BlogCacheService { private const string KEY_QueryPostsByTag = "Blog:Post:QueryPostsByTag-{0}"; /// <summary> /// 通过标签名称查询文章列表 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> public async Task<ServiceResult<IEnumerable<QueryPostDto>>> QueryPostsByTagAsync(string name, Func<Task<ServiceResult<IEnumerable<QueryPostDto>>>> factory) { return await Cache.GetOrAddAsync(KEY_QueryPostsByTag.FormatWith(name), factory, CacheStrategy.ONE_DAY); } } ... //BlogService.Post.cs /// <summary> /// 通过标签名称查询文章列表 /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult<IEnumerable<QueryPostDto>>> QueryPostsByTagAsync(string name) { return await _blogCacheService.QueryPostsByTagAsync(name, async () => { var result = new ServiceResult<IEnumerable<QueryPostDto>>(); var list = (from post_tags in await _postTagRepository.GetListAsync() join tags in await _tagRepository.GetListAsync() on post_tags.TagId equals tags.Id join posts in await _postRepository.GetListAsync() on post_tags.PostId equals posts.Id where tags.DisplayName.Equals(name) orderby posts.CreationTime descending select new PostBriefDto { Title = posts.Title, Url = posts.Url, Year = posts.CreationTime.Year, CreationTime = posts.CreationTime.TryToDateTime() }) .GroupBy(x => x.Year) .Select(x => new QueryPostDto { Year = x.Key, Posts = x.ToList() }); result.IsSuccess(list); return result; }); }这个查询有点特殊,联合查询了3张表,先查post_tags和tags,关联字段TagId,再根据PostId查询posts,指定查询条件tags.DisplayName==name,以CreationTime倒序排序,年份分组,筛选出所需字段返回,在BlogController中添加API。
/// <summary> /// 通过标签名称查询文章列表 /// </summary> /// <param></param> /// <returns></returns> [HttpGet] [Route("posts/tag")] public async Task<ServiceResult<IEnumerable<QueryPostDto>>> QueryPostsByTagAsync(string name) { return await _blogService.QueryPostsByTagAsync(name); }至此,基本上完成了博客前端所需的所有查询接口,就还剩下友链的查询,大家可以自己完成,后面如果需要什么新的接口再回头来写就好了。
开源地址:https://github.com/Meowv/Blog/tree/blog_tutorial
搭配下方课程学习更佳 ↓ ↓ ↓