Skip to content

Commit

Permalink
feat: 친구 다이어리 조회 변경
Browse files Browse the repository at this point in the history
- 전체 일자 최신순 조회로 변경
  • Loading branch information
HyeJiJUN committed Nov 4, 2024
1 parent 916ad1c commit ee36e37
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -56,18 +58,17 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest
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) -> root.get("member").get("id").in(friendIdList));
Specification<Diary> 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<FriendDiarySimpleResponse> friendDiarySimpleResponseList = diaryPage.getContent().stream()
.map(this::convertToFriendDiariesResponseDTO)
Expand Down Expand Up @@ -151,5 +152,4 @@ private FriendDiarySimpleResponse convertToFriendDiariesResponseDTO(Diary savedD
.createAt(savedDiary.getCreateAt())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -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<Diary> filterDiaries(
String keyword, List<String> emojis, List<String> categories,
LocalDate date, LocalDate from, LocalDate until) {
return (Root<Diary> 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<Diary, DiaryCategory> diaryCategoryJoin = root.join("diaryCategories", JoinType.LEFT);
Join<DiaryCategory, Category> 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;
};
}
}

0 comments on commit ee36e37

Please sign in to comment.