基於 abp vNext 和 .NET Core 開發博客項目 – 博客接口實戰篇(四)

系列文章

  1. 基於 abp vNext 和 .NET Core 開發博客項目 – 使用 abp cli 搭建項目
  2. 基於 abp vNext 和 .NET Core 開發博客項目 – 給項目瘦身,讓它跑起來
  3. 基於 abp vNext 和 .NET Core 開發博客項目 – 完善與美化,Swagger登場
  4. 基於 abp vNext 和 .NET Core 開發博客項目 – 數據訪問和代碼優先
  5. 基於 abp vNext 和 .NET Core 開發博客項目 – 自定義倉儲之增刪改查
  6. 基於 abp vNext 和 .NET Core 開發博客項目 – 統一規範API,包裝返回模型
  7. 基於 abp vNext 和 .NET Core 開發博客項目 – 再說Swagger,分組、描述、小綠鎖
  8. 基於 abp vNext 和 .NET Core 開發博客項目 – 接入GitHub,用JWT保護你的API
  9. 基於 abp vNext 和 .NET Core 開發博客項目 – 異常處理和日誌記錄
  10. 基於 abp vNext 和 .NET Core 開發博客項目 – 使用Redis緩存數據
  11. 基於 abp vNext 和 .NET Core 開發博客項目 – 集成Hangfire實現定時任務處理
  12. 基於 abp vNext 和 .NET Core 開發博客項目 – 用AutoMapper搞定對象映射
  13. 基於 abp vNext 和 .NET Core 開發博客項目 – 定時任務最佳實戰(一)
  14. 基於 abp vNext 和 .NET Core 開發博客項目 – 定時任務最佳實戰(二)
  15. 基於 abp vNext 和 .NET Core 開發博客項目 – 定時任務最佳實戰(三)
  16. 基於 abp vNext 和 .NET Core 開發博客項目 – 博客接口實戰篇(一)
  17. 基於 abp vNext 和 .NET Core 開發博客項目 – 博客接口實戰篇(二)
  18. 基於 abp vNext 和 .NET Core 開發博客項目 – 博客接口實戰篇(三)

上篇文章完成了文章增刪改的接口和友情鏈接列表的接口,本篇繼續。

善於思考的同學肯定發現,在執行增刪改操作后,Redis緩存中的數據還是存在的,也就意味着查詢接口返回的數據還是舊的,所以在寫接口之前,先完成一下清緩存的操作。

移除緩存

移除緩存我這裏找了一個新的包:Caching.CSRedis,選他是因為微軟的包Microsoft.Extensions.Caching.StackExchangeRedis沒有給我們實現批量刪除的功能。

Caching.CSRedis開源地址,https://github.com/2881099/csredis 在這不做過多介紹,感興趣的自己去看。

.Application.Caching層添加包Caching.CSRedisInstall-Package Caching.CSRedis,然後在模塊類MeowvBlogApplicationCachingModule中進行配置。

//MeowvBlogApplicationCachingModule.cs
...
public override void ConfigureServices(ServiceConfigurationContext context)
{
    ...

    var csredis = new CSRedis.CSRedisClient(AppSettings.Caching.RedisConnectionString);
    RedisHelper.Initialization(csredis);

    context.Services.AddSingleton<IDistributedCache>(new CSRedisCache(RedisHelper.Instance));
}
...

直接新建一個移除緩存的接口:ICacheRemoveService,添加移除緩存的方法RemoveAsync()。代碼較少,可以直接寫在緩存基類CachingServiceBase中。

public interface ICacheRemoveService
{
    /// <summary>
    /// 移除緩存
    /// </summary>
    /// <param name="key"></param>
    /// <param name="cursor"></param>
    /// <returns></returns>
    Task RemoveAsync(string key, int cursor = 0);
}

然後可以在基類中實現這個接口。

public async Task RemoveAsync(string key, int cursor = 0)
{
    var scan = await RedisHelper.ScanAsync(cursor);
    var keys = scan.Items;

    if (keys.Any() && key.IsNotNullOrEmpty())
    {
        keys = keys.Where(x => x.StartsWith(key)).ToArray();

        await RedisHelper.DelAsync(keys);
    }
}

簡單說一下這個操作過程,使用ScanAsync()獲取到所有的Redis key值,返回的是一個string數組,然後根據參數找到符合此前綴的所有key,最後調用DelAsync(keys)刪除緩存。

在需要有移除緩存功能的接口上繼承ICacheRemoveService,這裏就是IBlogCacheService

//IBlogCacheService.cs
namespace Meowv.Blog.Application.Caching.Blog
{
    public partial interface IBlogCacheService : ICacheRemoveService
    {
    }
}

