Forráskód Böngészése

feat: 取得获取自己评论接口

ChenSiTong 6 éve
szülő
commit
5e512c9890

+ 16 - 16
pom.xml

@@ -154,22 +154,22 @@
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
             </plugin>
-            <plugin>
-                <groupId>com.github.apiggs</groupId>
-                <artifactId>apiggs-maven-plugin</artifactId>
-                <version>1.6</version>
-                <executions>
-                    <execution>
-                        <phase>compile</phase>
-                        <goals>
-                            <goal>apiggs</goal>
-                        </goals>
-                    </execution>
-                </executions>
-                <configuration>
-                    <!-- options in there -->
-                </configuration>
-            </plugin>
+<!--            <plugin>-->
+<!--                <groupId>com.github.apiggs</groupId>-->
+<!--                <artifactId>apiggs-maven-plugin</artifactId>-->
+<!--                <version>1.6</version>-->
+<!--                <executions>-->
+<!--                    <execution>-->
+<!--                        <phase>compile</phase>-->
+<!--                        <goals>-->
+<!--                            <goal>apiggs</goal>-->
+<!--                        </goals>-->
+<!--                    </execution>-->
+<!--                </executions>-->
+<!--                <configuration>-->
+<!--                    &lt;!&ndash; options in there &ndash;&gt;-->
+<!--                </configuration>-->
+<!--            </plugin>-->
         </plugins>
     </build>
 

+ 8 - 0
src/main/java/nju/seec/helper/controller/CommentController.java

@@ -119,4 +119,12 @@ public class CommentController {
                                   @NotNull(message = "缺少展示状态") Boolean show) {
         commentService.modifyCommentShow(user, commentId, show);
     }
+
+    @Auth(roles = {UserRole.TEACHER, UserRole.STUDENT}, message = "获取自己的评论")
+    @GetMapping("/self")
+    public PageResponse<CommentVO> getSelfComments(LoginUser user,
+                                                   @RequestParam(required = false, defaultValue = "") String key,
+                                                   @PageableDefault(Integer.MAX_VALUE) Pageable pageable) {
+        return PageResponse.of(commentService.getSelfComments(user, key, pageable));
+    }
 }

+ 21 - 0
src/main/java/nju/seec/helper/dao/CommentDAO.java

@@ -4,8 +4,10 @@ import nju.seec.helper.entity.Comment;
 import nju.seec.helper.entity.User;
 import nju.seec.helper.enums.ExceptionType;
 import nju.seec.helper.exception.HelperException;
+import nju.seec.helper.util.StringUtils;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.Pageable;
+import org.springframework.data.jpa.domain.Specification;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
 import org.springframework.stereotype.Repository;
@@ -70,4 +72,23 @@ public interface CommentDAO extends JpaRepository<Comment, Long>, JpaSpecificati
      * @return
      */
     Page<Comment> findBySlideIdAndPageNumberAndUserAndShow(Long slideId, Integer pageNumber, User user, Boolean show, Pageable pageable);
+
+    /**
+     * 根据用户ID、关键字检索
+     *
+     * @param userId
+     * @param key
+     * @param pageable
+     * @return
+     */
+    default Page<Comment> findByUserIdAndKey(Long userId, String key, Pageable pageable) {
+        String keyPattern = StringUtils.keyPattern(key);
+
+        return this.findAll(
+                (Specification<Comment>) (root, query, cb) -> cb.and(
+                        cb.equal(root.get("user").get("id"), userId),
+                        cb.or(cb.like(root.get("title"), keyPattern), cb.like(root.get("content"), keyPattern))
+                )
+                , pageable);
+    }
 }

+ 7 - 7
src/main/java/nju/seec/helper/dao/MessageDAO.java

@@ -50,11 +50,11 @@ public interface MessageDAO extends JpaRepository<Message, Long> {
      */
     Page<Message> findByToUserIdAndReadAndContentContains(Long toUserId, Boolean read, String key, Pageable pageable);
 
-    /**
-     * 基于关联数据删除
-     *
-     * @param refId
-     * @param messageType
-     */
-    void deleteByRefIdAndType(Long refId, MessageType messageType);
+//    /**
+//     * 基于关联数据删除
+//     *
+//     * @param refId
+//     * @param messageType
+//     */
+//    void deleteByRefIdAndType(Long refId, MessageType messageType);
 }

