feat:comment new/hot list

main
wangqing 10 months ago
parent 4368aa06c1
commit 640cc5f51f

@ -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 {
* ,parentID2
* @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<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
Page<Comment> pageData = commentService.findByJournalId(journalId,page,size);
return Result.success(new PageResult<Comment>(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<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findHotByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(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<Comment> pageList = commentService.findByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findNewByJournalId(journalId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(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) {
public Result findByParentId(@PathVariable String parentId,@PathVariable int page,@PathVariable int size, @RequestHeader(value = "Authorization", required = false) String authorization) {
Page<Comment> pageData = commentService.findByParentId(parentId,page,size);
return Result.success(new PageResult<Comment>(pageData.getTotalElements(),pageData.getContent()));
//验证是否登录并且拿到ID
UserLoginDto userLoginDto = getUserLoginDto(authorization);
if (null == userLoginDto) {
Page<Comment> pageList = commentService.findByParentId(parentId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,"")).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(Long.valueOf(list.size()),list));
}
String userId = userLoginDto.getUserId();
Page<Comment> pageList = commentService.findByParentId(parentId,page,size);
List<CommentResp> list =pageList.stream().map(x->getCommentResp(x,userId)).collect(Collectors.toList());
return Result.success(new PageResult<CommentResp>(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,"点赞成功");
}

@ -9,6 +9,8 @@ public interface CommentDao extends MongoRepository<Comment,String> {
public Page<Comment> findByParentId(String parentId, Pageable pageable);
public Page<Comment> findByJournalId(String journalId, Pageable pageable);
public Page<Comment> findByJournalIdOrderByPublishTimeDesc(String journalId, Pageable pageable);
public Page<Comment> findByJournalIdOrderByThumbupCountDesc(String journalId, Pageable pageable);
public Page<Comment> findAllByOrderByPublishTimeDesc(Pageable pageable);
}

@ -45,4 +45,6 @@ public class Comment implements Serializable {
private String parentId;
private String journalId;
private boolean haveThumbup;
}

@ -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;
}

@ -105,6 +105,18 @@ public class CommentService {
return commentDao.findByJournalId(journalId,pageable);
}
public Page<Comment> findHotByJournalId(String journalId, int page, int size) {
Pageable pageable = PageRequest.of(page-1,size);
return commentDao.findByJournalIdOrderByThumbupCountDesc(journalId,pageable);
}
public Page<Comment> 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();

@ -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");

Loading…
Cancel
Save