diff --git a/src/main/java/com/kuit/chatdiary/controller/DiaryController.java b/src/main/java/com/kuit/chatdiary/controller/DiaryController.java index 04158ac4..2919cb48 100644 --- a/src/main/java/com/kuit/chatdiary/controller/DiaryController.java +++ b/src/main/java/com/kuit/chatdiary/controller/DiaryController.java @@ -34,7 +34,7 @@ public class DiaryController { @GetMapping("/detail") - public ResponseEntity showDiary(@RequestParam(name="user_id") Long userId, @RequestParam(name="diary_date") Date diaryDate) throws ParseException, ParseException { + public ResponseEntity showDiary(@RequestParam(name="user_id") Long userId, @RequestParam(name="diary_date") Date diaryDate) throws Exception { log.info("[DiaryController.showDiary]"); return ResponseEntity.ok().body(diaryService.showDiary(userId,diaryDate)); diff --git a/src/main/java/com/kuit/chatdiary/dto/diary/DiaryShowDetailResponseDTO.java b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryShowDetailResponseDTO.java index 7b4e2419..c0d101c6 100644 --- a/src/main/java/com/kuit/chatdiary/dto/diary/DiaryShowDetailResponseDTO.java +++ b/src/main/java/com/kuit/chatdiary/dto/diary/DiaryShowDetailResponseDTO.java @@ -19,12 +19,12 @@ public class DiaryShowDetailResponseDTO { private String title; - //private String imgUrl; private List imgUrl; private String content; - //private String tagName; private List tagName; + private Long CharacterIndex; + } diff --git a/src/main/java/com/kuit/chatdiary/repository/DiaryRepository.java b/src/main/java/com/kuit/chatdiary/repository/DiaryRepository.java index 061f6710..d01d25bd 100644 --- a/src/main/java/com/kuit/chatdiary/repository/DiaryRepository.java +++ b/src/main/java/com/kuit/chatdiary/repository/DiaryRepository.java @@ -8,10 +8,14 @@ import jakarta.persistence.EntityManager; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Repository; +import org.springframework.web.server.ResponseStatusException; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -22,7 +26,7 @@ public class DiaryRepository { private final EntityManager em; - public DiaryShowDetailResponseDTO showDiaryDetail (Long userId, java.sql.Date diaryDate) { + public DiaryShowDetailResponseDTO showDiaryDetail (Long userId, java.sql.Date diaryDate) throws Exception { log.info("[DiaryRepository.showDiaryDetail]"); @@ -33,12 +37,17 @@ public DiaryShowDetailResponseDTO showDiaryDetail (Long userId, java.sql.Date di List resultList = em.createQuery("SELECT d.diaryId, d.title, d.content FROM diary d WHERE d.member.userId = :user_id AND d.diaryDate = :diary_date") .setParameter("user_id", userId).setParameter("diary_date", diaryDate).getResultList(); + if(resultList.size()==0){ + throw new ResponseStatusException(HttpStatus.NOT_FOUND); + } + for(Object[] result : resultList){ diaryId = (Long) result[0]; title = (String) result[1]; content = (String) result[2]; } + List imageUrlList = em.createQuery("SELECT p.imageUrl FROM diaryphoto dp LEFT OUTER JOIN dp.photo p"+ " WHERE dp.diary.diaryId = :diary_id").setParameter("diary_id", diaryId).getResultList(); @@ -47,8 +56,18 @@ public DiaryShowDetailResponseDTO showDiaryDetail (Long userId, java.sql.Date di " WHERE dt.diary.id = :diary_id") .setParameter("diary_id", diaryId).getResultList(); + List senderCounts = em.createQuery("SELECT c.sender, MAX(c.createAt) AS latest_created_at, COUNT(*) AS cnt FROM chat c"+ + " WHERE DATE(c.createAt) = : diary_date AND c.sender NOT IN :user"+ + " GROUP BY c.sender ORDER BY cnt DESC, latest_created_at DESC") + .setParameter("diary_date", diaryDate).setParameter("user", Sender.USER).getResultList(); + + if(senderCounts.size()==0){ + return new DiaryShowDetailResponseDTO(diaryDate, title, imageUrlList, content, tagNameList, null); + } + + Sender sender = (Sender) senderCounts.get(0)[0]; + return new DiaryShowDetailResponseDTO(diaryDate, title, imageUrlList, content, tagNameList, (long) sender.getIndex()); - return new DiaryShowDetailResponseDTO(diaryDate, title, imageUrlList, content, tagNameList); } @@ -64,6 +83,7 @@ public Long findDiaryId(DiaryModifyRequestDTO diaryModifyRequestDTO) throws Pars List resultList = em.createQuery("SELECT d.diaryId FROM diary d WHERE d.member.userId = :user_id AND d.diaryDate = :diary_date") .setParameter("user_id", diaryModifyRequestDTO.getUserId()).setParameter("diary_date",diaryDate).getResultList(); + for(Long result : resultList){ diaryId = result; } diff --git a/src/main/java/com/kuit/chatdiary/service/DiaryService.java b/src/main/java/com/kuit/chatdiary/service/DiaryService.java index b4526e84..5aee8fbf 100644 --- a/src/main/java/com/kuit/chatdiary/service/DiaryService.java +++ b/src/main/java/com/kuit/chatdiary/service/DiaryService.java @@ -10,6 +10,7 @@ import com.kuit.chatdiary.repository.DiaryRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; @@ -32,7 +33,7 @@ public class DiaryService { private final S3Uploader s3Uploader; - public DiaryShowDetailResponseDTO showDiary(Long userId, Date diaryDate) throws ParseException { + public DiaryShowDetailResponseDTO showDiary(Long userId, Date diaryDate) throws Exception { log.info("[DiaryService.showDiary]"); return diaryRepository.showDiaryDetail(userId, diaryDate); }