From 820855dc2c483623c87a40655a2fc9649f7028c7 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Sun, 28 Jul 2024 08:48:32 +0800 Subject: [PATCH] =?UTF-8?q?release:=E4=B8=93=E6=A0=8F=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AF=84=E8=AE=BA=E6=95=B0=E9=87=8F=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../music/controller/EssayController.java | 9 +++- .../luoo/music/dto/response/EssayRespDTO.java | 44 +++++++++++++++++++ .../com/luoo/music/service/EssayService.java | 37 ++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 luoo_music/src/main/java/com/luoo/music/dto/response/EssayRespDTO.java 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; + } }