1. 保存逻辑完善

2. 层级评论接口完善
评论模块逻辑补充提交
main
lyp 10 months ago
parent d195b78053
commit b71a1438d8

@ -34,6 +34,12 @@
<artifactId>spring-cloud-starter-config</artifactId> <artifactId>spring-cloud-starter-config</artifactId>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.16</version>
</dependency>
<dependency> <dependency>
<groupId>net.renfei</groupId> <groupId>net.renfei</groupId>
<artifactId>ip2location</artifactId> <artifactId>ip2location</artifactId>

@ -1,19 +1,20 @@
package com.luoo.comment.controller; package com.luoo.comment.controller;
import com.luoo.comment.pojo.Comment;
import com.luoo.comment.service.CommentService;
import api.PageResult; import api.PageResult;
import api.Result; import api.Result;
import api.StatusCode; import api.StatusCode;
import com.luoo.comment.pojo.Comment;
import java.util.List; import com.luoo.comment.pojo.CommentDto;
import com.luoo.comment.pojo.CommentVo;
import com.luoo.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController @RestController
@CrossOrigin @CrossOrigin
@RequestMapping("/comment") @RequestMapping("/comment")
@ -25,12 +26,6 @@ public class CommentController {
@Autowired @Autowired
private RedisTemplate redisTemplate; private RedisTemplate redisTemplate;
@GetMapping
public Result<List<Comment>> findAll(){
return Result.success(commentService.findAll());
}
@GetMapping("/findByVolid/{volid}") @GetMapping("/findByVolid/{volid}")
public Result<List<Comment>> findByVolid(@PathVariable String volid){ public Result<List<Comment>> findByVolid(@PathVariable String volid){
return Result.success(commentService.findByVolid(volid)); return Result.success(commentService.findByVolid(volid));
@ -43,18 +38,9 @@ public class CommentController {
} }
@PostMapping @PostMapping("/save")
public Result<Void> save(@RequestBody Comment comment){ public Result<Void> save(@RequestBody CommentDto commentDto){
commentService.save(comment); commentService.save(commentDto);
return Result.success();
}
@PutMapping("/{commentId}")
public Result<Void> update(@PathVariable String commentId,@RequestBody Comment comment){
comment.set_id(commentId);
commentService.update(comment);
return Result.success(); return Result.success();
} }
@ -63,14 +49,14 @@ public class CommentController {
public Result<Void> delete(@PathVariable String commentId){ public Result<Void> delete(@PathVariable String commentId){
commentService.deleteById(commentId); commentService.deleteById(commentId);
return Result.success(); return Result.success();
} }
@GetMapping("/{parentid}/{page}/{size}") @GetMapping("/findByParentId")
public Result<PageResult<Comment>> findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){ public Result<PageResult<CommentVo>> findByParentId(@RequestParam String parentId,
@RequestParam int page,
Page<Comment> pageData = commentService.findByParentId(parentid,page,size); @RequestParam int size){
return Result.success(new PageResult<Comment>(pageData.getTotalElements(),pageData.getContent())); Page<CommentVo> pageData = commentService.findByParentId(parentId,page,size);
return Result.success(new PageResult<>(pageData.getTotalElements(),pageData.getContent()));
} }
@PutMapping("/thumbup/{commentId}") @PutMapping("/thumbup/{commentId}")

@ -9,8 +9,8 @@ import java.util.List;
public interface CommentDao extends MongoRepository<Comment,String> { public interface CommentDao extends MongoRepository<Comment,String> {
public Page<Comment> findByParentid(String parentid, Pageable pageable); Page<Comment> findByParentId(String parentId, Pageable pageable);
public List<Comment> findByArticleid(String articleid); List<Comment> findByArticleId(String articleId);
} }

@ -0,0 +1,29 @@
package com.luoo.comment.enums;
public enum CommentStatusEnum {
/**
*
*/
NORMAL(1),
/**
*
*/
DELETE(2),
/**
*
*/
AUDITING(3);
private final int value;
CommentStatusEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}

@ -1,23 +1,24 @@
package com.luoo.comment.pojo; package com.luoo.comment.pojo;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@Data
public class Comment implements Serializable { public class Comment implements Serializable {
@Id @Id
private String _id; private String id;
// 评论内容 // 评论内容
private String content; private String content;
// 发布时间 // 发布时间
private Date publishtime; private Date publishTime;
private String userid;
// 昵称 private String userId;
private String nickname;
// 访问数
private Integer visits;
// 点赞数 // 点赞数
private Integer thumbup; private Integer thumbup;
// 转发数 // 转发数
@ -26,105 +27,30 @@ public class Comment implements Serializable {
private Integer comment; private Integer comment;
// 状态 // 状态
private String state; private Integer state;
private String parentid;
private String articleid;
public String get_id() {
return _id;
}
public void set_id(String _id) {
this._id = _id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPublishtime() { /**
return publishtime; * ID
} */
private String parentId;
public void setPublishtime(Date publishtime) { private String articleId;
this.publishtime = publishtime;
}
public String getUserid() { private Integer level;
return userid;
}
public void setUserid(String userid) { private String targetId;
this.userid = userid;
}
public String getNickname() { /**
return nickname; *
} */
private Integer type;
public void setNickname(String nickname) { private String city;
this.nickname = nickname;
}
public Integer getVisits() {
return visits;
}
public void setVisits(Integer visits) {
this.visits = visits;
}
public Integer getThumbup() { public CommentVo convertVo () {
return thumbup; CommentVo commentVo = new CommentVo();
BeanUtils.copyProperties(this, commentVo);
return commentVo;
} }
public void setThumbup(Integer thumbup) {
this.thumbup = thumbup;
}
public Integer getShare() {
return share;
}
public void setShare(Integer share) {
this.share = share;
}
public Integer getComment() {
return comment;
}
public void setComment(Integer comment) {
this.comment = comment;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
}
public String getArticleid() {
return articleid;
}
public void setArticleid(String articleid) {
this.articleid = articleid;
}
} }

@ -0,0 +1,37 @@
package com.luoo.comment.pojo;
import lombok.Data;
import java.io.Serializable;
@Data
public class CommentDto implements Serializable {
/**
* ID
*/
private String articleId;
/**
*
*/
private String content;
/**
* ID
*/
private String parentId;
/**
* ID
*/
private String targetId;
/**
* IP
*/
private String ipAddress;
private String userId;
}

@ -0,0 +1,78 @@
package com.luoo.comment.pojo;
import lombok.Data;
import java.util.Date;
@Data
public class CommentVo {
private String id;
/**
*
*/
private String content;
/**
*
*/
private Date publishTime;
/**
* ID
*/
private String userId;
/**
*
*/
private String nickName;
/**
*
*/
private Integer thumbup;
/**
*
*/
private Integer share;
/**
*
*/
private Integer comment;
/**
*
*/
private String state;
/**
* ID
*/
private String parentId;
/**
*
*/
private String articleId;
/**
*
*/
private Integer level;
/**
* ID
*/
private String targetId;
/**
*
*/
private CommentVo targetComment;
/**
*
*/
private String type;
/**
*
*/
private String city;
}

@ -1,87 +1,26 @@
package com.luoo.comment.service; package com.luoo.comment.service;
import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.pojo.Comment; import com.luoo.comment.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired; import com.luoo.comment.pojo.CommentDto;
import com.luoo.comment.pojo.CommentVo;
import org.springframework.data.domain.Page; 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.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.Date;
import java.util.List; import java.util.List;
@Service public interface CommentService {
@Transactional
public class CommentService {
@Autowired Comment findById(String id);
private CommentDao commentDao;
@Autowired void save(CommentDto commentDto);
private IdWorker idWorker;
@Autowired void update(Comment comment);
private MongoTemplate mongoTemplate;
public List<Comment> findAll() {
return commentDao.findAll();
}
public Comment findById(String id) {
return commentDao.findById(id).get();
}
public void save(Comment comment){
comment.set_id(idWorker.nextId()+"");
comment.setPublishtime(new Date());
comment.setVisits(0);
comment.setShare(0);
comment.setThumbup(0);
comment.setComment(0);
comment.setState("1");
// 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
if (comment.getParentid()!=null && !"".equals(comment.getParentid())) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(comment.getParentid()));
Update update = new Update();
update.inc("comment",1);
mongoTemplate.updateFirst(query,update,"spit");
}
commentDao.save(comment);
}
public void update(Comment comment) { void deleteById(String id);
commentDao.save(comment);
}
public void deleteById(String id) { Page<CommentVo> findByParentId(String parentId, int page, int size);
commentDao.deleteById(id);
}
void thumbup(String commentId);
public Page<Comment> findByParentId(String parentid,int page,int size){ List<Comment> findByVolid(String volid);
Pageable pageable = PageRequest.of(page-1,size);
return commentDao.findByParentid(parentid,pageable);
}
public void thumbup(String commentId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is("1"));
Update update = new Update();
update.inc("thumbup",1);
mongoTemplate.updateFirst(query,update,"spit");
}
public List<Comment> findByVolid(String volid) {
return commentDao.findByArticleid(volid);
}
} }

@ -0,0 +1,136 @@
package com.luoo.comment.service.impl;
import cn.hutool.core.util.ObjectUtil;
import com.luoo.comment.dao.CommentDao;
import com.luoo.comment.enums.CommentStatusEnum;
import com.luoo.comment.pojo.Comment;
import com.luoo.comment.pojo.CommentDto;
import com.luoo.comment.pojo.CommentVo;
import com.luoo.comment.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
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.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
@Service
@Transactional
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentDao commentDao;
@Autowired
private IdWorker idWorker;
@Autowired
private MongoTemplate mongoTemplate;
public Comment findById(String id) {
return commentDao.findById(id).get();
}
public void save(CommentDto commentDto){
Comment comment = new Comment();
comment.setContent(commentDto.getContent());
comment.setPublishTime(new Date());
comment.setUserId(commentDto.getUserId());
comment.setComment(0);
comment.setParentId(commentDto.getParentId());
comment.setArticleId(commentDto.getArticleId());
comment.setLevel(1);
comment.setTargetId(commentDto.getTargetId());
comment.setType(0);
comment.setCity("上海");
comment.setId(String.valueOf(idWorker.nextId()));
comment.setShare(0);
comment.setThumbup(0);
comment.setComment(0);
comment.setState(CommentStatusEnum.NORMAL.getValue());
// 如果当前添加的吐槽,有父节点,那么父节点的吐槽回复数要加一
if (ObjectUtil.isNotEmpty(comment.getParentId())) {
// 更新父节点的评论回复数
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(comment.getParentId()));
Update update = new Update();
update.inc("comment",1);
mongoTemplate.updateFirst(query,update,Comment.class);
}
if (ObjectUtil.isNotEmpty(comment.getTargetId())) {
Comment parentIdComment = mongoTemplate.findById(comment.getTargetId(), Comment.class);
comment.setLevel(parentIdComment.getLevel()+1);
Query query = new Query();
query.addCriteria(Criteria.where("_id").is(comment.getTargetId()));
Update update = new Update();
update.inc("comment",1);
mongoTemplate.updateFirst(query,update,Comment.class);
}
commentDao.save(comment);
}
public void update(Comment comment) {
commentDao.save(comment);
}
public void deleteById(String id) {
commentDao.deleteById(id);
}
public Page<CommentVo> findByParentId(String parentId,int page,int size){
Pageable pageable = PageRequest.of(page, size);
Query query = new Query();
Criteria criteria = Criteria.where("parentId").is(parentId);
query.addCriteria(criteria);
Sort sort = Sort.by(Sort.Direction.ASC, "age");
query.with(sort);
query.with(pageable);
List<Comment> comments = mongoTemplate.find(query, Comment.class);
List<CommentVo> commentVos = comments.stream().map(Comment::convertVo).collect(Collectors.toList());
List<String> targetComments = comments.stream().map(Comment::getTargetId).collect(Collectors.toList());
Map<String, CommentVo> idCommentMap = commentVos.stream().collect(Collectors.toMap(CommentVo::getId, Function.identity()));
long total = mongoTemplate.count(query, Comment.class);
// boolean isChange = targetComments.removeAll(idCommentMap.keySet());
for (CommentVo commentVo : commentVos) {
if (commentVo.getLevel() > 2 && ObjectUtil.isNotEmpty(commentVo.getTargetId())) {
CommentVo targetCommentVo = idCommentMap.get(commentVo.getTargetId());
if (targetCommentVo != null) {
commentVo.setTargetComment(targetCommentVo);
}
}
}
return new PageImpl<>(commentVos, pageable, total);
}
public void thumbup(String commentId) {
Query query = new Query();
query.addCriteria(Criteria.where("_id").is("1"));
Update update = new Update();
update.inc("thumbup",1);
mongoTemplate.updateFirst(query,update,"spit");
}
public List<Comment> findByVolid(String volid) {
return commentDao.findByArticleId(volid);
}
}
Loading…
Cancel
Save