diff --git a/luoo_music/src/main/java/com/luoo/music/controller/EssayController.java b/luoo_music/src/main/java/com/luoo/music/controller/EssayController.java index 71a45c7..5642fac 100644 --- a/luoo_music/src/main/java/com/luoo/music/controller/EssayController.java +++ b/luoo_music/src/main/java/com/luoo/music/controller/EssayController.java @@ -3,6 +3,7 @@ package com.luoo.music.controller; import api.PageResult; import api.Result; +import com.luoo.music.dto.response.EssayRespDTO; import com.luoo.music.pojo.Essay; import com.luoo.music.service.EssayService; import io.swagger.annotations.Api; @@ -11,6 +12,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; +import java.util.List; +import java.util.stream.Collectors; + /** * 专栏文章前台功能 * author: wangqing @@ -34,7 +38,10 @@ public class EssayController { }else{ pageData = essayService.getByCategory(category, page, size); } - return Result.success(new PageResult(pageData.getTotalElements(),pageData.getContent())); + + List essayRespDTOS = pageData.stream().map(essay -> { + EssayRespDTO essayRespDTO = essayService.getJournalRespDTOWithComment(essay);return essayRespDTO;}).collect(Collectors.toList()); + return Result.success(new PageResult(pageData.getTotalElements(),essayRespDTOS)); } @ApiOperation("根据ID获取专栏文章") diff --git a/luoo_music/src/main/java/com/luoo/music/dto/response/EssayRespDTO.java b/luoo_music/src/main/java/com/luoo/music/dto/response/EssayRespDTO.java new file mode 100644 index 0000000..9990285 --- /dev/null +++ b/luoo_music/src/main/java/com/luoo/music/dto/response/EssayRespDTO.java @@ -0,0 +1,44 @@ +package com.luoo.music.dto.response; + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.format.annotation.DateTimeFormat; + +import javax.persistence.Id; +import java.time.LocalDateTime; + +@Data +public class EssayRespDTO { + + private String id; + // 标题 + private String title; + // 简介 + private String summary; + // 阅读数 + private Integer readCount; + // 内容 + private String content; + // 作者 + private String authorId; + private String authorName; + private String authorAvatar; + // 分类 + private Integer category; + // 封面路径 + private String cover; + + @CreatedDate + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime createTime; + + @LastModifiedDate + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime updateTime; + + private Integer totalCommentReplyCount; +} diff --git a/luoo_music/src/main/java/com/luoo/music/service/EssayService.java b/luoo_music/src/main/java/com/luoo/music/service/EssayService.java index 41d0204..71f2e5a 100644 --- a/luoo_music/src/main/java/com/luoo/music/service/EssayService.java +++ b/luoo_music/src/main/java/com/luoo/music/service/EssayService.java @@ -1,11 +1,21 @@ package com.luoo.music.service; +import com.luoo.music.client.UserClient; +import com.luoo.music.dao.CommentDao; import com.luoo.music.dao.EssayDao; +import com.luoo.music.dao.TagDao; +import com.luoo.music.dto.mapper.CommentMapper; +import com.luoo.music.dto.response.CommentDTO; +import com.luoo.music.dto.response.EssayRespDTO; import com.luoo.music.pojo.Essay; +import net.oschina.j2cache.CacheChannel; +import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import util.IdWorker; @@ -19,6 +29,22 @@ public class EssayService { @Autowired private IdWorker idWorker; + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private CacheChannel cacheChannel; + + @Autowired + private MongoTemplate mongoTemplate; + + @Autowired + private CommentDao commentDao; + + + + @Autowired + private UserClient userClient; public Page search(int page, int size) { // TODO: implement search logic @@ -28,6 +54,7 @@ public class EssayService { public void add(Essay essay) { essay.setId(idWorker.nextId()+""); + essay.setReadCount(0); essayDao.save(essay); } @@ -48,4 +75,14 @@ public class EssayService { Pageable pageable = PageRequest.of(page-1, size); return essayDao.findAllByCategoryOrderByCreateTimeDesc(category, pageable); } + + public EssayRespDTO getJournalRespDTOWithComment(Essay essay) { + CommentDTO commentDTO = CommentMapper.getCommentDTO(essay.getId(), mongoTemplate, commentDao, + redisTemplate,userClient,cacheChannel); + + EssayRespDTO essayRespDTO = new EssayRespDTO(); + BeanUtils.copyProperties(essay, essayRespDTO); + essayRespDTO.setTotalCommentReplyCount(commentDTO.getTotalCommentReplyInt()); + return essayRespDTO; + } }