在BlogController.Admin.cs中添加接口。
#region Categories /// <summary> /// 查询分类列表 /// </summary> /// <returns></returns> [HttpGet] [Authorize] [Route("admin/categories")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult<IEnumerable<QueryCategoryForAdminDto>>> QueryCategoriesForAdminAsync() { return await _blogService.QueryCategoriesForAdminAsync(); } /// <summary> /// 新增分类 /// </summary> /// <param></param> /// <returns></returns> [HttpPost] [Authorize] [Route("category")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> InsertCategoryAsync([FromBody] EditCategoryInput input) { return await _blogService.InsertCategoryAsync(input); } /// <summary> /// 更新分类 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> [HttpPut] [Authorize] [Route("category")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> UpdateCategoryAsync([Required] int id, [FromBody] EditCategoryInput input) { return await _blogService.UpdateCategoryAsync(id, input); } /// <summary> /// 删除分类 /// </summary> /// <param></param> /// <returns></returns> [HttpDelete] [Authorize] [Route("category")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> DeleteCategoryAsync([Required] int id) { return await _blogService.DeleteCategoryAsync(id); } #endregion Categories 标签添加接口:查询标签列表QueryTagsForAdminAsync()、新增标签InsertTagAsync(...)、更新标签UpdateTagAsync(...)、删除标签DeleteTagAsync(...)
#region Tags /// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> Task<ServiceResult<IEnumerable<QueryTagForAdminDto>>> QueryTagsForAdminAsync(); /// <summary> /// 新增标签 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult> InsertTagAsync(EditTagInput input); /// <summary> /// 更新标签 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<ServiceResult> UpdateTagAsync(int id, EditTagInput input); /// <summary> /// 删除标签 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult> DeleteTagAsync(int id); #endregion Tags查询标签列表需要返回的模型类QueryTagForAdminDto.cs。
//QueryTagForAdminDto.cs namespace Meowv.Blog.Application.Contracts.Blog { public class QueryTagForAdminDto : QueryTagDto { /// <summary> /// 主键 /// </summary> public int Id { get; set; } } }新增标签和更新标签需要的输入参数EditTagInput.cs,直接继承TagDto即可。
//EditTagInput.cs namespace Meowv.Blog.Application.Contracts.Blog.Params { public class EditTagInput : TagDto { } }分别实现这几个接口。
/// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> public async Task<ServiceResult<IEnumerable<QueryTagForAdminDto>>> QueryTagsForAdminAsync() { var result = new ServiceResult<IEnumerable<QueryTagForAdminDto>>(); var post_tags = await _postTagRepository.GetListAsync(); var tags = _tagRepository.GetListAsync().Result.Select(x => new QueryTagForAdminDto { Id = x.Id, TagName = x.TagName, DisplayName = x.DisplayName, Count = post_tags.Count(p => p.TagId == x.Id) }); result.IsSuccess(tags); return result; } /// <summary> /// 新增标签 /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult> InsertTagAsync(EditTagInput input) { var result = new ServiceResult(); var tag = ObjectMapper.Map<EditTagInput, Tag>(input); await _tagRepository.InsertAsync(tag); result.IsSuccess(ResponseText.INSERT_SUCCESS); return result; }