Skip to content

Commit

Permalink
✨feature: 게시판 검색 기능 구현 (#167) (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
600gramSik authored Aug 3, 2024
1 parent 3b1425b commit 4e6b641
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
Expand Down Expand Up @@ -167,4 +168,16 @@ public ApiResponse<List<GetMyBoardBlockResponseDto>> getBlockedUsers(@LoginUser
return ApiResponse.onSuccess(blockedUsers);
}

@Operation(summary = "제목으로 게시판 검색", description = "제목으로 게시판을 검색. 페이징 적용, 생성 날짜 기준 내림차순 정렬.")
@GetMapping("/search")
public ApiResponse<Page<BoardListDto>> searchTitle(
@RequestParam(value ="title",required = false) String title,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "10") int size,
@LoginUser User user) {
Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt"));
Page<BoardListDto> boardLists = boardQueryService.searchByTitle(title, user, pageable);
return ApiResponse.onSuccess(boardLists);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface BoardRepository extends JpaRepository<Board, Long> {

Page<Board> findByUserId(Long userId, Pageable pageable);

Page<Board> findByBoardTitleContaining(String title, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public BoardDetailResponseDto getBoard(User user, Long boardId) {
}

Long commentCount = redisUtil.boardGetCommentCount(boardId);
if(commentCount == null){
if (commentCount == null) {
commentCount = (long) board.getComments().size();
redisUtil.boardSaveCommentCount(boardId, commentCount);
}
Expand All @@ -97,9 +97,20 @@ public Page<Board> getBoardsByUserId(Long userId, Pageable pageable) {
return boardRepository.findByUserId(userId, pageable);
}

//자유게시판 검색 기능
public Page<BoardListDto> searchByTitle(String title, User user, Pageable pageable) {
if (title == null) title = "";
Page<Board> byTitleContaining = boardRepository.findByBoardTitleContaining(title, pageable);
// 사용자가 차단한 다른 사용자들의 ID 목록을 가져온다
List<Long> blockedUserIds = boardBlockRepository.findIsBlockUserIdsByBlockUserId(user.getId());
// 차단한 사용자의 게시글이 제외된 리스트 생성
List<BoardListDto> filteredBoards = byTitleContaining.getContent().stream()
.filter(board -> !blockedUserIds.contains(board.getUser().getId()) || board.getUser().getId().equals(user.getId()))
.map(BoardListDto::from)
.toList();
return new PageImpl<>(filteredBoards, pageable, byTitleContaining.getTotalElements());
}
public boolean isUserBlocked(User blockUser, User isBlockedUser) {
return boardBlockRepository.existsByBlockUserAndIsBlockedUser(blockUser, isBlockedUser);
}


}

0 comments on commit 4e6b641

Please sign in to comment.