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: diary title search API #22

Merged
merged 1 commit into from
Dec 12, 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
6 changes: 6 additions & 0 deletions src/main/java/com/hadam/hadam/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,10 @@ public ResponseEntity<BaseResponse<?>> detailDiary(@PathVariable Long diaryId){
.body(BaseResponse.of(SuccessCode.OK, diaryService.getDetailDiary(diaryId)));
}

@GetMapping("/search")
public ResponseEntity<BaseResponse<?>> 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)));
}

}
19 changes: 19 additions & 0 deletions src/main/java/com/hadam/hadam/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,24 @@ public DiaryDetailRes getDetailDiary(Long diaryId){
);
}

@Transactional(readOnly = true)
public List<MonthlyListReq> getSearchDiary(String keyword, Long memberId, int year, int month){

List<Diary> 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());
}

}