Skip to content

Commit

Permalink
Feat:CHAT-152-BE-api-월별-일기-리스트-조회 (#24)
Browse files Browse the repository at this point in the history
* Fix: Develop 변경 사항에 맞게 수정해서 브렌치 생성

* Fix: Develop 변경 사항에 맞게 수정

* Fix: 순환참조 오류수정

Tag 엔티티 직접 가져오려니 데이터 더 추가시 순환 참조 오류 발생 우려있어서
TagDTO 추가해서 해결

* Fix: 순환참조 오류수정

Tag 엔티티 직접 가져오려니 데이터 더 추가시 순환 참조 오류 발생 우려있어서
TagDTO 추가해서 해결, 엔티티 객체 직접 참조 안함

* Fix: 도메인 형식 맞춰서 엔드 포인트 수정

* Chore: 엔드 포인트m및 클래스 이름 수정

리뷰 반영해서 쉬운이름으로 개선

* Fix : 파일 구조 수정

* Fix : content를 DTO에서 제외

.size 또한 제외하였습니다.

* Feat: 로직 수정

diaryphoto가 추가됨에 따라 쿼리 로직 수정
  • Loading branch information
Mouon authored Jan 30, 2024
1 parent 2a1cc61 commit f9f5915
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 0 deletions.
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);
}
}

0 comments on commit f9f5915

Please sign in to comment.