From 640cc5f51fbd1f1f89a49deeadd6eff2d3f47d45 Mon Sep 17 00:00:00 2001 From: wangqing <408564126@qq.com> Date: Tue, 6 Feb 2024 12:52:46 +0800 Subject: [PATCH] feat:comment new/hot list --- .../comment/controller/CommentController.java | 119 ++++++++++++++++-- .../java/com/luoo/comment/dao/CommentDao.java | 2 + .../java/com/luoo/comment/pojo/Comment.java | 2 + .../com/luoo/comment/pojo/CommentResp.java | 48 +++++++ .../luoo/comment/service/CommentService.java | 12 ++ .../com/luoo/comment/service/LikeService.java | 2 +- 6 files changed, 172 insertions(+), 13 deletions(-) create mode 100644 luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java diff --git a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java index 6d8e03e..5c3a33a 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java +++ b/luoo_comment/src/main/java/com/luoo/comment/controller/CommentController.java @@ -21,11 +21,18 @@ import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.core.query.Criteria; +import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; @RestController @CrossOrigin @@ -45,6 +52,9 @@ public class CommentController extends BaseController { @Autowired private RedisTemplate redisTemplate; + @Autowired + private MongoTemplate mongoTemplate; + @Autowired private HttpServletRequest request; @@ -64,17 +74,81 @@ public class CommentController extends BaseController { * ,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级 * @return */ - @ApiOperation(value = "根据期刊ID获取分布评论列表",notes = "根据期刊ID获取根节点评论分页列表 ,数据库中保存的有期刊号的都是根节点,期刊号为空的代表有父评论且parentID一定有值,拿到当然根节点评论列表,每个根评论有 commentCount字段,页面可以显示有 280个回复,然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口,分页拿到该评论下的子评论列表,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级") + @ApiOperation(value = "根据期刊ID获取分页评论列表(废除),用最新和热门两个接口,可以默认用热门展示",notes = "根据期刊ID获取根节点评论分页列表 ,数据库中保存的有期刊号的都是根节点,期刊号为空的代表有父评论且parentID一定有值,拿到当然根节点评论列表,每个根评论有 commentCount字段,页面可以显示有 280个回复,然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口,分页拿到该评论下的子评论列表,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级") @ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true), @ApiImplicitParam(name = "page", value = "分页: 页码,以1开始", required = true), @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) @GetMapping("/{journalId}/{page}/{size}") - public Result findAllByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size) { + public Result findAllByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + Page pageList = commentService.findByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + String userId = userLoginDto.getUserId(); + Page pageList = commentService.findByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } - Page pageData = commentService.findByJournalId(journalId,page,size); - return Result.success(new PageResult(pageData.getTotalElements(),pageData.getContent())); + + @ApiOperation(value = "根据期刊ID获取分页评论列表(热门),按点赞数倒序",notes = "根据期刊ID获取根节点评论分页列表 ,数据库中保存的有期刊号的都是根节点,期刊号为空的代表有父评论且parentID一定有值,拿到当然根节点评论列表,每个根评论有 commentCount字段,页面可以显示有 280个回复,然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口,分页拿到该评论下的子评论列表,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级") + @ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true), + @ApiImplicitParam(name = "page", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) + @GetMapping("/hot/{journalId}/{page}/{size}") + public Result findHotByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + Page pageList = commentService.findByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + String userId = userLoginDto.getUserId(); + Page pageList = commentService.findHotByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + + @ApiOperation(value = "根据期刊ID获取分页评论列表(最新),按时间倒序",notes = "根据期刊ID获取根节点评论分页列表 ,数据库中保存的有期刊号的都是根节点,期刊号为空的代表有父评论且parentID一定有值,拿到当然根节点评论列表,每个根评论有 commentCount字段,页面可以显示有 280个回复,然后根据 @GetMapping(\"/comment/{parentId}/{page}/{size}\") 接口,分页拿到该评论下的子评论列表,页面在请求添加评论的时候,当给评论添加子评论的时候,父评论parentID有值,就无法添加子评论并且不显示回复2字,控制到两级") + @ApiImplicitParams({@ApiImplicitParam(name = "journalId", value = "剘刊id", required = true), + @ApiImplicitParam(name = "page", value = "分页: 页码,以1开始", required = true), + @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) + @GetMapping("/new/{journalId}/{page}/{size}") + public Result findNewByJournalId(@PathVariable String journalId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + Page pageList = commentService.findByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + String userId = userLoginDto.getUserId(); + Page pageList = commentService.findNewByJournalId(journalId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); } + private CommentResp getCommentResp(Comment comment,String userId) { + + Criteria criteria = Criteria.where("userId").is(userId) + .and("type").is(2) + .and("likedItemId").is(comment.get_id()); + // 创建查询对象并应用查询条件 + Query query = new Query(criteria); + boolean isExists = mongoTemplate.exists(query, PublicationLike.class); + comment.setHaveThumbup(isExists); //是否已点赞 + CommentResp commentResp = new CommentResp(); + BeanUtils.copyProperties(comment,commentResp); + commentResp.setThumbupCountString(comment.getThumbupCount()+""); + if(comment.getThumbupCount()>999) { + commentResp.setThumbupCountString("999+"); + } + return commentResp; + } @GetMapping("/{page}/{size}") public Result search(@PathVariable int page,@PathVariable int size){ @@ -118,7 +192,17 @@ public class CommentController extends BaseController { } @DeleteMapping("/{commentId}") - public Result delete(@PathVariable String commentId) { + public Result delete(@PathVariable String commentId,@RequestHeader(value = "Authorization", required = true) String authorization) { + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + return Result.unauthorized(null); + } + String userId = userLoginDto.getUserId(); + Comment comment = commentService.findById(commentId); + if(!userId.equals(comment.get_id())) { + return Result.failed("您只能删除自己发表的评论"); + } commentService.deleteById(commentId); return Result.success(); } @@ -130,11 +214,19 @@ public class CommentController extends BaseController { @ApiImplicitParam(name = "page", value = "分页: 页码,以1开始", required = true), @ApiImplicitParam(name = "size", value = "分页: 每页数量", required = true)}) @GetMapping("/comment/{parentId}/{page}/{size}") - public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size) { - - Page pageData = commentService.findByParentId(parentId,page,size); + public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) { - return Result.success(new PageResult(pageData.getTotalElements(),pageData.getContent())); + //验证是否登录,并且拿到ID + UserLoginDto userLoginDto = getUserLoginDto(authorization); + if (null == userLoginDto) { + Page pageList = commentService.findByParentId(parentId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); + } + String userId = userLoginDto.getUserId(); + Page pageList = commentService.findByParentId(parentId,page,size); + List list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList()); + return Result.success(new PageResult(Long.valueOf(list.size()),list)); } @@ -159,15 +251,18 @@ public class CommentController extends BaseController { if (flag==0) { //当前用户已经点过赞,执行取消点赞 likeService.unlikePublication(commentId,userId,2); - return Result.success(null,"取消点赞成功"); + Map map = new HashMap(); + map.put("thumbState",0); + return Result.success(map,"取消点赞成功"); } commentService.thumbup(commentId,userLoginDto); - + Map map = new HashMap(); + map.put("thumbState",1); // redisTemplate.opsForValue().set("thumbup_"+commentId+"_"+userId,1,10, TimeUnit.SECONDS); - return Result.success(null,"点赞成功"); + return Result.success(map,"点赞成功"); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java index 0843c0d..874144a 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java +++ b/luoo_comment/src/main/java/com/luoo/comment/dao/CommentDao.java @@ -9,6 +9,8 @@ public interface CommentDao extends MongoRepository { public Page findByParentId(String parentId, Pageable pageable); public Page findByJournalId(String journalId, Pageable pageable); + public Page findByJournalIdOrderByPublishTimeDesc(String journalId, Pageable pageable); + public Page findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable); public Page findAllByOrderByPublishTimeDesc(Pageable pageable); } diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java index 98c28ca..3d71ce4 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/Comment.java @@ -45,4 +45,6 @@ public class Comment implements Serializable { private String parentId; private String journalId; + + private boolean haveThumbup; } diff --git a/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java new file mode 100644 index 0000000..abc85eb --- /dev/null +++ b/luoo_comment/src/main/java/com/luoo/comment/pojo/CommentResp.java @@ -0,0 +1,48 @@ +package com.luoo.comment.pojo; + + +import com.fasterxml.jackson.annotation.JsonFormat; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +@Data +public class CommentResp implements Serializable { + + private String _id; + // 评论内容 + private String content; + // 发布时间 + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private Date publishTime; + + private String userId; + + private String nickName; + + private String avatar; + + // 点赞数 + private String thumbupCountString; + // 转发数 +// private Integer share; + // 评论数量 + private Integer commentCount; + + // 状态 + private Integer state; + + + private String location; //归属地 + /** + * 父节点 ID + */ + private String parentId; + + private String journalId; + + private boolean haveThumbup; +} diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java index 77138c7..208db47 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/CommentService.java @@ -105,6 +105,18 @@ public class CommentService { return commentDao.findByJournalId(journalId,pageable); } + public Page findHotByJournalId(String journalId, int page, int size) { + + Pageable pageable = PageRequest.of(page-1,size); + return commentDao.findByJournalIdOrderByThumbupCountDesc(journalId,pageable); + } + + public Page findNewByJournalId(String journalId, int page, int size) { + + Pageable pageable = PageRequest.of(page-1,size); + return commentDao.findByJournalIdOrderByPublishTimeDesc(journalId,pageable); + } + public void thumbup(String commentId,UserLoginDto userLoginDto) { Query query = new Query(); diff --git a/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java b/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java index 9c6c1aa..ab1055c 100644 --- a/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java +++ b/luoo_comment/src/main/java/com/luoo/comment/service/LikeService.java @@ -58,7 +58,7 @@ public class LikeService { Query query1 = new Query(); - query.addCriteria(Criteria.where("_id").is(publicationId)); + query1.addCriteria(Criteria.where("_id").is(publicationId)); Update update = new Update(); update.inc("thumbupCount",-1); mongoTemplate.updateFirst(query1,update,"comment");