From 0179c61218eba264d9d62bdafe3f703391c41979 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 17 Feb 2024 17:51:51 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[feat]=20#21=20title=20=EA=B0=92=EC=9D=84?= =?UTF-8?q?=20=ED=8F=AC=ED=95=A8=ED=95=98=EB=8A=94=20=EA=B0=92=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EC=BF=BC=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/harang/server/repository/PostRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/org/harang/server/repository/PostRepository.java b/src/main/java/org/harang/server/repository/PostRepository.java index bd4919c..fcb5104 100644 --- a/src/main/java/org/harang/server/repository/PostRepository.java +++ b/src/main/java/org/harang/server/repository/PostRepository.java @@ -1,12 +1,16 @@ package org.harang.server.repository; +import java.util.List; import org.harang.server.domain.Post; import org.springframework.data.jpa.repository.JpaRepository; import org.harang.server.dto.type.ErrorMessage; import org.harang.server.exception.CustomException; +import org.springframework.data.jpa.repository.Query; public interface PostRepository extends JpaRepository { default Post findByIdOrThrow(Long id) { return findById(id).orElseThrow(() -> new CustomException(ErrorMessage.POST_NOT_FOUND)); } + + List findByTitleIsContaining(String title); } From 0fd95a81179c5a8cd115a05831ee64c6ec73e024 Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sat, 17 Feb 2024 17:52:17 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[feat]=20#21=20=EA=B2=8C=EC=8B=9C=EA=B8=80?= =?UTF-8?q?=20=EA=B2=80=EC=83=89=20=EA=B2=B0=EA=B3=BC=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/harang/server/controller/PostController.java | 7 +++++++ .../java/org/harang/server/service/PostService.java | 10 ++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/harang/server/controller/PostController.java b/src/main/java/org/harang/server/controller/PostController.java index 0c021d2..99dd598 100644 --- a/src/main/java/org/harang/server/controller/PostController.java +++ b/src/main/java/org/harang/server/controller/PostController.java @@ -10,6 +10,7 @@ import org.harang.server.dto.response.PostResponse; import org.harang.server.dto.type.SuccessMessage; import org.harang.server.service.PostService; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -34,4 +35,10 @@ public ApiResponse createPost(@MemberId Long memberId, @Valid @RequestBody Po public ApiResponse> getAllPosts() { return ApiResponse.success(postService.getAllPosts()); } + + @Transactional(readOnly = true) + @GetMapping("/search") + public ApiResponse getSearchResults(@RequestParam(name = "title") String title) { + return ApiResponse.success(postService.getSearchResults(title)); + } } diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 46e0900..19c6ab1 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -56,9 +56,9 @@ public Post createPost(Long memberId, PostRequest request) { .build() ); - for (String categoryName: request.categoryList()) { + for (String categoryName : request.categoryList()) { Category category = categoryRepository.findByName(categoryName); - if(category != null) { + if (category != null) { PostCategory savedPostCategory = PostCategory.builder() .post(savedPost) @@ -77,4 +77,10 @@ public List getAllPosts() { .collect(Collectors.toList()); } + public List getSearchResults(String title) { + return postRepository.findByTitleIsContaining(title) + .stream() + .map(p -> PostResponse.of(p)) + .toList(); + } } From 0c5ff77824ca8e72e12f4d477d895eb5d4092fcd Mon Sep 17 00:00:00 2001 From: nykoh2001 Date: Sun, 3 Mar 2024 09:20:43 +0900 Subject: [PATCH 3/3] [fix] #21 add right brace --- src/main/java/org/harang/server/service/PostService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/harang/server/service/PostService.java b/src/main/java/org/harang/server/service/PostService.java index 023a694..4f898c4 100644 --- a/src/main/java/org/harang/server/service/PostService.java +++ b/src/main/java/org/harang/server/service/PostService.java @@ -82,6 +82,7 @@ public List getSearchResults(String title) { .stream() .map(p -> PostResponse.of(p)) .toList(); + } @Transactional public void deletePost(Long memberId, Long postId) { @@ -89,6 +90,5 @@ public void deletePost(Long memberId, Long postId) { locationRepository.deleteByPostId(postId); postRepository.delete(post); - } }