diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java index 508dc16d..f94a16b1 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/controller/TacticController.java @@ -53,6 +53,17 @@ public ApiResponse> getUserTactic( return ApiResponse.onSuccess(userTacticLists); } + @Operation(summary = "제목으로 전술 검색", description = "제목으로 전술을 검색합니다. 단, 공개(anonymous가 false) 인 전술만 검색가능. 페이징 적용, 생성 날짜 기준 내림차순 정렬.") + @GetMapping("/search") + public ApiResponse> searchTitle( + @RequestParam(value ="title",required = false) String title, + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "10") int size) { + Pageable pageable = PageRequest.of(page, size, Sort.by(Sort.Direction.DESC, "createdAt")); + Page tacticLists = tacticQueryService.searchByTitle(title, pageable); + return ApiResponse.onSuccess(tacticLists); + } + @Operation(summary = "전술 생성", description = "전술을 생성합니다. 프로필이 생성이 된 후에 작성 가능.") @PostMapping public ApiResponse createTactic(@Valid @RequestBody TacticCreateRequest request, @LoginUser User user){ diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/repository/TacticRepository.java b/src/main/java/com/capstone/BnagFer/domain/tactic/repository/TacticRepository.java index 1337307b..4c40cce8 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/repository/TacticRepository.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/repository/TacticRepository.java @@ -16,4 +16,5 @@ public interface TacticRepository extends JpaRepository { Page findAllByAnonymousFalse(Pageable pageable); Page findAllByUser(User user, Pageable pageable); + Page findByTacticNameContainingAndAnonymousFalse(String title, Pageable pageable); } diff --git a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java index 10442b45..b3027e9b 100644 --- a/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java +++ b/src/main/java/com/capstone/BnagFer/domain/tactic/service/TacticQueryService.java @@ -51,4 +51,11 @@ public TacticDetailResponse getTacticById(Long tacticId) { return TacticDetailResponse.from(tactic, likeCount, commentCount); } + + public Page searchByTitle(String title, Pageable pageable) { + if (title == null) title = ""; + + Page byTitleContaining = tacticRepository.findByTacticNameContainingAndAnonymousFalse(title, pageable); + return byTitleContaining.map(TacticResponse.TacticList::from); + } }