基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(一)
基于 abp vNext 和 .NET Core 開發(fā)博客項(xiàng)目 - 博客接口實(shí)戰(zhàn)篇(一)
轉(zhuǎn)載于:https://github.com/Meowv/Blog
現(xiàn)在博客數(shù)據(jù)庫中的數(shù)據(jù)是比較混亂的,為了看起來像那么回事,顯得正式一點(diǎn),我先手動搞點(diǎn)數(shù)據(jù)進(jìn)去。
圖片
搞定了種子數(shù)據(jù),就可以去愉快的寫接口了,我這里將根據(jù)我現(xiàn)在的博客頁面去分析所需要接口,感興趣的去點(diǎn)點(diǎn)。
為了讓接口看起來清晰,一目了然,刪掉之前在IBlogService中添加的所有接口,將5個自定義倉儲全部添加至BlogService中,然后用partial修飾。
//IBlogService.cs
public partial interface IBlogService
{
}
//BlogService.cs
using Meowv.Blog.Application.Caching.Blog;
using Meowv.Blog.Domain.Blog.Repositories;
namespace Meowv.Blog.Application.Blog.Impl
{
public partial class BlogService : ServiceBase, IBlogService
{
private readonly IBlogCacheService _blogCacheService;
private readonly IPostRepository _postRepository;
private readonly ICategoryRepository _categoryRepository;
private readonly ITagRepository _tagRepository;
private readonly IPostTagRepository _postTagRepository;
private readonly IFriendLinkRepository _friendLinksRepository;
}
在Blog文件夾下依次添加接口:IBlogService.Post.cs、IBlogService.Category.cs、IBlogService.Tag.cs、IBlogService.FriendLink.cs、IBlogService.Admin.cs。
在Blog/Impl文件夾下添加實(shí)現(xiàn)類:IBlogService.Post.cs、BlogService.Category.cs、BlogService.Tag.cs、BlogService.FriendLink.cs、BlogService.Admin.cs。
同上,.Application.Caching層也按照這個樣子添加。
注意都需要添加partial修飾為局部的接口和實(shí)現(xiàn)類,所有文章相關(guān)的接口放在IBlogService.Post.cs中,分類放在IBlogService.Category.cs,標(biāo)簽放在IBlogService.Tag.cs,友鏈放在IBlogService.FriendLink.cs,后臺增刪改所有接口放在IBlogService.Admin.cs,最終效果圖如下:
圖片
文章列表頁
圖片
分析:列表帶分頁,以文章發(fā)表的年份分組,所需字段:標(biāo)題、鏈接、時間、年份。
在.Application.Contracts層Blog文件夾下添加返回的模型:QueryPostDto.cs。
//QueryPostDto.cs
using System.Collections.Generic;
namespace Meowv.Blog.Application.Contracts.Blog
{
public class QueryPostDto
{
///
/// 年份
///
public int Year { get; set; }
}
模型為一個年份和一個文章列表,文章列表模型:PostBriefDto.cs。
//PostBriefDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class PostBriefDto
{
///
/// 標(biāo)題
///
public string Title { get; set; }
}
搞定,因?yàn)榉祷貢r間為英文格式,所以CreationTime給了字符串類型。
在IBlogService.Post.cs中添加接口分頁查詢文章列表QueryPostsAsync,肯定需要接受倆參數(shù)分頁頁碼和分頁數(shù)量。還是去添加一個公共模型PagingInput吧,在.Application.Contracts下面。
//PagingInput.cs
using System.ComponentModel.DataAnnotations;
namespace Meowv.Blog.Application.Contracts
{
///
/// 分頁輸入?yún)?shù)
///
public class PagingInput
{
///
/// 頁碼
///
[Range(1, int.MaxValue)]
public int Page { get; set; } = 1;
}
Page設(shè)置默認(rèn)值為1,Limit設(shè)置默認(rèn)值為10,Range Attribute設(shè)置參數(shù)可輸入大小限制,于是這個分頁查詢文章列表的接口就是這個樣子的。
//IBlogService.Post.cs
public partial interface IBlogService
{
///
/// 分頁查詢文章列表
///
///
///
Task<ServiceResult<PagedList>> QueryPostsAsync(PagingInput input);
}
ServiceResult和PagedList是之前添加的統(tǒng)一返回模型,緊接著就去添加一個分頁查詢文章列表緩存接口,和上面是對應(yīng)的。
//IBlogCacheService.Post.cs
using Meowv.Blog.Application.Contracts;
using Meowv.Blog.Application.Contracts.Blog;
using Meowv.Blog.ToolKits.Base;
using System;
using System.Threading.Tasks;
namespace Meowv.Blog.Application.Caching.Blog
{
public partial interface IBlogCacheService
{
///
/// 分頁查詢文章列表
///
///
///
///
Task<ServiceResult<PagedList>> QueryPostsAsync(PagingInput input, Func<Task<ServiceResult<PagedList>>> factory);
}
}
分別實(shí)現(xiàn)這兩個接口。
//BlogCacheService.Post.cs
public partial class BlogCacheService
{
private const string KEY_QueryPosts = “Blog:Post:QueryPosts-{0}-{1}”;
}
//BlogService.Post.cs
///
/// 分頁查詢文章列表
///
///
///
public async Task<ServiceResult<PagedList>> QueryPostsAsync(PagingInput input)
{
return await _blogCacheService.QueryPostsAsync(input, async () =>
{
var result = new ServiceResult<PagedList>();
}
PageByIndex(…)、TryToDateTime()是.ToolKits層添加的擴(kuò)展方法,先查詢總數(shù),然后根據(jù)時間倒序,分頁,篩選出所需字段,根據(jù)年份分組,輸出,結(jié)束。
在BlogController中添加API。
///
/// 分頁查詢文章列表
///
///
///
[HttpGet]
[Route(“posts”)]
public async Task<ServiceResult<PagedList>> QueryPostsAsync([FromQuery] PagingInput input)
{
return await _blogService.QueryPostsAsync(input);
}
[FromQuery]設(shè)置input為從URL進(jìn)行查詢參數(shù),編譯運(yùn)行看效果。
圖片
已經(jīng)可以查詢出數(shù)據(jù),并且緩存至Redis中。
獲取文章詳情
圖片
分析:文章詳情頁,文章的標(biāo)題、作者、發(fā)布時間、所屬分類、標(biāo)簽列表、文章內(nèi)容(HTML和MarkDown)、鏈接、上下篇的標(biāo)題和鏈接。
創(chuàng)建返回模型:PostDetailDto.cs
//PostDetailDto.cs
using System.Collections.Generic;
namespace Meowv.Blog.Application.Contracts.Blog
{
public class PostDetailDto
{
///
/// 標(biāo)題
///
public string Title { get; set; }
}
同時添加CategoryDto、TagDto、PostForPagedDto。
//CategoryDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class CategoryDto
{
///
/// 分類名稱
///
public string CategoryName { get; set; }
}
//TagDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class TagDto
{
///
/// 標(biāo)簽名稱
///
public string TagName { get; set; }
}
//PostForPagedDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class PostForPagedDto
{
///
/// 標(biāo)題
///
public string Title { get; set; }
}
添加獲取文章詳情接口和緩存的接口。
//IBlogService.Post.cs
public partial interface IBlogService
{
///
/// 根據(jù)URL獲取文章詳情
///
///
///
Task<ServiceResult> GetPostDetailAsync(string url);
}
//IBlogCacheService.Post.cs
public partial interface IBlogCacheService
{
///
/// 根據(jù)URL獲取文章詳情
///
///
///
Task<ServiceResult> GetPostDetailAsync(string url, Func<Task<ServiceResult>> factory);
}
分別實(shí)現(xiàn)這兩個接口。
//BlogCacheService.Post.cs
public partial class BlogCacheService
{
private const string KEY_GetPostDetail = “Blog:Post:GetPostDetail-{0}”;
}
//BlogService.Post.cs
///
/// 根據(jù)URL獲取文章詳情
///
///
///
public async Task<ServiceResult> GetPostDetailAsync(string url)
{
return await _blogCacheService.GetPostDetailAsync(url, async () =>
{
var result = new ServiceResult();
}
ResponseText.WHAT_NOT_EXIST是定義在MeowvBlogConsts.cs的常量。
TryToDateTime()和列表查詢中的擴(kuò)展方法一樣,轉(zhuǎn)換時間為想要的格式。
簡單說一下查詢邏輯,先根據(jù)參數(shù)url,查詢是否存在數(shù)據(jù),如果文章不存在則返回錯誤消息。
然后根據(jù) post.CategoryId 就可以查詢到當(dāng)前文章的分類名稱。
聯(lián)合查詢post_tags和tag兩張表,指定查詢條件post.Id,查詢當(dāng)前文章的所有標(biāo)簽。
最后上下篇的邏輯也很簡單,上一篇取大于當(dāng)前文章發(fā)布時間的第一篇,下一篇取時間倒序排序并且小于當(dāng)前文章發(fā)布時間的第一篇文章。
最后將所有查詢到的數(shù)據(jù)賦值給輸出對象,返回,結(jié)束。
在BlogController中添加API。
///
/// 根據(jù)URL獲取文章詳情
///
///
///
[HttpGet]
[Route(“post”)]
public async Task<ServiceResult> GetPostDetailAsync(string url)
{
return await _blogService.GetPostDetailAsync(url);
}
編譯運(yùn)行,然后輸入URL查詢一條文章詳情數(shù)據(jù)。
圖片
成功輸出預(yù)期內(nèi)容,緩存同時也是ok的。
開源地址:https://github.com/Meowv/Blog/tree/blog_tutorial
總結(jié)
以上是生活随笔為你收集整理的基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于 abp vNext 和 .NET
- 下一篇: 基于 abp vNext 和 .NET