From dd94b05311f9cbdfacacbe23938f42b6b702a275 Mon Sep 17 00:00:00 2001 From: emilywin825 Date: Sat, 16 Sep 2023 21:32:46 +0900 Subject: [PATCH] =?UTF-8?q?[fix]=EB=B8=94=EB=A1=9C=EA=B7=B8=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=EC=A1=B0=ED=9A=8C,=20=EC=B9=B4=ED=85=8C=EA=B3=A0?= =?UTF-8?q?=EB=A6=AC=20=EA=B2=80=EC=83=89,=20=ED=82=A4=EC=9B=8C=EB=93=9C?= =?UTF-8?q?=20=EA=B2=80=EC=83=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blog/controller/BlogController.java | 25 +++++++++++++++-- .../domain/blog/service/BlogService.java | 27 ++++++++++--------- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/hicardi/domain/blog/controller/BlogController.java b/src/main/java/com/example/hicardi/domain/blog/controller/BlogController.java index 675fcbd..155a914 100644 --- a/src/main/java/com/example/hicardi/domain/blog/controller/BlogController.java +++ b/src/main/java/com/example/hicardi/domain/blog/controller/BlogController.java @@ -19,15 +19,36 @@ public class BlogController { private final BlogService blogService; + //전체조회 @GetMapping("/list") - public ResponseEntity blogMainPage(@Validated @RequestParam BlogSearch search) { - List all = blogService.findAll(search); + public ResponseEntity blogMainPage() { + List all = blogService.findAll(); return ResponseEntity .ok() .body(all); } + //키워드 검색 + @GetMapping("/keyword") + public ResponseEntity blogSearchWithKeyword(@Validated @RequestParam String keyword) { + List searchAll = blogService.findAllByKeyword(keyword); + + return ResponseEntity + .ok() + .body(searchAll); + } + + //카테고리 검색 + @GetMapping("/category") + public ResponseEntity blogSearchWithCategory(@Validated @RequestParam String category) { + List searchAll = blogService.findAllByCategory(category); + + return ResponseEntity + .ok() + .body(searchAll); + } + //상세 조회 @GetMapping("/{blogId}") public ResponseEntity blogDetail(@PathVariable("blogId") Long blogId diff --git a/src/main/java/com/example/hicardi/domain/blog/service/BlogService.java b/src/main/java/com/example/hicardi/domain/blog/service/BlogService.java index 26b3426..5a8c58c 100644 --- a/src/main/java/com/example/hicardi/domain/blog/service/BlogService.java +++ b/src/main/java/com/example/hicardi/domain/blog/service/BlogService.java @@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RequestParam; import java.util.List; import java.util.stream.Collectors; @@ -21,24 +22,26 @@ public class BlogService { private final BlogRepository blogRepository; //전체 조회 - public List findAll(BlogSearch search) { - //카테고리 검색 - if(search.getCategory()!=""&&search.getKeyword()==""){ - return blogRepository.findByCategoryContaining(search.getCategory()).stream() - .map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList()); - } - //검색어로 검색 - else if(search.getCategory()==""&&search.getKeyword()!=""){ - return blogRepository.findByKeywordInTitleOrContent(search.getKeyword()).stream() - .map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList()); - } - //키워드, 검색어 없이 전체 조회 할 시 + public List findAll() { return blogRepository.findAll().stream() .map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList()); } + //카테고리 검색 + public List findAllByCategory(String category) { + return blogRepository.findByCategoryContaining(category).stream() + .map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList()); + } + + //키워드 검색 + public List findAllByKeyword(String keyword) { + return blogRepository.findByKeywordInTitleOrContent(keyword).stream() + .map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList()); + } + public BlogListResponseDTO findById(Long id) { Blog blog = blogRepository.findById(id).orElseThrow(() -> new IllegalArgumentException("게시글 존재x. id=" + id)); return new BlogListResponseDTO(blog); } + }