基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(五)
基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(五)
转载于:https://github.com/Meowv/Blog
上篇文章完成了文章详情页数据查询和清除缓存的功能。
本篇继续完成分类、标签、友情链接的后台操作接口,还是那句话,这些纯CRUD的内容,建议还是自己动手完成比较好,本篇将不再啰嗦,直接贴代码,以供参考。
分类
添加接口:查询分类列表QueryCategoriesForAdminAsync()、新增分类InsertCategoryAsync(…)、更新分类UpdateCategoryAsync(…)、删除分类DeleteCategoryAsync(…)
#region Categories
///
/// 查询分类列表
///
///
Task<ServiceResult<IEnumerable>> QueryCategoriesForAdminAsync();
///
/// 新增分类
///
///
///
Task InsertCategoryAsync(EditCategoryInput input);
///
/// 更新分类
///
///
///
///
Task UpdateCategoryAsync(int id, EditCategoryInput input);
///
/// 删除分类
///
///
///
Task DeleteCategoryAsync(int id);
#endregion Categories
查询分类列表需要返回的模型类QueryCategoryForAdminDto.cs。
//QueryCategoryForAdminDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class QueryCategoryForAdminDto : QueryCategoryDto
{
///
/// 主键
///
public int Id { get; set; }
}
}
新增分类和更新分类需要的输入参数EditCategoryInput.cs,直接继承CategoryDto即可。
//EditCategoryInput.cs
namespace Meowv.Blog.Application.Contracts.Blog.Params
{
public class EditCategoryInput : CategoryDto
{
}
}
分别实现这几个接口。
///
/// 查询分类列表
///
///
public async Task<ServiceResult<IEnumerable>> QueryCategoriesForAdminAsync()
{
var result = new ServiceResult<IEnumerable>();
var posts = await _postRepository.GetListAsync();var categories = _categoryRepository.GetListAsync().Result.Select(x => new QueryCategoryForAdminDto
{Id = x.Id,CategoryName = x.CategoryName,DisplayName = x.DisplayName,Count = posts.Count(p => p.CategoryId == x.Id)
});result.IsSuccess(categories);
return result;
}
///
/// 新增分类
///
///
///
public async Task InsertCategoryAsync(EditCategoryInput input)
{
var result = new ServiceResult();
var category = ObjectMapper.Map<EditCategoryInput, Category>(input);
await _categoryRepository.InsertAsync(category);result.IsSuccess(ResponseText.INSERT_SUCCESS);
return result;
}
这里需要一条AutoMapper配置,将EditCategoryInput转换为Category,忽略Id字段。
CreateMap<EditCategoryInput, Category>().ForMember(x => x.Id, opt => opt.Ignore());
///
/// 更新分类
///
///
///
///
public async Task UpdateCategoryAsync(int id, EditCategoryInput input)
{
var result = new ServiceResult();
var category = await _categoryRepository.GetAsync(id);
category.CategoryName = input.CategoryName;
category.DisplayName = input.DisplayName;await _categoryRepository.UpdateAsync(category);result.IsSuccess(ResponseText.UPDATE_SUCCESS);
return result;
}
///
/// 删除分类
///
///
///
public async Task DeleteCategoryAsync(int id)
{
var result = new ServiceResult();
var category = await _categoryRepository.FindAsync(id);
if (null == category)
{result.IsFailed(ResponseText.WHAT_NOT_EXIST.FormatWith("Id", id));return result;
}await _categoryRepository.DeleteAsync(id);result.IsSuccess(ResponseText.DELETE_SUCCESS);
return result;
}
在BlogController.Admin.cs中添加接口。
#region Categories
///
/// 查询分类列表
///
///
[HttpGet]
[Authorize]
[Route(“admin/categories”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task<ServiceResult<IEnumerable>> QueryCategoriesForAdminAsync()
{
return await _blogService.QueryCategoriesForAdminAsync();
}
///
/// 新增分类
///
///
///
[HttpPost]
[Authorize]
[Route(“category”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task InsertCategoryAsync([FromBody] EditCategoryInput input)
{
return await _blogService.InsertCategoryAsync(input);
}
///
/// 更新分类
///
///
///
///
[HttpPut]
[Authorize]
[Route(“category”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task UpdateCategoryAsync([Required] int id, [FromBody] EditCategoryInput input)
{
return await _blogService.UpdateCategoryAsync(id, input);
}
///
/// 删除分类
///
///
///
[HttpDelete]
[Authorize]
[Route(“category”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task DeleteCategoryAsync([Required] int id)
{
return await _blogService.DeleteCategoryAsync(id);
}
#endregion Categories
图片
标签
添加接口:查询标签列表QueryTagsForAdminAsync()、新增标签InsertTagAsync(…)、更新标签UpdateTagAsync(…)、删除标签DeleteTagAsync(…)
#region Tags
///
/// 查询标签列表
///
///
Task<ServiceResult<IEnumerable>> QueryTagsForAdminAsync();
///
/// 新增标签
///
///
///
Task InsertTagAsync(EditTagInput input);
///
/// 更新标签
///
///
///
///
Task UpdateTagAsync(int id, EditTagInput input);
///
/// 删除标签
///
///
///
Task DeleteTagAsync(int id);
#endregion Tags
查询标签列表需要返回的模型类QueryTagForAdminDto.cs。
//QueryTagForAdminDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class QueryTagForAdminDto : QueryTagDto
{
///
/// 主键
///
public int Id { get; set; }
}
}
新增标签和更新标签需要的输入参数EditTagInput.cs,直接继承TagDto即可。
//EditTagInput.cs
namespace Meowv.Blog.Application.Contracts.Blog.Params
{
public class EditTagInput : TagDto
{
}
}
分别实现这几个接口。
///
/// 查询标签列表
///
///
public async Task<ServiceResult<IEnumerable>> QueryTagsForAdminAsync()
{
var result = new ServiceResult<IEnumerable>();
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;
}
///
/// 新增标签
///
///
///
public async Task 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;
}
这里需要一条AutoMapper配置,将EditCategoryInput转换为Tag,忽略Id字段。
CreateMap<EditTagInput, Tag>().ForMember(x => x.Id, opt => opt.Ignore());
///
/// 更新标签
///
///
///
///
public async Task UpdateTagAsync(int id, EditTagInput input)
{
var result = new ServiceResult();
var tag = await _tagRepository.GetAsync(id);
tag.TagName = input.TagName;
tag.DisplayName = input.DisplayName;await _tagRepository.UpdateAsync(tag);result.IsSuccess(ResponseText.UPDATE_SUCCESS);
return result;
}
///
/// 删除标签
///
///
///
public async Task DeleteTagAsync(int id)
{
var result = new ServiceResult();
var tag = await _tagRepository.FindAsync(id);
if (null == tag)
{result.IsFailed(ResponseText.WHAT_NOT_EXIST.FormatWith("Id", id));return result;
}await _tagRepository.DeleteAsync(id);
await _postTagRepository.DeleteAsync(x => x.TagId == id);result.IsSuccess(ResponseText.DELETE_SUCCESS);
return result;
}
在BlogController.Admin.cs中添加接口。
#region Tags
///
/// 查询标签列表
///
///
[HttpGet]
[Authorize]
[Route(“admin/tags”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task<ServiceResult<IEnumerable>> QueryTagsForAdminAsync()
{
return await _blogService.QueryTagsForAdminAsync();
}
///
/// 新增标签
///
///
///
[HttpPost]
[Authorize]
[Route(“tag”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task InsertTagAsync([FromBody] EditTagInput input)
{
return await _blogService.InsertTagAsync(input);
}
///
/// 更新标签
///
///
///
///
[HttpPut]
[Authorize]
[Route(“tag”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task UpdateTagAsync([Required] int id, [FromBody] EditTagInput input)
{
return await _blogService.UpdateTagAsync(id, input);
}
///
/// 删除标签
///
///
///
[HttpDelete]
[Authorize]
[Route(“tag”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task DeleteTagAsync([Required] int id)
{
return await _blogService.DeleteTagAsync(id);
}
#endregion Tags
图片
友链
添加接口:查询友链列表QueryFriendLinksForAdminAsync()、新增友链InsertFriendLinkAsync(…)、更新友链UpdateFriendLinkAsync(…)、删除友链DeleteFriendLinkAsync(…)
#region FriendLinks
///
/// 查询友链列表
///
///
Task<ServiceResult<IEnumerable>> QueryFriendLinksForAdminAsync();
///
/// 新增友链
///
///
///
Task InsertFriendLinkAsync(EditFriendLinkInput input);
///
/// 更新友链
///
///
///
///
Task UpdateFriendLinkAsync(int id, EditFriendLinkInput input);
///
/// 删除友链
///
///
///
Task DeleteFriendLinkAsync(int id);
#endregion FriendLinks
查询友链列表需要返回的模型类QueryFriendLinkForAdminDto.cs。
//QueryFriendLinkForAdminDto.cs
namespace Meowv.Blog.Application.Contracts.Blog
{
public class QueryFriendLinkForAdminDto : FriendLinkDto
{
///
/// 主键
///
public int Id { get; set; }
}
}
新增友链和更新友链需要的输入参数EditFriendLinkInput.cs,直接继承FriendLinkDto即可。
//EditFriendLinkInput .cs
namespace Meowv.Blog.Application.Contracts.Blog.Params
{
public class EditFriendLinkInput : FriendLinkDto
{
}
}
分别实现这几个接口。
///
/// 查询友链列表
///
///
public async Task<ServiceResult<IEnumerable>> QueryFriendLinksForAdminAsync()
{
var result = new ServiceResult<IEnumerable>();
var friendLinks = await _friendLinksRepository.GetListAsync();var dto = ObjectMapper.Map<List<FriendLink>, IEnumerable<QueryFriendLinkForAdminDto>>(friendLinks);result.IsSuccess(dto);
return result;
}
///
/// 新增友链
///
///
///
public async Task InsertFriendLinkAsync(EditFriendLinkInput input)
{
var result = new ServiceResult();
var friendLink = ObjectMapper.Map<EditFriendLinkInput, FriendLink>(input);
await _friendLinksRepository.InsertAsync(friendLink);// 执行清除缓存操作
await _blogCacheService.RemoveAsync(CachePrefix.Blog_FriendLink);result.IsSuccess(ResponseText.INSERT_SUCCESS);
return result;
}
///
/// 更新友链
///
///
///
///
public async Task UpdateFriendLinkAsync(int id, EditFriendLinkInput input)
{
var result = new ServiceResult();
var friendLink = await _friendLinksRepository.GetAsync(id);
friendLink.Title = input.Title;
friendLink.LinkUrl = input.LinkUrl;await _friendLinksRepository.UpdateAsync(friendLink);// 执行清除缓存操作
await _blogCacheService.RemoveAsync(CachePrefix.Blog_FriendLink);result.IsSuccess(ResponseText.UPDATE_SUCCESS);
return result;
}
///
/// 删除友链
///
///
///
public async Task DeleteFriendLinkAsync(int id)
{
var result = new ServiceResult();
var friendLink = await _friendLinksRepository.FindAsync(id);
if (null == friendLink)
{result.IsFailed(ResponseText.WHAT_NOT_EXIST.FormatWith("Id", id));return result;
}await _friendLinksRepository.DeleteAsync(id);// 执行清除缓存操作
await _blogCacheService.RemoveAsync(CachePrefix.Blog_FriendLink);result.IsSuccess(ResponseText.DELETE_SUCCESS);
return result;
}
其中查询友链列表和新增友链中有两条AutoMapper配置。
CreateMap<FriendLink, QueryFriendLinkForAdminDto>();
CreateMap<EditFriendLinkInput, FriendLink>().ForMember(x => x.Id, opt => opt.Ignore());
在BlogController.Admin.cs中添加接口。
#region FriendLinks
///
/// 查询友链列表
///
///
[HttpGet]
[Authorize]
[Route(“admin/friendlinks”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task<ServiceResult<IEnumerable>> QueryFriendLinksForAdminAsync()
{
return await _blogService.QueryFriendLinksForAdminAsync();
}
///
/// 新增友链
///
///
///
[HttpPost]
[Authorize]
[Route(“friendlink”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task InsertFriendLinkAsync([FromBody] EditFriendLinkInput input)
{
return await _blogService.InsertFriendLinkAsync(input);
}
///
/// 更新友链
///
///
///
///
[HttpPut]
[Authorize]
[Route(“friendlink”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task UpdateFriendLinkAsync([Required] int id, [FromBody] EditFriendLinkInput input)
{
return await _blogService.UpdateFriendLinkAsync(id, input);
}
///
/// 删除友链
///
///
///
[HttpDelete]
[Authorize]
[Route(“friendlink”)]
[ApiExplorerSettings(GroupName = Grouping.GroupName_v2)]
public async Task DeleteFriendLinkAsync([Required] int id)
{
return await _blogService.DeleteFriendLinkAsync(id);
}
#endregion
图片
Next
截止本篇,基于 abp vNext 和 .NET Core 开发博客项目 系列的后台API部分便全部开发完成了。
本博客项目系列是我一边写代码一边记录后的成果,并不是开发完成后再拿出来写的,涉及到东西也不是很多,对于新手入门来说应该是够了的,如果你从中有所收获请多多转发分享。
在此,希望大家可以关注一下我的微信公众号:『阿星Plus』,文章将会首发在公众号中。
现在有了API,大家可以选择自己熟悉的方式去开发前端界面,比如目前我博客的线上版本就是用的 ASP.NET Core Web ,感兴趣的可以去 release 分支查看。
关于前端部分,看到有人呼吁vue,说实话前端技术不是很厉害,本职主要是后端开发,可能达不到预期效果。
所以我准备入坑 Blazor
基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(五)相关推荐
- 基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目
基于 abp vNext 和 .NET Core 开发博客项目 - 终结篇之发布项目 转载于:https://github.com/Meowv/Blog 既然开发完成了,还是拿出来溜溜比较好,本篇是本 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(九)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(九) 转载于:https://github.com/Meowv/Blog 终于要接近尾声了,上一篇基本上将文 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(八)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(八) 转载于:https://github.com/Meowv/Blog 上一篇完成了标签模块和友情链接模块 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(七)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(七) 转载于:https://github.com/Meowv/Blog 上一篇完成了后台分类模块的所有功能 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(六)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(六) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客文章详情页面的数据 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(五)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(五) 转载于:https://github.com/Meowv/Blog 上一篇完成了分类标签友链的列表查询 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(四)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(四) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客的分页查询文章列表 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(三) 转载于:https://github.com/Meowv/Blog 上一篇完成了博客的主题切换,菜单和 ...
- 基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(二)
基于 abp vNext 和 .NET Core 开发博客项目 - Blazor 实战系列(二) 转载于:https://github.com/Meowv/Blog 上一篇搭建了 Blazor 项目并 ...
最新文章
- [实现] 利用 Seq2Seq 预测句子后续字词 (Pytorch)
- python编程小游戏-使用Python写一个小游戏
- [PAL编程规范]SAP HANA PAL多项式回归预测分析Polynomial Regression编程规范FORECASTWITHPOLYNOMIALR(预测)...
- asp.net MVC中的tip
- 坑爹的日志无法按天切割问题
- Spring-Boot + AOP实现多数据源动态切换
- linux内核模块间通信
- pythoncde-实战1--坐标生成
- android native c++ 打印调用栈
- 一加手机刷入第三方Rec
- 【闲置路由器的有效利用】路由器有线桥接实现无线漫游
- 淘宝评论API接口,item_review-获得淘宝商品评论API接口接入说明
- 数据库服务Amozon DynamoDB(入门分享)
- 服务器的虚拟机网速如何分配,管理ESXi主机网络与虚拟机网络
- Matlab中结构体struct创建和使用
- 江南大学计算机技术复试科目,江南大学计算机专硕考哪些科目
- Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem(dp 最大子区间)
- GWO(灰狼优化)算法MATLAB源码逐行中文注解()
- 小程序开发——模板与配置
- 【演示文稿制作软件】Focusky教程 | 巧用半透明色块来提升演示文稿颜值