基于 abp vNext 和 .NET Core 开发博客项目 - 博客接口实战篇(五) (3)

这里需要一条AutoMapper配置,将EditCategoryInput转换为Tag,忽略Id字段。

CreateMap<EditTagInput, Tag>().ForMember(x => x.Id, opt => opt.Ignore()); /// <summary> /// 更新标签 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> public async Task<ServiceResult> 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; } /// <summary> /// 删除标签 /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult> 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 /// <summary> /// 查询标签列表 /// </summary> /// <returns></returns> [HttpGet] [Authorize] [Route("admin/tags")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult<IEnumerable<QueryTagForAdminDto>>> QueryTagsForAdminAsync() { return await _blogService.QueryTagsForAdminAsync(); } /// <summary> /// 新增标签 /// </summary> /// <param></param> /// <returns></returns> [HttpPost] [Authorize] [Route("tag")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> InsertTagAsync([FromBody] EditTagInput input) { return await _blogService.InsertTagAsync(input); } /// <summary> /// 更新标签 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> [HttpPut] [Authorize] [Route("tag")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> UpdateTagAsync([Required] int id, [FromBody] EditTagInput input) { return await _blogService.UpdateTagAsync(id, input); } /// <summary> /// 删除标签 /// </summary> /// <param></param> /// <returns></returns> [HttpDelete] [Authorize] [Route("tag")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult> DeleteTagAsync([Required] int id) { return await _blogService.DeleteTagAsync(id); } #endregion Tags

1

友链

添加接口:查询友链列表QueryFriendLinksForAdminAsync()、新增友链InsertFriendLinkAsync(...)、更新友链UpdateFriendLinkAsync(...)、删除友链DeleteFriendLinkAsync(...)

#region FriendLinks /// <summary> /// 查询友链列表 /// </summary> /// <returns></returns> Task<ServiceResult<IEnumerable<QueryFriendLinkForAdminDto>>> QueryFriendLinksForAdminAsync(); /// <summary> /// 新增友链 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult> InsertFriendLinkAsync(EditFriendLinkInput input); /// <summary> /// 更新友链 /// </summary> /// <param></param> /// <param></param> /// <returns></returns> Task<ServiceResult> UpdateFriendLinkAsync(int id, EditFriendLinkInput input); /// <summary> /// 删除友链 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult> DeleteFriendLinkAsync(int id); #endregion FriendLinks

查询友链列表需要返回的模型类QueryFriendLinkForAdminDto.cs。

//QueryFriendLinkForAdminDto.cs namespace Meowv.Blog.Application.Contracts.Blog { public class QueryFriendLinkForAdminDto : FriendLinkDto { /// <summary> /// 主键 /// </summary> public int Id { get; set; } } }

新增友链和更新友链需要的输入参数EditFriendLinkInput.cs,直接继承FriendLinkDto即可。

//EditFriendLinkInput .cs namespace Meowv.Blog.Application.Contracts.Blog.Params { public class EditFriendLinkInput : FriendLinkDto { } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wssfww.html