Explorar el Código

fix: 修复无法正常获取评论bug

ChenSiTong hace 6 años
padre
commit
92f40e32fc

+ 0 - 2
src/main/java/nju/seec/helper/HelperApplication.java

@@ -2,14 +2,12 @@ package nju.seec.helper;
 
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.context.properties.ConfigurationPropertiesScan;
 import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 
 /**
  * @author cst
  */
 @SpringBootApplication
-@ConfigurationPropertiesScan(basePackages = "nju.seec.helper.config")
 @EnableJpaRepositories(basePackages = "nju.seec.helper.data.dao")
 public class HelperApplication {
 

+ 11 - 1
src/main/java/nju/seec/helper/data/dao/CommentDAO.java

@@ -7,6 +7,8 @@ import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;
 
+import java.util.List;
+
 /**
  * @author cst
  */
@@ -29,5 +31,13 @@ public interface CommentDAO extends JpaRepository<Comment, Integer> {
      * @param pageable
      * @return
      */
-    Page<Comment> findByItemId(Integer itemId, Pageable pageable);
+    Page<Comment> findByItemIdAndFatherCommentIsNull(Integer itemId, Pageable pageable);
+
+    /**
+     * 子贴
+     *
+     * @param comment
+     * @return
+     */
+    List<Comment> findByFatherCommentOrderByPublishTimeAsc(Comment comment);
 }

+ 1 - 6
src/main/java/nju/seec/helper/data/entity/Comment.java

@@ -6,8 +6,6 @@ import org.hibernate.annotations.CreationTimestamp;
 
 import javax.persistence.*;
 import java.time.LocalDateTime;
-import java.util.Collections;
-import java.util.Set;
 
 /**
  * @author cst
@@ -26,7 +24,7 @@ public class Comment {
     @Column(name = "user_id")
     private Integer userId;
 
-    @Column(name = "user_name")
+    @Column(name = "username")
     private String username;
 
     @Enumerated(EnumType.STRING)
@@ -39,9 +37,6 @@ public class Comment {
     @CreationTimestamp
     private LocalDateTime publishTime;
 
-    @OneToMany(cascade = CascadeType.ALL, mappedBy = "fatherComment")
-    private Set<Comment> followComments = Collections.emptySet();
-
     @ManyToOne
     @JoinColumn(name = "father_comment_id")
     private Comment fatherComment;

+ 15 - 6
src/main/java/nju/seec/helper/logic/service/impl/CommentServiceImpl.java

@@ -10,7 +10,6 @@ import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
 import org.springframework.stereotype.Service;
 
-import java.util.Collections;
 import java.util.List;
 import java.util.stream.Collectors;
 
@@ -33,9 +32,9 @@ public class CommentServiceImpl implements CommentService {
         comment.setUserType(userType);
         comment.setItemId(commentDTO.getItemId());
         comment.setContent(commentDTO.getContent());
-        comment.setFollowComments(Collections.emptySet());
 
         Integer fatherCommentId = commentDTO.getFatherCommentId();
+
         if (fatherCommentId != null && fatherCommentId > 0) {
             Comment fatherComment = commentDAO.findCommentById(fatherCommentId);
             comment.setFatherComment(fatherComment);
@@ -46,14 +45,24 @@ public class CommentServiceImpl implements CommentService {
 
     @Override
     public List<CommentVO> getComments(Integer itemId, Pageable pageable) {
-        Page<Comment> commentPage = commentDAO.findByItemId(itemId, pageable);
-        return commentPage
-                .getContent()
+        Page<Comment> commentPage = commentDAO.findByItemIdAndFatherCommentIsNull(itemId, pageable);
+        List<Comment> comments = commentPage.getContent();
+        return comments
                 .stream()
-                .map(CommentVO::new)
+                .map(this::getCommentVO)
                 .collect(Collectors.toList());
     }
 
+    private CommentVO getCommentVO(Comment comment) {
+        List<Comment> childComments = commentDAO.findByFatherCommentOrderByPublishTimeAsc(comment);
+        return new CommentVO(
+                comment, childComments
+                .stream()
+                .map(this::getCommentVO)
+                .collect(Collectors.toList())
+        );
+    }
+
     @Override
     public void deleteComment(Integer commentId) {
         commentDAO.deleteById(commentId);

+ 15 - 3
src/main/java/nju/seec/helper/logic/vo/CommentVO.java

@@ -5,7 +5,8 @@ import nju.seec.helper.data.entity.Comment;
 import nju.seec.helper.util.enums.UserType;
 
 import java.time.LocalDateTime;
-import java.util.Set;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * @author cst
@@ -26,7 +27,7 @@ public class CommentVO {
 
     private LocalDateTime publishTime;
 
-    private Set<Comment> followComments;
+    private List<CommentVO> followComments;
 
     public CommentVO(Comment comment) {
         this.id = comment.getId();
@@ -36,6 +37,17 @@ public class CommentVO {
         this.userType = comment.getUserType();
         this.content = comment.getContent();
         this.publishTime = comment.getPublishTime();
-        this.followComments = comment.getFollowComments();
+        this.followComments = Collections.emptyList();
+    }
+
+    public CommentVO(Comment comment, List<CommentVO> comments) {
+        this.id = comment.getId();
+        this.itemId = comment.getItemId();
+        this.userId = comment.getUserId();
+        this.username = comment.getUsername();
+        this.userType = comment.getUserType();
+        this.content = comment.getContent();
+        this.publishTime = comment.getPublishTime();
+        this.followComments = comments;
     }
 }