diff --git a/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java b/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java index e1b15c4..5f7077a 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/common/error/CommonErrorAdvice.java @@ -21,4 +21,11 @@ public ResponseEntity errorHandler(IllegalArgumentException e) { new ErrorResponse(e.getMessage(), 400) ); } + + @ExceptionHandler(value = {NullPointerException.class}) + public ResponseEntity errorHandler(NullPointerException e){ + return ResponseEntity.badRequest().body( + new ErrorResponse(e.getMessage(), 400) + ); + } } diff --git a/src/main/java/com/example/marumaru_sparta_verspring/controller/PostController.java b/src/main/java/com/example/marumaru_sparta_verspring/controller/PostController.java index b263c85..23dff85 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/controller/PostController.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/controller/PostController.java @@ -40,6 +40,15 @@ public List getPostList(){ return postList; } + + //검색 리스트 + @GetMapping("/posts/search") + public List getSearchList(@RequestParam("category") String category, @RequestParam("keyword") String keyword){ + List searchPostList = postService.getPostSearchList(category, keyword); + + return searchPostList.stream().map(post -> modelMapper.map(post, PostResponseDto.class)).collect(Collectors.toList()); + } + //조회 @GetMapping("/posts/detail") @@ -122,4 +131,6 @@ public boolean checkUserLike(@RequestParam Long id, @AuthenticationPrincipal Use Long userId = userDetails.getUser().getId(); return postService.checkUserLike(id, userId); } + + } diff --git a/src/main/java/com/example/marumaru_sparta_verspring/repository/PostRepository.java b/src/main/java/com/example/marumaru_sparta_verspring/repository/PostRepository.java index 6e89afb..d5ace19 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/repository/PostRepository.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/repository/PostRepository.java @@ -1,6 +1,7 @@ package com.example.marumaru_sparta_verspring.repository; import com.example.marumaru_sparta_verspring.domain.articles.Post; +import com.example.marumaru_sparta_verspring.domain.user.User; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; @@ -8,4 +9,7 @@ public interface PostRepository extends JpaRepository { List findByUserId(Long userId); + List findAllByUser(User user); + List findAllByTitleContainingIgnoreCase(String keyword); + List findAllByContentContainingIgnoreCase(String keyword); } diff --git a/src/main/java/com/example/marumaru_sparta_verspring/service/PostService.java b/src/main/java/com/example/marumaru_sparta_verspring/service/PostService.java index ed20c34..cabec96 100644 --- a/src/main/java/com/example/marumaru_sparta_verspring/service/PostService.java +++ b/src/main/java/com/example/marumaru_sparta_verspring/service/PostService.java @@ -102,6 +102,7 @@ public String deletePost(Long id, Long userID){ } } + //게시글 리스트 public List getPostList(){ List postList = postrepository.findAll(); @@ -112,6 +113,24 @@ public List getPostList(){ return resultList; } + //게시글 검색 + public List getPostSearchList(String category, String keyword){ + if(category.equals("user")){ + User user = userRepository.findByUsername(keyword).orElseThrow( + () -> new NullPointerException("해당 아이디가 존재하지 않습니다.") + ); + + return postrepository.findAllByUser(user); + } + else if(category.equals("title")){ + return postrepository.findAllByTitleContainingIgnoreCase(keyword); + } + else if(category.equals("content")){ + return postrepository.findAllByContentContainingIgnoreCase(keyword); + } + return postrepository.findAll(); + } + @Transactional public void createComment(@RequestBody PostCommentRequsetDto postCommentRequsetDto, Long userId){ User user = userRepository.findById(userId).orElseThrow( diff --git a/src/main/resources/static/js/articles/post_list.js b/src/main/resources/static/js/articles/post_list.js index bfcb2bf..19dd00b 100644 --- a/src/main/resources/static/js/articles/post_list.js +++ b/src/main/resources/static/js/articles/post_list.js @@ -1,30 +1,23 @@ $(document).ready(function () { + $('#post-body').empty(); show_post_list(); }); -function show_post_list() { - $.ajax({ - type: 'GET', - url: '/post-list', - contentType: 'application/json; charset=utf-8', - data: {}, - success: function (response) { - const articles = response; - let list_num = 0 - if(articles.length>0) { - show_best(articles[0]); - } - for (let i = 1; i < articles.length; i++) { - const username = articles[i]['user']['nickname'] - const title = articles[i]['title'] - const number = articles[i]['idx'] - const contents = articles[i]['content'] - const time = formatDate(articles[i]['createdAt']) - const view = articles[i]['view'] - const card_img = articles[i]['img'] - list_num+=1 +function display(articles){ + let list_num = 0 + + for (let i = 0; i < articles.length; i++) { + const username = articles[i]['user']['nickname'] + const title = articles[i]['title'] + const number = articles[i]['idx'] + const contents = articles[i]['content'] + const time = formatDate(articles[i]['createdAt']) + const view = articles[i]['view'] + const card_img = articles[i]['img'] - let temp_html = ` + list_num+=1 + + let temp_html = `
pic @@ -41,8 +34,23 @@ function show_post_list() {
` - $('#post-body').append(temp_html) + $('#post-body').append(temp_html) + } +} + +function show_post_list() { + $.ajax({ + type: 'GET', + url: '/post-list', + contentType: 'application/json; charset=utf-8', + data: {}, + success: function (response) { + const articles = response; + if(articles.length>0) { + show_best(articles[0]); + articles.shift(); //1번 요소 삭제 } + display(articles); } }); } @@ -81,4 +89,26 @@ function show_best(best) { ` $('#post-body').append(temp_html) +} + +function postSearch(){ + let category = $('#SearchSelect').val(); + let keyword = $('#post-search-keyword').val(); + $.ajax({ + type: 'GET', + url: '/posts/search', + data: { + "category" : category, + "keyword" : keyword + }, + success: function (response) { + $('#post-body').empty(); + console.log(response) + display(response) + }, + error: function (request, status, error) { + console.log(error); + alert(request.responseJSON.message); + } + }) } \ No newline at end of file diff --git a/src/main/resources/templates/articles/post_list.html b/src/main/resources/templates/articles/post_list.html index 358a48c..3804fd3 100644 --- a/src/main/resources/templates/articles/post_list.html +++ b/src/main/resources/templates/articles/post_list.html @@ -105,7 +105,23 @@
오늘도 재미있는 소식들이 한가득
+ + +
+