diff --git a/src/docs/asciidoc/comment.adoc b/src/docs/asciidoc/comment.adoc index e60d023f..d2c81c53 100644 --- a/src/docs/asciidoc/comment.adoc +++ b/src/docs/asciidoc/comment.adoc @@ -1,4 +1,4 @@ -== 게시판 댓글 +== 댓글 === 댓글 목록 조회 operation::comment/find-comments[snippets='http-request,http-response,cookie,request-parameters,response-fields'] diff --git a/src/docs/asciidoc/post.adoc b/src/docs/asciidoc/post.adoc index 5feb0d4e..3d105b69 100644 --- a/src/docs/asciidoc/post.adoc +++ b/src/docs/asciidoc/post.adoc @@ -1,4 +1,4 @@ -== 익명 게시판 +== 게시글 === 전체 게시글 목록 조회(Offset) operation::post/find-all-posts-by-offset[snippets='http-request,http-response,cookie,request-parameters,response-fields'] @@ -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'] diff --git a/src/main/java/com/ssafy/ssafsound/domain/board/repository/BoardRepository.java b/src/main/java/com/ssafy/ssafsound/domain/board/repository/BoardRepository.java index f43d630f..48b75a0e 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/board/repository/BoardRepository.java +++ b/src/main/java/com/ssafy/ssafsound/domain/board/repository/BoardRepository.java @@ -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 { - List findAllByUsedBoardTrue(); + List findAllByUsedBoardTrue(); + + Optional findByIdAndUsedBoardTrue(Long id); + + Boolean existsByIdAndUsedBoardTrue(Long id); } diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/controller/PostController.java b/src/main/java/com/ssafy/ssafsound/domain/post/controller/PostController.java index d57dc29d..f8f0bf4e 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/controller/PostController.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/controller/PostController.java @@ -133,6 +133,13 @@ public EnvelopeResponse findMyScrapPostsByOffset(@Valid @Mo .build(); } + @GetMapping("/all/search/offset") + public EnvelopeResponse searchAllPostsByOffset(@Valid @ModelAttribute GetPostAllSearchOffsetReqDto getPostAllSearchOffsetReqDto) { + return EnvelopeResponse.builder() + .data(postService.searchAllPostsByOffset(getPostAllSearchOffsetReqDto)) + .build(); + } + @GetMapping("/search/cursor") public EnvelopeResponse searchPostsByCursor(@Valid @ModelAttribute GetPostSearchCursorReqDto getPostSearchCursorReqDto) { return EnvelopeResponse.builder() diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/dto/GetPostAllSearchOffsetReqDto.java b/src/main/java/com/ssafy/ssafsound/domain/post/dto/GetPostAllSearchOffsetReqDto.java new file mode 100644 index 00000000..2450f697 --- /dev/null +++ b/src/main/java/com/ssafy/ssafsound/domain/post/dto/GetPostAllSearchOffsetReqDto.java @@ -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(" ", ""); + } +} diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/repository/PostRepository.java b/src/main/java/com/ssafy/ssafsound/domain/post/repository/PostRepository.java index bc3585b2..a0872946 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/repository/PostRepository.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/repository/PostRepository.java @@ -33,7 +33,13 @@ public interface PostRepository extends JpaRepository, PostCustomRep @EntityGraph(attributePaths = {"board", "member", "likes"}) @Query(value = "select p from post p " + "where p.board = :board ") - Page findPostsByboardAndPageable(@Param("board") Board board, Pageable pageable); + Page 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 searchAllPostsByBoardAndKeywordAndPageable(String keyword, PageRequest pageRequest); @EntityGraph(attributePaths = {"board", "member", "likes"}) @Query("select p from post p " + diff --git a/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java b/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java index 0aa6ca49..aa719505 100644 --- a/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java +++ b/src/main/java/com/ssafy/ssafsound/domain/post/service/PostService.java @@ -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; @@ -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); } @@ -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 posts = postRepository.findPostsByboardAndPageable(board, pageRequest); + Page posts = postRepository.findPostsByBoardAndPageable(board, pageRequest); return GetPostOffsetResDto.ofPosts(posts); } @@ -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); } @@ -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 posts = postRepository.searchPostsByBoardAndKeywordAndPageable(board, keyword, pageRequest); @@ -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 posts = postRepository.searchAllPostsByBoardAndKeywordAndPageable(keyword, pageRequest); + return GetPostOffsetResDto.ofPosts(posts); + } } diff --git a/src/main/resources/static/docs/index.html b/src/main/resources/static/docs/index.html index af016051..2fae2e7b 100644 --- a/src/main/resources/static/docs/index.html +++ b/src/main/resources/static/docs/index.html @@ -451,31 +451,32 @@

SSAF-SOUND API Documentation

  • 1.1. 게시판 목록 조회
  • -
  • 2. 익명 게시판 +
  • 2. 게시글
  • -
  • 3. 게시판 댓글 +
  • 3. 댓글