Skip to content

Commit

Permalink
Merge pull request #83 from Dallili/dev/feat/notify3
Browse files Browse the repository at this point in the history
[feat] 프론트 요청 사항 반영
  • Loading branch information
idon1nab authored May 8, 2024
2 parents 80be94e + 9d91345 commit cfb152b
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public Map<String,String> 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","일기 전달 성공");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ public Map<String, String> 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());
Expand All @@ -104,10 +104,11 @@ public Map<String, Object> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ public class NotifyController {
private final NotifyService notifyService;
@Operation(summary = "Notify List GET", description = "알림 목록 조회")
@GetMapping(value = "/")
public List<NotifyDTO> notifyDTOList (Authentication authentication){
public List<NotifyDTO.notifyResponse> notifyDTOList (Authentication authentication){
return notifyService.findAllNotify(Long.parseLong(authentication.getName()));
}


}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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에 속할 수 있음
Expand All @@ -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;




}

32 changes: 21 additions & 11 deletions src/main/java/org/dallili/secretfriends/notify/dto/NotifyDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,34 @@ public class NotifyDTO {

@NotNull
private Long notifyID;

private Enum<NotifyDTO.NotifyType> 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

public enum NotifyType{
NEWDIARY, REPLY, INACTIVATE
}

//modelmapper 매핑 규칙 정의를 위한 setter
//일반 코드 작성 시에는 사용 지양
public void setNotifyType(Enum<NotifyType> 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


}



}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> addNewEvents(String senderNickname, NotifyDTO.NotifyType type);
Map<String, Object> addNewEvents(Long diaryID, String senderNickname, NotifyDTO.NotifyType type);

SseEmitter addEmitter(Long memberID, SseEmitter emitter);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, Object> addNewEvents(String senderNickname, NotifyDTO.NotifyType type){
public Map<String, Object> 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<String, Object> EventData = new HashMap<>();
switch (type){
case NEWDIARY: EventData.put("message", "님과의 일기장이 만들어졌어요."); break;
Expand All @@ -40,6 +44,7 @@ public Map<String, Object> addNewEvents(String senderNickname, NotifyDTO.NotifyT
EventData.put("type", type);
EventData.put("timestamp", formattedDateTime);
EventData.put("opponent", senderNickname); // 상대방
EventData.put("color", color);

//log.info(EventData);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotifyDTO> findAllNotify(Long receiverID);
List<NotifyDTO.notifyResponse> findAllNotify(Long receiverID);

}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -49,18 +49,24 @@ public void removeNotify(Long notifyID){
notifyRepository.deleteById(notifyID);
}

public List<NotifyDTO> findAllNotify(Long receiverID){
@Transactional
public List<NotifyDTO.notifyResponse> findAllNotify(Long receiverID) {
List<Notify> notifyList = notifyRepository.findAllByReceiver_MemberID(receiverID);

List<NotifyDTO> notifyDTOList = notifyList.stream()
List<NotifyDTO.notifyResponse> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public interface EntryService {
List<EntryDTO.UnsentEntryResponse> findUnsentEntry(Long diaryID);
List<EntryDTO.SentEntryResponse> modifyTextFiltering(List<EntryDTO.SentEntryResponse> entry);
Long findOpponent(Long entryID, Long memberID);
Long findDiaryID(Long entryID);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public void testFindOpponent() {

}

@Test
public void testFindDiary() {
log.info(entryService.findDiaryID(3L));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,6 +29,6 @@ public void testRemoveNotify(){

@Test
public void testFindAllNotify() {
log.info(notifyService.findAllNotify(2L));
log.info(notifyService.findAllNotify(1L));
}
}

0 comments on commit cfb152b

Please sign in to comment.