diff --git a/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java index 4ff0273e..7396331d 100644 --- a/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java +++ b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java @@ -1,9 +1,15 @@ package com.kuit.chatdiary.dto.diary; import com.kuit.chatdiary.domain.Diary; +import com.kuit.chatdiary.domain.DiaryPhoto; +import com.kuit.chatdiary.domain.DiaryTag; import com.kuit.chatdiary.domain.Photo; -import lombok.*; +import com.kuit.chatdiary.domain.Tag; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import java.time.LocalDateTime; import java.util.Date; import java.util.List; import java.util.stream.Collectors; @@ -13,26 +19,21 @@ @NoArgsConstructor public class DiaryListResponseDTO { private Long diaryId; - private Long userId; private String title; private Date diaryDate; + private List photoUrls; private List tagList; - private List photoList; - @Builder - public DiaryListResponseDTO(Diary diary){ + + public DiaryListResponseDTO(Diary diary, List diaryPhotos, List diaryTags) { this.diaryId = diary.getDiaryId(); this.title = diary.getTitle(); this.diaryDate = diary.getDiaryDate(); - this.tagList = diary.getDiaryTagList().stream() - .map(diaryTag -> { - TagInfoDTO TagInfoDTO = new TagInfoDTO(); - TagInfoDTO.setTagId(diaryTag.getTag().getTagId()); - TagInfoDTO.setTagName(diaryTag.getTag().getTagName()); - return TagInfoDTO; - }) - .collect(Collectors.toList()); - this.photoList = diary.getPhotoList().stream() + this.photoUrls = diaryPhotos.stream() + .map(DiaryPhoto::getPhoto) .map(Photo::getImageUrl) .collect(Collectors.toList()); + this.tagList = diaryTags.stream() + .map(diaryTag -> new TagInfoDTO(diaryTag.getTag().getTagId(), diaryTag.getTag().getTagName())) + .collect(Collectors.toList()); } -} \ No newline at end of file +} diff --git a/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java b/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java index 2e1d5cc1..625d51b7 100644 --- a/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java +++ b/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java @@ -1,5 +1,6 @@ package com.kuit.chatdiary.dto.diary; +import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -7,6 +8,7 @@ @Getter @Setter @NoArgsConstructor +@AllArgsConstructor public class TagInfoDTO { private Long tagId; private String tagName; diff --git a/src/main/java/com/kuit/chatdiary/repository/diary/DiaryListRepository.java b/src/main/java/com/kuit/chatdiary/repository/diary/DiaryListRepository.java index ecf54906..cb6850e0 100644 --- a/src/main/java/com/kuit/chatdiary/repository/diary/DiaryListRepository.java +++ b/src/main/java/com/kuit/chatdiary/repository/diary/DiaryListRepository.java @@ -1,6 +1,8 @@ package com.kuit.chatdiary.repository.diary; import com.kuit.chatdiary.domain.Diary; +import com.kuit.chatdiary.domain.DiaryPhoto; +import com.kuit.chatdiary.domain.DiaryTag; import com.kuit.chatdiary.dto.diary.DiaryListResponseDTO; import jakarta.persistence.EntityManager; import jakarta.persistence.TemporalType; @@ -11,23 +13,34 @@ import java.util.List; import java.util.stream.Collectors; + @Repository public class DiaryListRepository { - private final EntityManager em; - public DiaryListRepository(EntityManager em){ - this.em=em; - } + private final EntityManager em; + + public DiaryListRepository(EntityManager em) { + this.em = em; + } - public List inquiryDiaryRange(Long userId, Date startDate, Date endDate) { - List diaries=em.createQuery("SELECT d FROM diary d LEFT JOIN FETCH d.diaryTagList WHERE d.member.userId = :userId AND d.diaryDate BETWEEN :startDate AND :endDate", Diary.class) - .setParameter("userId", userId) - .setParameter("startDate", startDate, TemporalType.DATE) - .setParameter("endDate", endDate,TemporalType.DATE) - .getResultList(); - - return diaries.stream() - .map(DiaryListResponseDTO::new) - .collect(Collectors.toList()); + /** 우선 쿼리 세개 날리기.. */ + public List inquiryDiaryRange(Long userId, Date startDate, Date endDate) { + List diaries = em.createQuery("SELECT d FROM diary d WHERE d.member.userId = :userId AND d.diaryDate BETWEEN :startDate AND :endDate", Diary.class) + .setParameter("userId", userId) + .setParameter("startDate", startDate, TemporalType.DATE) + .setParameter("endDate", endDate, TemporalType.DATE) + .getResultList(); + return diaries.stream().map(diary -> { + List diaryPhotos = em.createQuery("SELECT dp FROM diaryphoto dp WHERE dp.diary = :diary", DiaryPhoto.class) + .setParameter("diary", diary) + .getResultList(); + List diaryTags = em.createQuery("SELECT dt FROM diarytag dt WHERE dt.diary = :diary", DiaryTag.class) + .setParameter("diary", diary) + .getResultList(); + + return new DiaryListResponseDTO(diary, diaryPhotos, diaryTags); + }).collect(Collectors.toList()); + } } -} \ No newline at end of file + + diff --git a/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java b/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java index 1f7c85b8..ef15fcf3 100644 --- a/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java +++ b/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java @@ -14,23 +14,16 @@ public class DiaryListService { private final DiaryListRepository diaryListRepository; public DiaryListService(DiaryListRepository diaryListRepository){ - this.diaryListRepository=diaryListRepository; + this.diaryListRepository = diaryListRepository; } - /** - * diary.getPhotoList().size(); 를 통해 jpa가 각 객체의 사진 목록 로드 - * */ - public List getMonthlyDiaryPhotos(Long userId, int year, int month){ - LocalDate firstDayOfMonthLocal =LocalDate.of(year,month,1); - LocalDate lastDayOfMonthLocal =firstDayOfMonthLocal.plusMonths(1).minusDays(1); + LocalDate firstDayOfMonthLocal = LocalDate.of(year, month, 1); + LocalDate lastDayOfMonthLocal = firstDayOfMonthLocal.plusMonths(1).minusDays(1); Date firstDayOfMonth = Date.valueOf(firstDayOfMonthLocal); Date lastDayOfMonth = Date.valueOf(lastDayOfMonthLocal); - List diaries = diaryListRepository.inquiryDiaryRange(userId, firstDayOfMonth, lastDayOfMonth); - diaries.forEach(diary -> diary.getPhotoList()); - return diaries; + return diaryListRepository.inquiryDiaryRange(userId, firstDayOfMonth, lastDayOfMonth); } - -} \ No newline at end of file +}