From ee36e376794ae48e4944e1f919fa67152114f17e Mon Sep 17 00:00:00 2001 From: HyeJiJUN Date: Mon, 4 Nov 2024 14:28:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=B9=9C=EA=B5=AC=20=EB=8B=A4=EC=9D=B4?= =?UTF-8?q?=EC=96=B4=EB=A6=AC=20=EC=A1=B0=ED=9A=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 전체 일자 최신순 조회로 변경 --- .../service/FriendDiaryService.java | 22 ++++---- .../service/FriendDiarySpecification.java | 52 +++++++++++++++++++ 2 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/potatocake/everymoment/service/FriendDiarySpecification.java diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java index 2d6ad53..430ae61 100644 --- a/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiaryService.java @@ -20,11 +20,13 @@ import com.potatocake.everymoment.repository.FriendRepository; import com.potatocake.everymoment.repository.LikeRepository; import com.potatocake.everymoment.repository.MemberRepository; +import java.time.LocalDate; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -56,18 +58,17 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest 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) -> root.get("member").get("id").in(friendIdList)); + Specification spec = FriendDiarySpecification.filterDiaries( + diaryFilterRequest.getKeyword(), + emojis, + categories, + diaryFilterRequest.getDate(), + diaryFilterRequest.getFrom(), + diaryFilterRequest.getUntil()) + .and((root, query, builder) -> root.get("member").get("id").in(friendIdList)); diaryPage = diaryRepository.findAll(spec, - PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize())); + PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize(), Sort.by(Sort.Direction.DESC, "createAt"))); List friendDiarySimpleResponseList = diaryPage.getContent().stream() .map(this::convertToFriendDiariesResponseDTO) @@ -151,5 +152,4 @@ private FriendDiarySimpleResponse convertToFriendDiariesResponseDTO(Diary savedD .createAt(savedDiary.getCreateAt()) .build(); } - } diff --git a/src/main/java/com/potatocake/everymoment/service/FriendDiarySpecification.java b/src/main/java/com/potatocake/everymoment/service/FriendDiarySpecification.java new file mode 100644 index 0000000..34854d7 --- /dev/null +++ b/src/main/java/com/potatocake/everymoment/service/FriendDiarySpecification.java @@ -0,0 +1,52 @@ +package com.potatocake.everymoment.service; + +import com.potatocake.everymoment.entity.Category; +import com.potatocake.everymoment.entity.Diary; +import com.potatocake.everymoment.entity.DiaryCategory; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Join; +import jakarta.persistence.criteria.JoinType; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; +import java.time.LocalDate; +import java.util.List; +import org.springframework.data.jpa.domain.Specification; + +public class FriendDiarySpecification { + + public static Specification filterDiaries( + String keyword, List emojis, List categories, + LocalDate date, LocalDate from, LocalDate until) { + return (Root root, CriteriaQuery query, CriteriaBuilder builder) -> { + Predicate predicate = builder.conjunction(); + + 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)); + } + + if (categories != null && !categories.isEmpty()) { + Join diaryCategoryJoin = root.join("diaryCategories", JoinType.LEFT); + Join categoryJoin = diaryCategoryJoin.join("category", JoinType.LEFT); + predicate = builder.and(predicate, categoryJoin.get("categoryName").in(categories)); + } + + 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.plusDays(1))); + } + + predicate = builder.and(predicate, builder.isTrue(root.get("public"))); + + return predicate; + }; + } +}