在基類中已經實現了這個接口,所以現在所有繼承基類的緩存實現類都可以調用移除緩存方法了。

MeowvBlogConsts中添加緩存前綴的常量。

//MeowvBlogConsts.cs
/// <summary>
/// 緩存前綴
/// </summary>
public static class CachePrefix
{
    public const string Authorize = "Authorize";

    public const string Blog = "Blog";

    public const string Blog_Post = Blog + ":Post";

    public const string Blog_Tag = Blog + ":Tag";

    public const string Blog_Category = Blog + ":Category";

    public const string Blog_FriendLink = Blog + ":FriendLink";
}

然後在BlogService.Admin.cs服務執行增刪改后調用移除緩存的方法。

//BlogService.Admin.cs

// 執行清除緩存操作
await _blogCacheService.RemoveAsync(CachePrefix.Blog_Post);

因為是小項目,採用這種策略直接刪除緩存,這樣就搞定了當在執行增刪改操作后,前台接口可以實時查詢出最後的結果。

文章詳情

當我們修改文章數據的時候,是需要把當前數據庫中的數據帶出來显示在界面上的,因為有可能只是個別地方需要修改,所以這還需要一個查詢文章詳情的接口,當然這裏的詳情和前端的是不一樣的,這裡是需要根據Id主鍵去查詢。

添加模型類PostForAdminDto.cs,直接繼承PostDto,然後添加一個Tags列表就行,==,好像和上一篇文章中的EditPostInput字段是一模一樣的。順手將EditPostInput改一下吧,具體代碼如下:

//PostForAdminDto.cs
using System.Collections.Generic;

namespace Meowv.Blog.Application.Contracts.Blog
{
    public class PostForAdminDto : PostDto
    {
        /// <summary>
        /// 標籤列表
        /// </summary>
        public IEnumerable<string> Tags { get; set; }
    }
}

//EditPostInput.cs
namespace Meowv.Blog.Application.Contracts.Blog.Params
{
    public class EditPostInput : PostForAdminDto
    {
    }
}

IBlogService.Admin.cs中添加接口。

/// <summary>
/// 獲取文章詳情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Task<ServiceResult<PostForAdminDto>> GetPostForAdminAsync(int id);

實現這個接口。

/// <summary>
/// 獲取文章詳情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<ServiceResult<PostForAdminDto>> GetPostForAdminAsync(int id)
{
    var result = new ServiceResult<PostForAdminDto>();

    var post = await _postRepository.GetAsync(id);

    var tags = from post_tags in await _postTagRepository.GetListAsync()
               join tag in await _tagRepository.GetListAsync()
               on post_tags.TagId equals tag.Id
               where post_tags.PostId.Equals(post.Id)
               select tag.TagName;

    var detail = ObjectMapper.Map<Post, PostForAdminDto>(post);
    detail.Tags = tags;
    detail.Url = post.Url.Split("/").Where(x => !string.IsNullOrEmpty(x)).Last();

    result.IsSuccess(detail);
    return result;
}

先根據Id查出文章數據,再通過聯合查詢找出標籤數據。

CreateMap<Post, PostForAdminDto>().ForMember(x => x.Tags, opt => opt.Ignore());

新建一條AutoMapper配置,將Post轉換成PostForAdminDto,忽略Tags。

然後將查出來的標籤、Url賦值給DTO,輸出即可。在BlogController.Admin中添加API。

/// <summary>
/// 獲取文章詳情
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpGet]
[Authorize]
[Route("admin/post")]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task<ServiceResult<PostForAdminDto>> GetPostForAdminAsync([Required] int id)
{
    return await _blogService.GetPostForAdminAsync(id);
}

至此,完成了關於文章的所有接口。

接下來按照以上方式依次完成分類、標籤、友鏈的增刪改查接口,我覺得如果你有跟着我一起做,剩下的可以自己完成。

開源地址:https://github.com/Meowv/Blog/tree/blog_tutorial

搭配下方課程學習更佳 ↓ ↓ ↓

http://gk.link/a/10iQ7

本站聲明:網站內容來源於博客園,如有侵權,請聯繫我們,我們將及時處理

【其他文章推薦】

網頁設計一頭霧水該從何著手呢? 台北網頁設計公司幫您輕鬆架站!

網頁設計公司推薦不同的風格,搶佔消費者視覺第一線

※Google地圖已可更新顯示潭子電動車充電站設置地點!!

※廣告預算用在刀口上,台北網頁設計公司幫您達到更多曝光效益

※別再煩惱如何寫文案,掌握八大原則!

網頁設計最專業,超強功能平台可客製化

※回頭車貨運收費標準

您可能也會喜歡…