diff --git a/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java b/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java index 1f31183..29fbb5d 100644 --- a/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java +++ b/src/main/java/com/potatocake/everymoment/dto/request/DiaryFilterRequest.java @@ -36,4 +36,13 @@ public List getCategories() { : Collections.emptyList(); } + public boolean hasFilter() { + return (keyword != null && !keyword.isEmpty()) || + (emoji != null && !emoji.isEmpty()) || + (category != null && !category.isEmpty()) || + date != null || + from != null || + until != null || + isBookmark != null; + } } diff --git a/src/main/java/com/potatocake/everymoment/service/DiaryService.java b/src/main/java/com/potatocake/everymoment/service/DiaryService.java index d16bdd3..253f4a0 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/DiaryService.java @@ -23,6 +23,7 @@ import com.potatocake.everymoment.repository.DiaryRepository; import com.potatocake.everymoment.repository.FileRepository; import com.potatocake.everymoment.repository.MemberRepository; +import java.time.LocalDate; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @@ -134,15 +135,33 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil List categories = diaryFilterRequest.getCategories(); List emojis = diaryFilterRequest.getEmojis(); - Specification spec = DiarySpecification.filterDiaries( - diaryFilterRequest.getKeyword(), - emojis, - categories, - diaryFilterRequest.getDate(), - diaryFilterRequest.getFrom(), - diaryFilterRequest.getUntil(), - diaryFilterRequest.getIsBookmark()) - .and((root, query, builder) -> builder.equal(root.get("member"), currentMember)); + Specification spec; + + if(!diaryFilterRequest.hasFilter()){ + LocalDate today = LocalDate.now(); + + spec = DiarySpecification.filterDiaries( + diaryFilterRequest.getKeyword(), + emojis, + categories, + today, + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil(), + diaryFilterRequest.getIsBookmark()) + .and((root, query, builder) -> builder.equal(root.get("member"), currentMember)); + } + + else{ + spec = DiarySpecification.filterDiaries( + diaryFilterRequest.getKeyword(), + emojis, + categories, + diaryFilterRequest.getDate(), + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil(), + diaryFilterRequest.getIsBookmark()) + .and((root, query, builder) -> builder.equal(root.get("member"), currentMember)); + } diaryPage = diaryRepository.findAll(spec, PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); diff --git a/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java b/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java index 5ea0708..96e8e9d 100644 --- a/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java +++ b/src/main/java/com/potatocake/everymoment/service/DiarySpecification.java @@ -25,6 +25,7 @@ public static Specification filterDiaries( if (keyword != null) { predicate = builder.and(predicate, builder.like(root.get("content"), "%" + keyword + "%")); } + if (emojis != null && !emojis.isEmpty()) { predicate = builder.and(predicate, root.get("emoji").in(emojis)); } @@ -35,13 +36,15 @@ public static Specification filterDiaries( predicate = builder.and(predicate, categoryJoin.get("categoryName").in(categories)); } - LocalDate filterDate = (date != null) ? date : LocalDate.now(); - predicate = builder.and(predicate, builder.equal(root.get("createAt").as(LocalDate.class), filterDate)); + if(date != null){ + predicate = builder.and(predicate, builder.equal(root.get("createAt").as(LocalDate.class), date)); + } if (from != null && until != null) { predicate = builder.and(predicate, - builder.between(root.get("createAt"), from, until)); + builder.between(root.get("createAt"), from, until.plusDays(1))); } + if (isBookmark != null) { predicate = builder.and(predicate, builder.equal(root.get("isBookmark"), isBookmark)); }