diff --git a/src/main/java/com/hadam/hadam/controller/DiaryController.java b/src/main/java/com/hadam/hadam/controller/DiaryController.java index e11d458..9e7d5e0 100644 --- a/src/main/java/com/hadam/hadam/controller/DiaryController.java +++ b/src/main/java/com/hadam/hadam/controller/DiaryController.java @@ -60,4 +60,10 @@ public ResponseEntity> detailDiary(@PathVariable Long diaryId){ .body(BaseResponse.of(SuccessCode.OK, diaryService.getDetailDiary(diaryId))); } + @GetMapping("/search") + public ResponseEntity> searchDiary(@RequestParam String keyword, @RequestParam Long memberId, @RequestParam int year, @RequestParam int month){ + return ResponseEntity.status(HttpStatus.OK) + .body(BaseResponse.of(SuccessCode.OK, diaryService.getSearchDiary(keyword, memberId, year, month))); + } + } diff --git a/src/main/java/com/hadam/hadam/service/DiaryService.java b/src/main/java/com/hadam/hadam/service/DiaryService.java index d0ca11e..b7deb50 100644 --- a/src/main/java/com/hadam/hadam/service/DiaryService.java +++ b/src/main/java/com/hadam/hadam/service/DiaryService.java @@ -141,5 +141,24 @@ public DiaryDetailRes getDetailDiary(Long diaryId){ ); } + @Transactional(readOnly = true) + public List getSearchDiary(String keyword, Long memberId, int year, int month){ + + List diaries = diaryRepository.findDiariesByMemberIdAndYearMonth( + memberId, + year, + month + ); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d"); + return diaries.stream() + .filter(diary -> diary.getTitle().contains(keyword)) + .map(diary -> new MonthlyListReq( + diary.getId(), + diary.getImg(), + truncateContent(diary.getTitle()), // truncateContent 메서드 사용 + diary.getDate().format(formatter) + )) + .collect(Collectors.toList()); + } }