diff --git a/src/main/java/org/dallili/secretfriends/controller/DiaryController.java b/src/main/java/org/dallili/secretfriends/controller/DiaryController.java index 0c5c0a2..c31cea1 100644 --- a/src/main/java/org/dallili/secretfriends/controller/DiaryController.java +++ b/src/main/java/org/dallili/secretfriends/controller/DiaryController.java @@ -53,9 +53,9 @@ public void diaryStateModify (@PathVariable("diaryID") Long diaryID){ diaryService.modifyState(diaryID); Long receiverID = diaryService.findDiaryById(diaryID).getMember().getMemberID(); Long senderID = diaryService.findDiaryById(diaryID).getPartner().getMemberID(); - emitterService.sendEvents(receiverID, senderID, NotifyDTO.NotifyType.INACTIVATE); - emitterService.sendEvents(senderID, receiverID, NotifyDTO.NotifyType.INACTIVATE); - notifyService.saveNotifyTable(receiverID, senderID, NotifyDTO.NotifyType.INACTIVATE); + emitterService.sendEvents(diaryID, receiverID, senderID, NotifyDTO.NotifyType.INACTIVATE); + emitterService.sendEvents(diaryID, senderID, receiverID, NotifyDTO.NotifyType.INACTIVATE); + notifyService.saveNotifyTable(diaryID, receiverID, senderID, NotifyDTO.NotifyType.INACTIVATE); log.info(diaryService.findOne(diaryID)); diff --git a/src/main/java/org/dallili/secretfriends/controller/EntryController.java b/src/main/java/org/dallili/secretfriends/controller/EntryController.java index f8bad13..8b5ccf5 100644 --- a/src/main/java/org/dallili/secretfriends/controller/EntryController.java +++ b/src/main/java/org/dallili/secretfriends/controller/EntryController.java @@ -75,8 +75,9 @@ public Map stateModify(@PathVariable("entryID") Long entryID, Aut Boolean result = entryService.modifyState(entryID,memberID); if(result){ Long receiverID = entryService.findOpponent(entryID, memberID); - emitterService.sendEvents(receiverID, memberID, NotifyDTO.NotifyType.REPLY); - notifyService.saveNotifyTable(receiverID, memberID, NotifyDTO.NotifyType.REPLY); + Long diaryID = entryService.findDiaryID(entryID); + emitterService.sendEvents(diaryID, receiverID, memberID, NotifyDTO.NotifyType.REPLY); + notifyService.saveNotifyTable(diaryID, receiverID, memberID, NotifyDTO.NotifyType.REPLY); return Map.of("entryID",Long.toString(entryID), "result","일기 전달 성공"); diff --git a/src/main/java/org/dallili/secretfriends/controller/MatchingController.java b/src/main/java/org/dallili/secretfriends/controller/MatchingController.java index fbbe648..610dd1e 100644 --- a/src/main/java/org/dallili/secretfriends/controller/MatchingController.java +++ b/src/main/java/org/dallili/secretfriends/controller/MatchingController.java @@ -79,9 +79,9 @@ public Map partnerModify(String code, Authentication authenticat diaryService.modifyUpdate(diaryID, partnerID); diaryService.modifyPartner(code, partnerID); - emitterService.sendEvents(receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); - emitterService.sendEvents(partnerID, receiverID, NotifyDTO.NotifyType.NEWDIARY); - notifyService.saveNotifyTable(receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); + emitterService.sendEvents(diaryID, receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); + emitterService.sendEvents(diaryID, partnerID, receiverID, NotifyDTO.NotifyType.NEWDIARY); + notifyService.saveNotifyTable(diaryID, receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); matchingHistoryService.addHistory(receiverID, partnerID); result.put("diaryID", diaryID.toString()); @@ -104,10 +104,11 @@ public Map unknownMatchingAdd(@Valid @RequestBody MatchingDTO.ne Long receiverID = (Long)result.get("memberID"); Long partnerID = (Long)result.get("partnerID"); + Long diaryID = (Long)result.get("diaryID"); - emitterService.sendEvents(receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); - emitterService.sendEvents(partnerID, receiverID, NotifyDTO.NotifyType.NEWDIARY); - notifyService.saveNotifyTable(receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); + emitterService.sendEvents(diaryID, receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); + emitterService.sendEvents(diaryID, partnerID, receiverID, NotifyDTO.NotifyType.NEWDIARY); + notifyService.saveNotifyTable(diaryID, receiverID, partnerID, NotifyDTO.NotifyType.NEWDIARY); } return result; diff --git a/src/main/java/org/dallili/secretfriends/notify/controller/NotifyController.java b/src/main/java/org/dallili/secretfriends/notify/controller/NotifyController.java index e854529..f1e8fe1 100644 --- a/src/main/java/org/dallili/secretfriends/notify/controller/NotifyController.java +++ b/src/main/java/org/dallili/secretfriends/notify/controller/NotifyController.java @@ -21,9 +21,8 @@ public class NotifyController { private final NotifyService notifyService; @Operation(summary = "Notify List GET", description = "알림 목록 조회") @GetMapping(value = "/") - public List notifyDTOList (Authentication authentication){ + public List notifyDTOList (Authentication authentication){ return notifyService.findAllNotify(Long.parseLong(authentication.getName())); } - } diff --git a/src/main/java/org/dallili/secretfriends/notify/domain/Notify.java b/src/main/java/org/dallili/secretfriends/notify/domain/Notify.java index f5b3c7e..d6d55a2 100644 --- a/src/main/java/org/dallili/secretfriends/notify/domain/Notify.java +++ b/src/main/java/org/dallili/secretfriends/notify/domain/Notify.java @@ -1,8 +1,11 @@ package org.dallili.secretfriends.notify.domain; +import com.fasterxml.jackson.annotation.JsonFormat; import jakarta.persistence.*; import lombok.*; +import org.dallili.secretfriends.domain.Diary; import org.dallili.secretfriends.domain.Member; +import org.dallili.secretfriends.notify.dto.NotifyDTO; import org.hibernate.annotations.OnDelete; import org.hibernate.annotations.OnDeleteAction; import org.springframework.data.annotation.LastModifiedDate; @@ -25,9 +28,11 @@ public class Notify { private Long notifyID; @Column(name = "notifyType") - private String notifyType; + private NotifyDTO.NotifyType notifyType; @Column(name = "updatedAt", columnDefinition = "TIMESTAMP") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @LastModifiedDate private LocalDateTime updatedAt; @ManyToOne (fetch = FetchType.LAZY) //여러 개의 알림이 하나의 user에 속할 수 있음 @@ -40,6 +45,13 @@ public class Notify { @OnDelete(action = OnDeleteAction.CASCADE) private Member sender; // 일기 비활성화 시킨 사람, 답장을 보낸 사람. + @ManyToOne(fetch = FetchType.LAZY) + @OnDelete(action = OnDeleteAction.CASCADE) + @JoinColumn(name = "diary_id", referencedColumnName = "diary_id", insertable = true, updatable = false) + private Diary diary; + + + } diff --git a/src/main/java/org/dallili/secretfriends/notify/dto/NotifyDTO.java b/src/main/java/org/dallili/secretfriends/notify/dto/NotifyDTO.java index 3e7d79d..9d09ba6 100644 --- a/src/main/java/org/dallili/secretfriends/notify/dto/NotifyDTO.java +++ b/src/main/java/org/dallili/secretfriends/notify/dto/NotifyDTO.java @@ -17,14 +17,11 @@ public class NotifyDTO { @NotNull private Long notifyID; - - private Enum notifyType; - + private NotifyDTO.NotifyType notifyType; @NotNull - private Long receiverID; //get, set - - private Long senderID; //get, set - + private Long receiverID; + private Long senderID; + private Long diaryID; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime updatedAt; // get, set @@ -32,9 +29,22 @@ public enum NotifyType{ NEWDIARY, REPLY, INACTIVATE } - //modelmapper 매핑 규칙 정의를 위한 setter - //일반 코드 작성 시에는 사용 지양 - public void setNotifyType(Enum notifyType) { - this.notifyType = notifyType; + @Data + @Builder + public static class notifyResponse{ + @NotNull + private Long notifyID; + private NotifyDTO.NotifyType notifyType; + @NotNull + private String receiverNickname; + private String senderNickname; + private String diaryColor; + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime updatedAt; // get, set + + } + + + } diff --git a/src/main/java/org/dallili/secretfriends/notify/service/EmitterService.java b/src/main/java/org/dallili/secretfriends/notify/service/EmitterService.java index c353ad0..e999dce 100644 --- a/src/main/java/org/dallili/secretfriends/notify/service/EmitterService.java +++ b/src/main/java/org/dallili/secretfriends/notify/service/EmitterService.java @@ -11,10 +11,10 @@ public interface EmitterService { void sendDummyEvents(Long receiverID, Object data); // 특정 이벤트 발생 시, 1. emitter를 통해 유저에게 알림 보내고 2. notify 테이블에 알림 데이터 저장하는 메소드 - void sendEvents(Long receiverID, Long senderID, NotifyDTO.NotifyType type); + void sendEvents(Long diaryID, Long receiverID, Long senderID, NotifyDTO.NotifyType type); // "1. emitter를 통해 유저에게 알림 보내고" 담당하는 메소드 - Map addNewEvents(String senderNickname, NotifyDTO.NotifyType type); + Map addNewEvents(Long diaryID, String senderNickname, NotifyDTO.NotifyType type); SseEmitter addEmitter(Long memberID, SseEmitter emitter); } diff --git a/src/main/java/org/dallili/secretfriends/notify/service/EmitterServiceImpl.java b/src/main/java/org/dallili/secretfriends/notify/service/EmitterServiceImpl.java index 4c27187..3f37bbe 100644 --- a/src/main/java/org/dallili/secretfriends/notify/service/EmitterServiceImpl.java +++ b/src/main/java/org/dallili/secretfriends/notify/service/EmitterServiceImpl.java @@ -5,6 +5,7 @@ import lombok.extern.log4j.Log4j2; import org.dallili.secretfriends.notify.dto.NotifyDTO; import org.dallili.secretfriends.notify.repository.EmitterRepository; +import org.dallili.secretfriends.service.DiaryService; import org.dallili.secretfriends.service.MemberService; import org.springframework.http.MediaType; import org.springframework.stereotype.Service; @@ -24,13 +25,16 @@ public class EmitterServiceImpl implements EmitterService{ private final EmitterRepository emitterRepository; private final ObjectMapper objectMapper; private final MemberService memberService; + private final DiaryService diaryService; - public Map addNewEvents(String senderNickname, NotifyDTO.NotifyType type){ + public Map addNewEvents(Long diaryID, String senderNickname, NotifyDTO.NotifyType type){ LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedDateTime = now.format(formatter); + String color = diaryService.findDiaryById(diaryID).getColor(); + Map EventData = new HashMap<>(); switch (type){ case NEWDIARY: EventData.put("message", "님과의 일기장이 만들어졌어요."); break; @@ -40,6 +44,7 @@ public Map addNewEvents(String senderNickname, NotifyDTO.NotifyT EventData.put("type", type); EventData.put("timestamp", formattedDateTime); EventData.put("opponent", senderNickname); // 상대방 + EventData.put("color", color); //log.info(EventData); @@ -59,14 +64,14 @@ public void sendDummyEvents(Long receiverID, Object data){ } } - public void sendEvents(Long receiverID, Long senderID, NotifyDTO.NotifyType type){ + public void sendEvents(Long diaryID, Long receiverID, Long senderID, NotifyDTO.NotifyType type){ SseEmitter receiverEmitter = emitterRepository.findById(receiverID); String senderNickname = memberService.findMemberById(senderID).getNickname(); try { // receiver에게 보낼 알림 // 1. 데이터 생성 및 JSON으로 변환 - String receiverJson = objectMapper.writeValueAsString(addNewEvents(senderNickname, type)); + String receiverJson = objectMapper.writeValueAsString(addNewEvents(diaryID, senderNickname, type)); log.info(receiverJson); // 2. receiver의 emitter로 전송 receiverEmitter.send(receiverJson, MediaType.APPLICATION_JSON); diff --git a/src/main/java/org/dallili/secretfriends/notify/service/NotifyService.java b/src/main/java/org/dallili/secretfriends/notify/service/NotifyService.java index ae1c783..c780ba0 100644 --- a/src/main/java/org/dallili/secretfriends/notify/service/NotifyService.java +++ b/src/main/java/org/dallili/secretfriends/notify/service/NotifyService.java @@ -7,8 +7,8 @@ public interface NotifyService { // notify 테이블에 알림 데이터 저장" 담당하는 메소드 - void saveNotifyTable(Long receiverID, Long senderID, NotifyDTO.NotifyType type); + void saveNotifyTable(Long diaryID, Long receiverID, Long senderID, NotifyDTO.NotifyType type); void removeNotify(Long notifyID); - List findAllNotify(Long receiverID); + List findAllNotify(Long receiverID); } diff --git a/src/main/java/org/dallili/secretfriends/notify/service/NotifyServiceImpl.java b/src/main/java/org/dallili/secretfriends/notify/service/NotifyServiceImpl.java index 0e5dcbf..588ac87 100644 --- a/src/main/java/org/dallili/secretfriends/notify/service/NotifyServiceImpl.java +++ b/src/main/java/org/dallili/secretfriends/notify/service/NotifyServiceImpl.java @@ -1,5 +1,6 @@ package org.dallili.secretfriends.notify.service; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.log4j.Log4j2; import org.dallili.secretfriends.notify.domain.Notify; @@ -8,7 +9,6 @@ import org.modelmapper.ModelMapper; import org.springframework.stereotype.Service; -import java.time.LocalDateTime; import java.util.List; import java.util.stream.Collectors; @@ -20,13 +20,13 @@ public class NotifyServiceImpl implements NotifyService{ private final NotifyRepository notifyRepository; private final ModelMapper modelMapper; - public void saveNotifyTable(Long receiverID, Long senderID, NotifyDTO.NotifyType type){ + public void saveNotifyTable(Long diaryID, Long receiverID, Long senderID, NotifyDTO.NotifyType type){ NotifyDTO receiverNotifyDTO = NotifyDTO.builder() .notifyType(type) - .updatedAt(LocalDateTime.now()) .receiverID(receiverID) .senderID(senderID) + .diaryID(diaryID) .build(); log.info(receiverNotifyDTO); Notify receiverNotify = modelMapper.map(receiverNotifyDTO, Notify.class); @@ -36,9 +36,9 @@ public void saveNotifyTable(Long receiverID, Long senderID, NotifyDTO.NotifyType if(type == NotifyDTO.NotifyType.NEWDIARY || type == NotifyDTO.NotifyType.INACTIVATE){ NotifyDTO senderNotifyDTO = NotifyDTO.builder() .notifyType(type) - .updatedAt(LocalDateTime.now()) .receiverID(senderID) .senderID(receiverID) + .diaryID(diaryID) .build(); Notify senderNotify = modelMapper.map(senderNotifyDTO, Notify.class); notifyRepository.save(senderNotify); @@ -49,18 +49,24 @@ public void removeNotify(Long notifyID){ notifyRepository.deleteById(notifyID); } - public List findAllNotify(Long receiverID){ + @Transactional + public List findAllNotify(Long receiverID) { List notifyList = notifyRepository.findAllByReceiver_MemberID(receiverID); - List notifyDTOList = notifyList.stream() + List notifyResponseList = notifyList.stream() .map(notify -> { - NotifyDTO notifyDTO = modelMapper.map(notify, NotifyDTO.class); - // notificationType 가공하여 NotifyDTO에 추가 - notifyDTO.setNotifyType(NotifyDTO.NotifyType.valueOf(notify.getNotifyType())); - return notifyDTO; + NotifyDTO.notifyResponse notifyResponse = NotifyDTO.notifyResponse.builder() + .notifyID(notify.getNotifyID()) + .notifyType(notify.getNotifyType()) + .receiverNickname(notify.getReceiver().getNickname()) + .senderNickname(notify.getSender().getNickname()) + .updatedAt(notify.getUpdatedAt()) + .diaryColor(notify.getDiary().getColor()) + .build(); + return notifyResponse; }) .collect(Collectors.toList()); - return notifyDTOList; + return notifyResponseList; } } diff --git a/src/main/java/org/dallili/secretfriends/service/EntryService.java b/src/main/java/org/dallili/secretfriends/service/EntryService.java index 6ad12e9..477ddb6 100644 --- a/src/main/java/org/dallili/secretfriends/service/EntryService.java +++ b/src/main/java/org/dallili/secretfriends/service/EntryService.java @@ -15,5 +15,6 @@ public interface EntryService { List findUnsentEntry(Long diaryID); List modifyTextFiltering(List entry); Long findOpponent(Long entryID, Long memberID); + Long findDiaryID(Long entryID); } diff --git a/src/main/java/org/dallili/secretfriends/service/EntryServiceImpl.java b/src/main/java/org/dallili/secretfriends/service/EntryServiceImpl.java index 2e89f9f..7557cfb 100644 --- a/src/main/java/org/dallili/secretfriends/service/EntryServiceImpl.java +++ b/src/main/java/org/dallili/secretfriends/service/EntryServiceImpl.java @@ -119,4 +119,9 @@ else if (diary.getPartner().getMemberID().equals(memberID)) { } else throw new RuntimeException("존재하지 않는 사용자입니다"); } + + @Override + public Long findDiaryID(Long entryID){ + return entryRepository.findById(entryID).get().getDiary().getDiaryID(); + } } diff --git a/src/test/java/org/dallili/secretfriends/repository/NotifyRepositoryTests.java b/src/test/java/org/dallili/secretfriends/repository/NotifyRepositoryTests.java index c3355f6..45dde63 100644 --- a/src/test/java/org/dallili/secretfriends/repository/NotifyRepositoryTests.java +++ b/src/test/java/org/dallili/secretfriends/repository/NotifyRepositoryTests.java @@ -1,10 +1,12 @@ package org.dallili.secretfriends.repository; import lombok.extern.log4j.Log4j2; +import org.dallili.secretfriends.domain.Diary; import org.dallili.secretfriends.domain.Member; import org.dallili.secretfriends.notify.domain.Notify; import org.dallili.secretfriends.notify.dto.NotifyDTO; import org.dallili.secretfriends.notify.repository.NotifyRepository; +import org.dallili.secretfriends.service.DiaryService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -18,19 +20,25 @@ public class NotifyRepositoryTests { @Autowired private MemberRepository memberRepository; -/* + + @Autowired + private DiaryService diaryService; + + @Test public void testInsert() { Member receiver = memberRepository.findById(1L).orElseThrow(); Member sender = memberRepository.findById(2L).orElseThrow(); + Diary diary = diaryService.findDiaryById(1L); Notify notify = Notify.builder() - .notifyType("NEWDIARY") + .notifyType(NotifyDTO.NotifyType.INACTIVATE) .receiver(receiver) .sender(sender) + .diary(diary) .build(); notifyRepository.save(notify); - }*/ + } } diff --git a/src/test/java/org/dallili/secretfriends/service/EmitterServiceTests.java b/src/test/java/org/dallili/secretfriends/service/EmitterServiceTests.java index 7ffaa42..2336481 100644 --- a/src/test/java/org/dallili/secretfriends/service/EmitterServiceTests.java +++ b/src/test/java/org/dallili/secretfriends/service/EmitterServiceTests.java @@ -33,9 +33,9 @@ public void testSendEvents(){ emitterService.addEmitter(senderID, emitter2); emitterRepository.findAllID(); - emitterService.sendEvents(1L, 2L, NotifyDTO.NotifyType.NEWDIARY); - emitterService.sendEvents(1L, 2L, NotifyDTO.NotifyType.REPLY); - emitterService.sendEvents(1L, 2L, NotifyDTO.NotifyType.INACTIVATE); + emitterService.sendEvents(1L,1L, 2L, NotifyDTO.NotifyType.NEWDIARY); + emitterService.sendEvents(1L,1L, 2L, NotifyDTO.NotifyType.REPLY); + emitterService.sendEvents(1L,1L, 2L, NotifyDTO.NotifyType.INACTIVATE); } diff --git a/src/test/java/org/dallili/secretfriends/service/EntryServiceTests.java b/src/test/java/org/dallili/secretfriends/service/EntryServiceTests.java index 611ad31..fdd9a28 100644 --- a/src/test/java/org/dallili/secretfriends/service/EntryServiceTests.java +++ b/src/test/java/org/dallili/secretfriends/service/EntryServiceTests.java @@ -84,4 +84,9 @@ public void testFindOpponent() { } + @Test + public void testFindDiary() { + log.info(entryService.findDiaryID(3L)); + } + } diff --git a/src/test/java/org/dallili/secretfriends/service/NotifyServiceTests.java b/src/test/java/org/dallili/secretfriends/service/NotifyServiceTests.java index 525db82..5ec2c99 100644 --- a/src/test/java/org/dallili/secretfriends/service/NotifyServiceTests.java +++ b/src/test/java/org/dallili/secretfriends/service/NotifyServiceTests.java @@ -18,7 +18,7 @@ public class NotifyServiceTests { @Test public void testSaveNotify() { - notifyService.saveNotifyTable(1L, 2L, NotifyDTO.NotifyType.INACTIVATE); + notifyService.saveNotifyTable(1L, 1L, 2L, NotifyDTO.NotifyType.INACTIVATE); } @Test @@ -29,6 +29,6 @@ public void testRemoveNotify(){ @Test public void testFindAllNotify() { - log.info(notifyService.findAllNotify(2L)); + log.info(notifyService.findAllNotify(1L)); } }