+ 32 - 32
src/main/java/nju/seec/helper/dao/UserDAO.java

@@ -30,38 +30,38 @@ public interface UserDAO extends JpaRepository<User, Long>, JpaSpecificationExec
     }
 
 //    /**
-////     * 检查邮箱是否已存在
-////     *
-////     * @param email
-////     * @return
-////     */
-////    boolean existsByEmail(String email);
-////
-////
-////    /**
-////     * 检查手机号是否已存在
-////     *
-////     * @param phone
-////     * @return
-////     */
-////    boolean existsByPhone(String phone);
-////
-////    /**
-////     * 邮箱密码匹配
-////     *
-////     * @param email
-////     * @param password
-////     * @return
-////     */
-////    Optional<User> findByEmailAndPassword(String email, String password);
-////
-////    /**
-////     * 根据邮箱查询
-////     *
-////     * @param email
-////     * @return
-////     */
-////    Optional<User> findByEmail(String email);
+//     * 检查邮箱是否已存在
+//     *
+//     * @param email
+//     * @return
+//     */
+//    boolean existsByEmail(String email);
+//
+//
+//    /**
+//     * 检查手机号是否已存在
+//     *
+//     * @param phone
+//     * @return
+//     */
+//    boolean existsByPhone(String phone);
+//
+//    /**
+//     * 邮箱密码匹配
+//     *
+//     * @param email
+//     * @param password
+//     * @return
+//     */
+//    Optional<User> findByEmailAndPassword(String email, String password);
+//
+//    /**
+//     * 根据邮箱查询
+//     *
+//     * @param email
+//     * @return
+//     */
+//    Optional<User> findByEmail(String email);
 
     /**
      * 根据手机号查询

+ 3 - 0
src/main/java/nju/seec/helper/dto/comment/CommentDTO.java

@@ -23,4 +23,7 @@ public class CommentDTO implements Serializable {
     @NotBlank(message = "内容不能为空")
     @Length(max = 1000, message = "内容长度不能超过1000")
     private String content;
+
+    @NotNull(message = "缺少展示状态")
+    private Boolean show = true;
 }

+ 11 - 1
src/main/java/nju/seec/helper/service/CommentService.java

@@ -64,7 +64,7 @@ public interface CommentService extends AuthService<Comment> {
     CommentVO getOneComment(LoginUser user, Long commentId);
 
     /**
-     * 取得自己的评论
+     * 取得自己的评论(基于slide和pageNumber)
      *
      * @param user
      * @param slideId
@@ -83,4 +83,14 @@ public interface CommentService extends AuthService<Comment> {
      * @param show
      */
     void modifyCommentShow(LoginUser user, Long commentId, Boolean show);
+
+    /**
+     * 取得自己的评论
+     *
+     * @param user
+     * @param key
+     * @param pageable
+     * @return
+     */
+    Page<CommentVO> getSelfComments(LoginUser user, String key, Pageable pageable);
 }

+ 7 - 0
src/main/java/nju/seec/helper/service/impl/CommentServiceImpl.java

@@ -52,6 +52,7 @@ public class CommentServiceImpl implements CommentService {
                         .setUser(userDAO.findUserById(user.getId()))
                         .setTitle(commentDTO.getTitle())
                         .setContent(commentDTO.getContent())
+                        .setShow(commentDTO.getShow())
         );
 
         return new CommentVO(comment);
@@ -146,6 +147,12 @@ public class CommentServiceImpl implements CommentService {
         commentDAO.save(comment.setShow(show));
     }
 
+    @Transactional(readOnly = true)
+    @Override
+    public Page<CommentVO> getSelfComments(LoginUser user, String key, Pageable pageable) {
+        return commentDAO.findByUserIdAndKey(user.getId(), key, pageable).map(CommentVO::new);
+    }
+
     private void unTopComment(Comment comment) {
         Integer topNumber = comment.getTopNumber();
         comment.setTopNumber(0);

+ 1 - 1
src/main/java/nju/seec/helper/vo/CommentVO.java

@@ -22,7 +22,7 @@ public class CommentVO {
      */
     private Integer pageNumber;
     /**
-     * 置顶优先级(越大说明越高,为null说明不是置顶评论)
+     * 置顶优先级
      */
     private Integer topNumber;
     private String title;