Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 전체 게시글 검색 기능 구현 #317

Merged
merged 6 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/docs/asciidoc/comment.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
== 게시판 댓글
== 댓글

=== 댓글 목록 조회
operation::comment/find-comments[snippets='http-request,http-response,cookie,request-parameters,response-fields']
Expand Down
5 changes: 4 additions & 1 deletion src/docs/asciidoc/post.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
== 익명 게시판
== 게시글

=== 전체 게시글 목록 조회(Offset)
operation::post/find-all-posts-by-offset[snippets='http-request,http-response,cookie,request-parameters,response-fields']
Expand All @@ -15,6 +15,9 @@ operation::post/find-post-anonymity-false[snippets='http-request,http-response,c
=== 게시글 상세 보기(익명)
operation::post/find-post-anonymity-true[snippets='http-request,http-response,cookie,path-parameters,response-fields']

=== 전체 게시글 검색(Offset)
operation::post/search-all-posts-by-offset[snippets='http-request,http-response,cookie,request-parameters,response-fields']

=== 게시글 검색(Cursor)
operation::post/search-posts-by-cursor[snippets='http-request,http-response,cookie,request-parameters,response-fields']

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.ssafy.ssafsound.domain.board.repository;

import java.util.List;

import com.ssafy.ssafsound.domain.board.domain.Board;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
List<Board> findAllByUsedBoardTrue();
List<Board> findAllByUsedBoardTrue();

Optional<Board> findByIdAndUsedBoardTrue(Long id);

Boolean existsByIdAndUsedBoardTrue(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ public EnvelopeResponse<GetPostOffsetResDto> findMyScrapPostsByOffset(@Valid @Mo
.build();
}

@GetMapping("/all/search/offset")
public EnvelopeResponse<GetPostOffsetResDto> searchAllPostsByOffset(@Valid @ModelAttribute GetPostAllSearchOffsetReqDto getPostAllSearchOffsetReqDto) {
return EnvelopeResponse.<GetPostOffsetResDto>builder()
.data(postService.searchAllPostsByOffset(getPostAllSearchOffsetReqDto))
.build();
}

@GetMapping("/search/cursor")
public EnvelopeResponse<GetPostCursorResDto> searchPostsByCursor(@Valid @ModelAttribute GetPostSearchCursorReqDto getPostSearchCursorReqDto) {
return EnvelopeResponse.<GetPostCursorResDto>builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.ssafy.ssafsound.domain.post.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class GetPostAllSearchOffsetReqDto extends BasePageRequest {
@Size(min = 2)
@NotBlank
private String keyword;

public void setKeyword(String keyword) {
this.keyword = removeBlank(keyword);
}

private String removeBlank(String value) {
return value.replaceAll(" ", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ public interface PostRepository extends JpaRepository<Post, Long>, PostCustomRep
@EntityGraph(attributePaths = {"board", "member", "likes"})
@Query(value = "select p from post p " +
"where p.board = :board ")
Page<Post> findPostsByboardAndPageable(@Param("board") Board board, Pageable pageable);
Page<Post> findPostsByBoardAndPageable(@Param("board") Board board, Pageable pageable);

@EntityGraph(attributePaths = {"board", "member", "likes"})
@Query("select p from post p " +
"where (replace(p.title, ' ', '') like CONCAT('%', :keyword, '%') " +
"or replace(p.content, ' ', '') like CONCAT('%', :keyword, '%')) ")
Page<Post> searchAllPostsByBoardAndKeywordAndPageable(String keyword, PageRequest pageRequest);

@EntityGraph(attributePaths = {"board", "member", "likes"})
@Query("select p from post p " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -48,7 +47,7 @@ public GetPostCursorResDto findPostsByCursor(GetPostCursorReqDto getPostCursorRe
Long cursor = getPostCursorReqDto.getCursor();
int size = getPostCursorReqDto.getSize();

if (!boardRepository.existsById(boardId)) {
if (!boardRepository.existsByIdAndUsedBoardTrue(boardId)) {
throw new BoardException(BoardErrorInfo.NO_BOARD);
}

Expand All @@ -62,10 +61,10 @@ public GetPostOffsetResDto findPostsByOffset(GetPostOffsetReqDto getPostOffsetRe

Long boardId = getPostOffsetReqDto.getBoardId();

Board board = boardRepository.findById(boardId)
Board board = boardRepository.findByIdAndUsedBoardTrue(boardId)
.orElseThrow(() -> new BoardException(BoardErrorInfo.NO_BOARD));

Page<Post> posts = postRepository.findPostsByboardAndPageable(board, pageRequest);
Page<Post> posts = postRepository.findPostsByBoardAndPageable(board, pageRequest);
return GetPostOffsetResDto.ofPosts(posts);
}

Expand Down Expand Up @@ -322,7 +321,7 @@ public GetPostCursorResDto searchPostsByCursor(GetPostSearchCursorReqDto getPost
Long cursor = getPostSearchCursorReqDto.getCursor();
int size = getPostSearchCursorReqDto.getSize();

if (!boardRepository.existsById(boardId)) {
if (!boardRepository.existsByIdAndUsedBoardTrue(boardId)) {
throw new BoardException(BoardErrorInfo.NO_BOARD);
}

Expand All @@ -337,7 +336,7 @@ public GetPostOffsetResDto searchPostsByOffset(GetPostSearchOffsetReqDto getPost
Long boardId = getPostSearchOffsetReqDto.getBoardId();
String keyword = getPostSearchOffsetReqDto.getKeyword();

Board board = boardRepository.findById(boardId)
Board board = boardRepository.findByIdAndUsedBoardTrue(boardId)
.orElseThrow(() -> new BoardException(BoardErrorInfo.NO_BOARD));

Page<Post> posts = postRepository.searchPostsByBoardAndKeywordAndPageable(board, keyword, pageRequest);
Expand Down Expand Up @@ -371,4 +370,13 @@ public GetPostOffsetResDto findAllPostsByOffset(BasePageRequest basePageRequest)
return GetPostOffsetResDto.ofPosts(posts);

}

@Transactional(readOnly = true)
public GetPostOffsetResDto searchAllPostsByOffset(GetPostAllSearchOffsetReqDto getPostAllSearchOffsetReqDto) {
PageRequest pageRequest = getPostAllSearchOffsetReqDto.toPageRequest();
String keyword = getPostAllSearchOffsetReqDto.getKeyword();

Page<Post> posts = postRepository.searchAllPostsByBoardAndKeywordAndPageable(keyword, pageRequest);
return GetPostOffsetResDto.ofPosts(posts);
}
}
Loading
Loading