diff --git a/server/src/main/java/com/project/Glog/controller/PostController.java b/server/src/main/java/com/project/Glog/controller/PostController.java index 10fc522..f8daf09 100644 --- a/server/src/main/java/com/project/Glog/controller/PostController.java +++ b/server/src/main/java/com/project/Glog/controller/PostController.java @@ -154,6 +154,16 @@ public ResponseEntity searchPost(@RequestParam(defaultValue = " return new ResponseEntity<>(postPreviewDtos, HttpStatus.OK); } + @GetMapping("/search/category") + public ResponseEntity searchContentsByCategory(@RequestParam Long categoryId) { + //content 내용을 포함한 게시글의 리스트를 생성한다. + PostPreviewDtos postPreviewDtos = postService.searchPostsByCategory(categoryId); + + return new ResponseEntity<>(postPreviewDtos, HttpStatus.OK); + } + + + @PatchMapping("/post/like") public ResponseEntity plusLike(@CurrentUser UserPrincipal userPrincipal, @RequestParam Long postId) { diff --git a/server/src/main/java/com/project/Glog/repository/PostRepository.java b/server/src/main/java/com/project/Glog/repository/PostRepository.java index b35ace7..479d28d 100644 --- a/server/src/main/java/com/project/Glog/repository/PostRepository.java +++ b/server/src/main/java/com/project/Glog/repository/PostRepository.java @@ -1,5 +1,6 @@ package com.project.Glog.repository; +import com.project.Glog.domain.Category; import com.project.Glog.domain.Post; import com.project.Glog.domain.User; import org.springframework.data.domain.Page; @@ -34,6 +35,7 @@ public interface PostRepository extends JpaRepository { List findAllByTitleContaining(String title); List findAllByUser(User user); + List findAllByCategory(Category category); List findAllByContentContaining(String contnt); diff --git a/server/src/main/java/com/project/Glog/repository/PrPostRepository.java b/server/src/main/java/com/project/Glog/repository/PrPostRepository.java index e41184a..5adb22f 100644 --- a/server/src/main/java/com/project/Glog/repository/PrPostRepository.java +++ b/server/src/main/java/com/project/Glog/repository/PrPostRepository.java @@ -24,7 +24,6 @@ public interface PrPostRepository extends JpaRepository { @Query("SELECT pr FROM PrPost pr WHERE pr.id=:prId") Optional findPrByPrId( @Param("prId") Long prId); - @Query("SELECT p.category FROM PrPost p WHERE p.category.id=:PrId") - Category findByPrId(@Param("PrId") Long PrId); + } diff --git a/server/src/main/java/com/project/Glog/service/PostService.java b/server/src/main/java/com/project/Glog/service/PostService.java index 858a9aa..d7ac58c 100644 --- a/server/src/main/java/com/project/Glog/service/PostService.java +++ b/server/src/main/java/com/project/Glog/service/PostService.java @@ -74,7 +74,7 @@ public Post create(UserPrincipal userPrincipal, MultipartFile multipartFile, Pos if (req.getPrId() != null) { PrPost prPost = prPostRepository.findPrByPrId(req.getPrId()).get(); - Category ct = prPostRepository.findByPrId(req.getPrId()); + Category ct = prPost.getCategory(); post = req.toPost(user, ct, blog); post.setPrPost(prPost); post.setIsPr(true); @@ -312,6 +312,13 @@ public String clickLike(UserPrincipal userPrincipal, Long postId) { } } + public PostPreviewDtos searchPostsByCategory(Long categoryId) { + Category category = categoryRepository.findByCategoryId(categoryId); + List posts = postRepository.findAllByCategory(category); + + return new PostPreviewDtos(posts, posts.size()); + } + private void setPostHashtag(Post post, List hashtagList) { for (String hashtag : hashtagList) { PostHashtag postHashtag = new PostHashtag();