Skip to content

Commit

Permalink
fix: 일기 필터링 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
HyeJiJUN committed Oct 28, 2024
1 parent 9d0384e commit 20a0e8f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,13 @@ public List<String> 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;
}
}
37 changes: 28 additions & 9 deletions src/main/java/com/potatocake/everymoment/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -134,15 +135,33 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil
List<String> categories = diaryFilterRequest.getCategories();
List<String> emojis = diaryFilterRequest.getEmojis();

Specification<Diary> 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<Diary> 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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static Specification<Diary> 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));
}
Expand All @@ -35,13 +36,15 @@ public static Specification<Diary> 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));
}
Expand Down

0 comments on commit 20a0e8f

Please sign in to comment.