Skip to content

Commit

Permalink
fix: 카테곡리 필터 관련 에러 수정
Browse files Browse the repository at this point in the history
- 카테고리 이름을 받아야 하는데 ID를 받아 처리하던 부분 수정 및 JOIN 관련 수정
  • Loading branch information
peeerr committed Oct 16, 2024
1 parent 8dab3f2 commit 3cca37c
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public List<String> getEmojis() {
: Collections.emptyList();
}

public List<Long> getCategories() {
public List<String> getCategories() {
return (category != null && !category.isEmpty())
? Arrays.stream(category.split(","))
.map(Long::parseLong)
.map(String::trim)
.filter(s -> !s.isEmpty())
.collect(Collectors.toList())
: Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ public class DiaryManualCreateRequest {
private boolean isBookmark;
private boolean isPublic;
private String emoji;
private List<FileRequest> file;
private String content;
}

7 changes: 7 additions & 0 deletions src/main/java/com/potatocake/everymoment/entity/Diary.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.potatocake.everymoment.entity;

import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -9,6 +10,9 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Lob;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -53,6 +57,9 @@ public class Diary extends BaseTimeEntity {
@Builder.Default
private boolean isPublic = false;

@OneToMany(mappedBy = "diary", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<DiaryCategory> diaryCategories = new HashSet<>();

public void updateContent(String content) {
if (content != null) {
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,13 @@ public class DiaryCategory {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(foreignKey = @ForeignKey(name = "fk_category_id"), nullable = false)
private Category category;

public void setDiary(Diary diary) {
this.diary = diary;
}

public void setCategory(Category category) {
this.category = category;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public void createDiaryManual(Long memberId, DiaryManualCreateRequest diaryManua
Member currentMember = memberRepository.findById(memberId)
.orElseThrow(() -> new GlobalException(ErrorCode.MEMBER_NOT_FOUND));


double longitude = diaryManualCreateRequest.getLocationPoint().getLongitude();
double latitude = diaryManualCreateRequest.getLocationPoint().getLatitude();

Expand Down Expand Up @@ -149,21 +148,21 @@ public MyDiariesResponse getMyDiaries(Long memberId, DiaryFilterRequest diaryFil

Page<Diary> diaryPage;

List<Long> categoryIds = diaryFilterRequest.getCategories();
List<String> categories = diaryFilterRequest.getCategories();
List<String> emojis = diaryFilterRequest.getEmojis();

Specification<Diary> spec = DiarySpecification.filterDiaries(
diaryFilterRequest.getKeyword(),
emojis,
categoryIds,
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()));

diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize()));

List<MyDiarySimpleResponse> diaryDTOs = diaryPage.getContent().stream()
.map(this::convertToMyDiarySimpleResponseDto)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
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;

import java.time.LocalDate;

public class DiarySpecification {

public static Specification<Diary> filterDiaries(
String keyword, List<String> emojis, List<Long> categories,
String keyword, List<String> emojis, List<String> categories,
LocalDate date, LocalDate from, LocalDate until, Boolean isBookmark) {
return (Root<Diary> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
Predicate predicate = builder.conjunction();
Expand All @@ -29,15 +30,17 @@ public static Specification<Diary> filterDiaries(
}

if (categories != null && !categories.isEmpty()) {
Join<Diary, DiaryCategory> diaryCategoryJoin = root.join("diaryCategories");
predicate = builder.and(predicate, diaryCategoryJoin.get("category").get("id").in(categories));
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));
}

LocalDate filterDate = (date != null) ? date : LocalDate.now();
predicate = builder.and(predicate, builder.equal(root.get("createAt").as(LocalDate.class), filterDate));

if (from != null && until != null) {
predicate = builder.and(predicate, builder.between(root.get("createAt"), from.atStartOfDay(), until.plusDays(1).atStartOfDay()));
predicate = builder.and(predicate,
builder.between(root.get("createAt"), from.atStartOfDay(), until.plusDays(1).atStartOfDay()));
}
if (isBookmark != null) {
predicate = builder.and(predicate, builder.equal(root.get("isBookmark"), isBookmark));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,21 @@ public FriendDiariesResponse getFriendDiaries(Long memberId, DiaryFilterRequest
Page<Diary> diaryPage;

// 카테고리와 이모지가 여러 개 전달될 수 있으므로 이를 리스트로 변환
List<Long> categoryIds = diaryFilterRequest.getCategories();
List<String> categories = diaryFilterRequest.getCategories();
List<String> emojis = diaryFilterRequest.getEmojis();

Specification<Diary> spec = DiarySpecification.filterDiaries(
diaryFilterRequest.getKeyword(),
emojis,
categoryIds,
categories,
diaryFilterRequest.getDate(),
diaryFilterRequest.getFrom(),
diaryFilterRequest.getUntil(),
diaryFilterRequest.getIsBookmark())
.and((root, query, builder) -> root.get("member").in(friendIdList));

diaryPage = diaryRepository.findAll(spec, PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize()));
diaryPage = diaryRepository.findAll(spec,
PageRequest.of(diaryFilterRequest.getKey(), diaryFilterRequest.getSize()));

List<FriendDiarySimpleResponse> friendDiarySimpleResponseList = diaryPage.getContent().stream()
.map(this::convertToFriendDiariesResponseDTO)
Expand Down

0 comments on commit 3cca37c

Please sign in to comment.