Skip to content

Commit

Permalink
Merge pull request #22 from Ha-dam/feat/21-searchDiary
Browse files Browse the repository at this point in the history
feat: diary title search API
  • Loading branch information
ziiyouth authored Dec 12, 2023
2 parents 6e1dbfb + 4dac19c commit 08e9076
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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());
}

}

0 comments on commit 08e9076

Please sign in to comment.