diff --git a/src/main/java/com/kuit/chatdiary/controller/diary/DiaryListController.java b/src/main/java/com/kuit/chatdiary/controller/diary/DiaryListController.java new file mode 100644 index 00000000..1eb6d9d6 --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/controller/diary/DiaryListController.java @@ -0,0 +1,32 @@ +package com.kuit.chatdiary.controller.diary; + + +import com.kuit.chatdiary.dto.diary.DiaryListResponseDTO; +import com.kuit.chatdiary.service.diary.DiaryListService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("diary") +public class DiaryListController { + + private final DiaryListService diaryListService; + + public DiaryListController(DiaryListService diaryListService){ + this.diaryListService = diaryListService; + } + + @GetMapping("/monthly/list") + public ResponseEntity> getMonthlyDiaryList(@RequestParam("user_id") Long user_id, + @RequestParam("year") int year, @RequestParam("month") int month){ + List monthlyEvents = diaryListService.getMonthlyDiaryPhotos(user_id,year,month); + return ResponseEntity.ok(monthlyEvents); + } + + +} \ No newline at end of file diff --git a/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java new file mode 100644 index 00000000..7396331d --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryListResponseDTO.java @@ -0,0 +1,39 @@ +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 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; + +@Getter +@Setter +@NoArgsConstructor +public class DiaryListResponseDTO { + private Long diaryId; + private String title; + private Date diaryDate; + private List photoUrls; + private List tagList; + + public DiaryListResponseDTO(Diary diary, List diaryPhotos, List diaryTags) { + this.diaryId = diary.getDiaryId(); + this.title = diary.getTitle(); + this.diaryDate = diary.getDiaryDate(); + 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()); + } +} diff --git a/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java b/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java new file mode 100644 index 00000000..625d51b7 --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java @@ -0,0 +1,15 @@ +package com.kuit.chatdiary.dto.diary; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@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 new file mode 100644 index 00000000..cb6850e0 --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/repository/diary/DiaryListRepository.java @@ -0,0 +1,46 @@ +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; +import org.springframework.stereotype.Repository; + +import java.time.LocalDate; +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + + +@Repository +public class DiaryListRepository { + 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 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()); + } + } + + diff --git a/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java b/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java new file mode 100644 index 00000000..ef15fcf3 --- /dev/null +++ b/src/main/java/com/kuit/chatdiary/service/diary/DiaryListService.java @@ -0,0 +1,29 @@ +package com.kuit.chatdiary.service.diary; + +import com.kuit.chatdiary.dto.diary.DiaryListResponseDTO; +import com.kuit.chatdiary.repository.diary.DiaryListRepository; +import org.springframework.stereotype.Service; + +import java.sql.Date; +import java.time.LocalDate; +import java.util.List; + +@Service +public class DiaryListService { + + private final DiaryListRepository diaryListRepository; + + public DiaryListService(DiaryListRepository diaryListRepository){ + this.diaryListRepository = diaryListRepository; + } + + public List getMonthlyDiaryPhotos(Long userId, int year, int month){ + LocalDate firstDayOfMonthLocal = LocalDate.of(year, month, 1); + LocalDate lastDayOfMonthLocal = firstDayOfMonthLocal.plusMonths(1).minusDays(1); + + Date firstDayOfMonth = Date.valueOf(firstDayOfMonthLocal); + Date lastDayOfMonth = Date.valueOf(lastDayOfMonthLocal); + + return diaryListRepository.inquiryDiaryRange(userId, firstDayOfMonth, lastDayOfMonth); + } +}