Skip to content

Commit

Permalink
Merge pull request #24 from Kusitms-28th-Hicardi-C/fix/#11/블로그_전체조회
Browse files Browse the repository at this point in the history
[fix]블로그 전체조회, 카테고리 검색, 키워드 검색
  • Loading branch information
emilywin825 authored Sep 16, 2023
2 parents 4c23320 + dd94b05 commit 22035ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,36 @@ public class BlogController {

private final BlogService blogService;

//전체조회
@GetMapping("/list")
public ResponseEntity<?> blogMainPage(@Validated @RequestParam BlogSearch search) {
List<BlogListResponseDTO> all = blogService.findAll(search);
public ResponseEntity<?> blogMainPage() {
List<BlogListResponseDTO> all = blogService.findAll();

return ResponseEntity
.ok()
.body(all);
}

//키워드 검색
@GetMapping("/keyword")
public ResponseEntity<?> blogSearchWithKeyword(@Validated @RequestParam String keyword) {
List<BlogListResponseDTO> searchAll = blogService.findAllByKeyword(keyword);

return ResponseEntity
.ok()
.body(searchAll);
}

//카테고리 검색
@GetMapping("/category")
public ResponseEntity<?> blogSearchWithCategory(@Validated @RequestParam String category) {
List<BlogListResponseDTO> searchAll = blogService.findAllByCategory(category);

return ResponseEntity
.ok()
.body(searchAll);
}

//상세 조회
@GetMapping("/{blogId}")
public ResponseEntity<?> blogDetail(@PathVariable("blogId") Long blogId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,24 +22,26 @@ public class BlogService {
private final BlogRepository blogRepository;

//전체 조회
public List<BlogListResponseDTO> 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<BlogListResponseDTO> findAll() {
return blogRepository.findAll().stream()
.map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList());
}

//카테고리 검색
public List<BlogListResponseDTO> findAllByCategory(String category) {
return blogRepository.findByCategoryContaining(category).stream()
.map(blog -> new BlogListResponseDTO(blog)).collect(Collectors.toList());
}

//키워드 검색
public List<BlogListResponseDTO> 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);
}

}

0 comments on commit 22035ed

Please sign in to comment.