/// <summary> /// 查询文章分页列表 /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <PageList <QueryArticlePageResponse> > QueryArticlePageAsync(QueryArticlePageRequest request, CancellationToken cancellationToken = default) { var sql = "select * from article where BloggerId=@BloggerId limit @Skip,@Take"; var param = new { BloggerId = request.UserId, Skip = (request.PageParm.Page - 1) * (request.PageParm.Size), Take = request.PageParm.Size }; var list = await _dapperHelper.QueryAsync <QueryArticlePageResponse>(sql, param, CommandFlags.None, cancellationToken); //查询分类和文章数据缓存 if (list == null) { return(null); } foreach (var a in list) { //查询分类 var categorySql = @"select c.Id as CategoryId,c.CategoryName from category_association as ca join category as c on ca.CategoryId = c.Id where ca.ArticleId=@ArticleId"; var cParam = new { ArticleId = Convert.ToInt64(a.Id) }; var cl = await _dapperHelper.QueryAsync <QueryCategoryResponse>(categorySql, cParam, CommandFlags.None, cancellationToken); //查询文章数据缓存 var articleData = await _articleDataRepository.GetByIdAsync(Convert.ToInt64(a.Id)); a.Like = articleData.Like; a.Comment = articleData.Comment; a.View = articleData.View; } //计算分页总数 var countSql = "select count(*) from article where BloggerId=@BloggerId limit @Skip,@Take"; var count = await _dapperHelper.QueryFirstOrDefaultAsync <int>(countSql, param, CommandFlags.None, cancellationToken); return(new PageList <QueryArticlePageResponse>(request.PageParm.Page, request.PageParm.Size, count, list)); }
/// <summary> /// Handler /// </summary> /// <param name="request"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task <ApiResult> Handle(ArticleLikeCommand request, CancellationToken cancellationToken) { var article = await _articleDataRepository.GetByIdAsync(request.ArticleId); if (article == null) { return(NotFound()); } article.IncLike(request.State); await _articleDataRepository.UpdateAsync(article); //await _articleDataRepository.UnitOfWork.SaveChangesAsync(cancellationToken); return(Ok()); }