在MeowvBlogAutoMapperProfile.cs中添加一条配置:CreateMap<FriendLink, FriendLinkDto>();,在BlogController中添加API。
/// <summary> /// 查询友链列表 /// </summary> /// <returns></returns> [HttpGet] [Route("friendlinks")] public async Task<ServiceResult<IEnumerable<FriendLinkDto>>> QueryFriendLinksAsync() { return await _blogService.QueryFriendLinksAsync(); }编译运行,打开查询友链的API,此时没数据,手动添加几条数据进去再试试吧。
文章管理后台文章管理包含:文章列表、新增、更新、删除文章,接下来依次完成这些接口。
文章列表这里的文章列表和前台的文章列表差不多,就是多了一个Id,以供编辑和删除使用,所以可以新建一个模型类QueryPostForAdminDto继承QueryPostDto,添加PostBriefForAdminDto继承PostBriefDto同时新增一个字段主键Id。
在QueryPostForAdminDto中隐藏基类成员Posts,使用新的接收类型:IEnumerable<PostBriefForAdminDto>。
//PostBriefForAdminDto.cs namespace Meowv.Blog.Application.Contracts.Blog { public class PostBriefForAdminDto : PostBriefDto { /// <summary> /// 主键 /// </summary> public int Id { get; set; } } } //QueryPostForAdminDto.cs using System.Collections.Generic; namespace Meowv.Blog.Application.Contracts.Blog { public class QueryPostForAdminDto : QueryPostDto { /// <summary> /// Posts /// </summary> public new IEnumerable<PostBriefForAdminDto> Posts { get; set; } } }添加分页查询文章列表的接口:QueryPostsForAdminAsync(),关于后台的一些接口就不添加缓存了。
//IBlogService.Admin.cs using Meowv.Blog.Application.Contracts; using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog { public partial interface IBlogService { /// <summary> /// 分页查询文章列表 /// </summary> /// <param></param> /// <returns></returns> Task<ServiceResult<PagedList<QueryPostForAdminDto>>> QueryPostsForAdminAsync(PagingInput input); } }然后实现这个接口。
//BlogService.Admin.cs using Meowv.Blog.Application.Contracts; using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using Meowv.Blog.ToolKits.Extensions; using System.Linq; using System.Threading.Tasks; namespace Meowv.Blog.Application.Blog.Impl { public partial class BlogService { /// <summary> /// 分页查询文章列表 /// </summary> /// <param></param> /// <returns></returns> public async Task<ServiceResult<PagedList<QueryPostForAdminDto>>> QueryPostsForAdminAsync(PagingInput input) { var result = new ServiceResult<PagedList<QueryPostForAdminDto>>(); var count = await _postRepository.GetCountAsync(); var list = _postRepository.OrderByDescending(x => x.CreationTime) .PageByIndex(input.Page, input.Limit) .Select(x => new PostBriefForAdminDto { Id = x.Id, Title = x.Title, Url = x.Url, Year = x.CreationTime.Year, CreationTime = x.CreationTime.TryToDateTime() }) .GroupBy(x => x.Year) .Select(x => new QueryPostForAdminDto { Year = x.Key, Posts = x.ToList() }).ToList(); result.IsSuccess(new PagedList<QueryPostForAdminDto>(count.TryToInt(), list)); return result; } } }实现逻辑也非常简单和之前一样,就是在Select的时候多了一个Id,添加一个新的Controller:BlogController.Admin.cs,添加这个接口。
//BlogController.Admin.cs using Meowv.Blog.Application.Contracts; using Meowv.Blog.Application.Contracts.Blog; using Meowv.Blog.ToolKits.Base; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Threading.Tasks; using static Meowv.Blog.Domain.Shared.MeowvBlogConsts; namespace Meowv.Blog.HttpApi.Controllers { public partial class BlogController { /// <summary> /// 分页查询文章列表 /// </summary> /// <param></param> /// <returns></returns> [HttpGet] [Authorize] [Route("admin/posts")] [ApiExplorerSettings(GroupName = Grouping.GroupName_v2)] public async Task<ServiceResult<PagedList<QueryPostForAdminDto>>> QueryPostsForAdminAsync([FromQuery] PagingInput input) { return await _blogService.QueryPostsForAdminAsync(input); } } }因为是后台的接口,所以加上AuthorizeAttribute,指定接口组为GroupName_v2,参数方式为[FromQuery]。
当没有进行授权的时候,是无法访问接口的。