Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat:CHAT-152-BE-api-월별-일기-리스트-조회 #24

Merged
merged 9 commits into from
Jan 30, 2024
Original file line number Diff line number Diff line change
@@ -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<List<DiaryListResponseDTO>> getMonthlyDiaryList(@RequestParam("user_id") Long user_id,
@RequestParam("year") int year, @RequestParam("month") int month){
List<DiaryListResponseDTO> monthlyEvents = diaryListService.getMonthlyDiaryPhotos(user_id,year,month);
return ResponseEntity.ok(monthlyEvents);
}


}
Original file line number Diff line number Diff line change
@@ -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<String> photoUrls;
private List<TagInfoDTO> tagList;

public DiaryListResponseDTO(Diary diary, List<DiaryPhoto> diaryPhotos, List<DiaryTag> 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());
}
}
15 changes: 15 additions & 0 deletions src/main/java/com/kuit/chatdiary/dto/diary/TagInfoDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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<DiaryListResponseDTO> inquiryDiaryRange(Long userId, Date startDate, Date endDate) {
List<Diary> 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<DiaryPhoto> diaryPhotos = em.createQuery("SELECT dp FROM diaryphoto dp WHERE dp.diary = :diary", DiaryPhoto.class)
.setParameter("diary", diary)
.getResultList();
List<DiaryTag> 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());
}
}


Original file line number Diff line number Diff line change
@@ -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<DiaryListResponseDTO> 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);
}
}
Loading