fix ArticleController

main
wangqing 6 months ago
parent a9ae732898
commit ea4e4a85de

@ -27,6 +27,8 @@ public class Constants {
public static final String J2CACHE_REGION_JOURNAL_HOT_COMMENTS = "journal_hot_comments";
public static final String J2CACHE_REGION_ARTICLE = "article";
public static final String J2CACHE_REGION_ARTICLE_PAGE = "article_page";
public static final String J2CACHE_REGION_ARTICLE_DTO_PAGE = "article_dto_page";
public static final String J2CACHE_REGION_ADVERTISEMENT = "advertisement";
public static final String J2CACHE_REGION_ADVERTISEMENT_PAGE = "advertisement_page";

@ -2,11 +2,13 @@ package com.luoo.music.controller;
import api.PageResult;
import api.Result;
import com.luoo.music.dto.response.ArticleDTO;
import com.luoo.music.dto.response.cms.ArticleRespDTO;
import com.luoo.music.service.CMSArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -26,16 +28,21 @@ public class ArticleController {
@ApiOperation(value = "查询文章详情", notes = "查询文章详情")
@GetMapping("/{id}")
public Result<ArticleRespDTO> findOne(@ApiParam(value = "文章ID", required = true) @PathVariable String id){
return articleService.findOne(id);
public Result<ArticleDTO> findOne(@ApiParam(value = "文章ID", required = true) @PathVariable String id){
ArticleRespDTO articleRespDTO= articleService.findOne(id).getData();
ArticleDTO articleDTO = new ArticleDTO();
BeanUtils.copyProperties(articleRespDTO,articleDTO);
articleDTO.setAutoPush("1");
return Result.success(articleDTO);
}
@ApiOperation(value = "查询文章列表", notes = "查询文章列表")
@PostMapping("/search/{page}/{size}")
public Result<PageResult<ArticleRespDTO>> search(@ApiParam(value = "页码", required = true) @PathVariable int page,
public Result<PageResult<ArticleDTO>> search(@ApiParam(value = "页码", required = true) @PathVariable int page,
@ApiParam(value = "每页条数", required = true) @PathVariable int size){
return articleService.search(page, size);
return articleService.searchArticleDTO(page, size);
}
@ApiOperation(value = "文章阅览次数加一", notes = "文章阅览次数加一")

@ -0,0 +1,80 @@
package com.luoo.music.dto.response;
import com.luoo.music.dto.response.cms.ArticleRespDTO;
import com.luoo.music.pojo.Article;
import com.luoo.music.util.Constants;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import java.io.Serializable;
import java.time.format.DateTimeFormatter;
@Data
public class ArticleDTO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "ID")
private String id;
@ApiModelProperty(value = "文章标题")
private String title;
@ApiModelProperty(value = "文章类型")
private String type;
@ApiModelProperty(value = "文章封面路径")
private String image;
@ApiModelProperty(value = "文章详情")
private String content;
@ApiModelProperty(value = "概要")
private String summary;
@ApiModelProperty(value = "发布作者id")
private String userId;
@ApiModelProperty(value = "发布作者昵称")
private String userName;
@ApiModelProperty(value = "编辑日期,格式为: yyyy-MM-dd HH:mm:ss")
private String date;
@ApiModelProperty(value = "是否定时发布 否0 是1")
private String isScheduled;
@ApiModelProperty(value = "发布时间,格式为: yyyy-MM-ddTHH:mm:ss.sssZ")
private String pubTime;
@ApiModelProperty(value = "是否允许评论 否0 是1")
private String allowCommit;
@ApiModelProperty(value = "是否自动推送 否0 是1")
private String autoPush;
@ApiModelProperty(value = "总评论数,大于99显示99+")
private Long totalCommentReply;
@ApiModelProperty(value = "总阅览数,大于99显示99+")
private Long vistisNum;
@ApiModelProperty(value = "歌曲ID")
private String songId;
public static ArticleDTO convertPojo(Article article) {
ArticleDTO response = new ArticleDTO();
response.setId(article.getId());
response.setTitle(article.getTitle());
response.setType(article.getType());
if (StringUtils.isNotBlank(article.getImage())) {
response.setImage(Constants.ARTICLE_RESOURCE_PREFIX + article.getImage());
} else {
response.setImage("");
}
response.setSummary(article.getSummary());
response.setContent(article.getContent());
response.setUserId(article.getUserId());
response.setUserName(article.getUserName());
response.setDate(article.getUpdateTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setTotalCommentReply(article.getComment());
response.setVistisNum(article.getVisits());
response.setIsScheduled(article.getIsScheduled());
response.setPubTime(article.getPubTime().format(DateTimeFormatter.ISO_DATE_TIME));
response.setAllowCommit(article.getAllowCommit());
response.setAutoPush(article.getAutoPush());
response.setSongId(article.getSongId());
return response;
}
}

@ -6,6 +6,7 @@ import com.alibaba.fastjson.JSON;
import com.luoo.music.client.UserClient;
import com.luoo.music.dao.*;
import com.luoo.music.dto.request.cms.ArticleAddModel;
import com.luoo.music.dto.response.ArticleDTO;
import com.luoo.music.dto.response.TotalCommentVo;
import com.luoo.music.dto.response.cms.ArticleRespDTO;
import com.luoo.music.pojo.Article;
@ -264,6 +265,11 @@ public class CMSArticleService {
List<ArticleRespDTO> pageArticleFromCache = getPageArticleFromCache(page, size);
return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache));
}
public Result<PageResult<ArticleDTO>> searchArticleDTO(int page, int size) {
List<ArticleDTO> pageArticleFromCache = getPageArticleDTOFromCache(page, size);
return Result.success(new PageResult<>(Long.valueOf(pageArticleFromCache.size()), pageArticleFromCache));
}
private Specification<Article> buildSearchSpecification() {
return (Root<Article> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
List<Predicate> predicateList = new ArrayList<Predicate>();
@ -293,6 +299,12 @@ public class CMSArticleService {
return (List<ArticleRespDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_PAGE, page+","+size, key -> getPageArticle(page,size), false).getValue();
}
private List<ArticleDTO> getPageArticleDTOFromCache(int page , int size){
return (List<ArticleDTO>) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE_DTO_PAGE, page+","+size, key -> getPageArticleDTO(page,size), false).getValue();
}
private List<ArticleRespDTO> getPageArticle(int page , int size){
List<ArticleRespDTO> result = new ArrayList<>();
@ -310,6 +322,24 @@ public class CMSArticleService {
return result;
}
public List<ArticleDTO> getPageArticleDTO(int page,int size) {
List<ArticleDTO> result = new ArrayList<>();
PageRequest pageRequest = PageRequest.of(page - 1, size);
Specification<Article> articleSpecification = buildSearchSpecification();
Page<Article> ArticlePage = articleDao.findAll(articleSpecification,pageRequest);
List<Article> content = ArticlePage.getContent();
if (!CollectionUtils.isEmpty(content)) {
for (Article item : content) {
ArticleDTO response = ArticleDTO.convertPojo(item);
result.add(response);
}
}
return result;
}
private String getArticleFromCache(String id){
return (String) cacheChannel.get(constants.Constants.J2CACHE_REGION_ARTICLE, id, key -> getArticleById(id), false).getValue();
}

Loading…
Cancel
Save