Skip to content

Commit

Permalink
feat: #173 알림 엔티티 정규화
Browse files Browse the repository at this point in the history
  • Loading branch information
jaewonLeeKOR committed Sep 23, 2024
1 parent 8158f70 commit 9e67330
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public <T> Boolean sendApns(T request, List<String> deviceToken) {

deviceToken.parallelStream()
.forEach(token -> {
if (token.isBlank()) return;
if (token == null || token.isBlank() || token.equals("string")) return;
tmpWebClient
.method(HttpMethod.POST)
.uri(token)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.server.capple.domain.notifiaction.entity;

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.boardComment.entity.BoardComment;
import com.server.capple.domain.question.entity.Question;
import com.server.capple.global.common.BaseEntity;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -17,12 +14,8 @@ public class Notification extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long memberId;
@ManyToOne(fetch = FetchType.LAZY)
private Board board;
@ManyToOne(fetch = FetchType.LAZY)
private BoardComment boardComment;
@ManyToOne(fetch = FetchType.LAZY)
private Question question;
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private NotificationLog notificationLog;
@Enumerated(EnumType.ORDINAL)
private NotificationType type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.server.capple.domain.notifiaction.entity;

import com.server.capple.global.common.BaseEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.*;

@Getter
@Entity
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class NotificationLog extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String subtitle;
private String body;
private Long boardId;
private Long boardCommentId;
private Long questionId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.server.capple.domain.boardComment.entity.BoardComment;
import com.server.capple.domain.notifiaction.dto.NotificationResponse.NotificationInfo;
import com.server.capple.domain.notifiaction.entity.Notification;
import com.server.capple.domain.notifiaction.entity.NotificationLog;
import com.server.capple.domain.notifiaction.entity.NotificationType;
import com.server.capple.domain.question.entity.Question;
import com.server.capple.global.common.SliceResponse;
Expand All @@ -14,27 +15,41 @@
@Slf4j
@Component
public class NotificationMapper {
public Notification toNotification(Long memberId, Board board, NotificationType type) {
public Notification toNotification(Long memberId, NotificationLog notificationLog, NotificationType type) {
return Notification.builder()
.memberId(memberId)
.board(board)
.notificationLog(notificationLog)
.type(type)
.build();
}

public Notification toNotification(Long memberId, Board board, BoardComment boardComment, NotificationType type) {
public Notification toNotification(NotificationLog notificationLog, NotificationType type) {
return Notification.builder()
.memberId(memberId)
.board(board)
.boardComment(boardComment)
.notificationLog(notificationLog)
.type(type)
.build();
}

public Notification toNotification(Question question, NotificationType type) {
return Notification.builder()
.question(question)
.type(type)
public NotificationLog toNotificationLog(Board board) {
return NotificationLog.builder()
.body(board.getContent())
.boardId(board.getId())
.build();
}

public NotificationLog toNotificationLog(Board board, BoardComment boardComment) {
return NotificationLog.builder()
.subtitle(boardComment.getContent())
.body(board.getContent())
.boardId(board.getId())
.boardCommentId(boardComment.getId())
.build();
}

public NotificationLog toNotificationLog(Question question) {
return NotificationLog.builder()
.body(question.getContent())
.questionId(question.getId())
.build();
}

Expand All @@ -54,19 +69,19 @@ private NotificationInfo toNotificationInfo(Notification notification) {
private NotificationInfo toBoardNotificationInfo(Notification notification) {
return NotificationInfo.builder()
.title(notification.getType().getTitle())
.content(notification.getBoard().getContent())
.boardId(notification.getBoard().getId().toString())
.content(notification.getNotificationLog().getBody())
.boardId(notification.getNotificationLog().getBoardId().toString())
.createdAt(notification.getCreatedAt())
.build();
}

private NotificationInfo toBoardCommentNotificationInfo(Notification notification) {
return NotificationInfo.builder()
.title(notification.getType().getTitle())
.subtitle(notification.getBoardComment().getContent())
.content(notification.getBoard().getContent())
.boardId(notification.getBoard().getId().toString())
.boardCommentId(notification.getBoardComment().getId().toString())
.subtitle(notification.getNotificationLog().getSubtitle())
.content(notification.getNotificationLog().getBody())
.boardId(notification.getNotificationLog().getBoardId().toString())
.boardCommentId(notification.getNotificationLog().getBoardCommentId().toString())
.createdAt(notification.getCreatedAt())
.build();
}
Expand All @@ -75,8 +90,8 @@ private NotificationInfo toQuestionNotificationInfo(Notification notification) {
return NotificationInfo.builder()
.title(notification.getType().getTitle())
.subtitle(notification.getType().getContent())
.content(notification.getQuestion().getContent())
.questionId(notification.getQuestion().getId().toString())
.content(notification.getNotificationLog().getBody())
.questionId(notification.getNotificationLog().getQuestionId().toString())
.createdAt(notification.getCreatedAt())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.server.capple.domain.member.entity.Member;
import com.server.capple.domain.notifiaction.dto.NotificationResponse.NotificationInfo;
import com.server.capple.domain.notifiaction.entity.Notification;
import com.server.capple.domain.notifiaction.entity.NotificationLog;
import com.server.capple.domain.notifiaction.mapper.NotificationMapper;
import com.server.capple.domain.notifiaction.repository.NotificationRepository;
import com.server.capple.domain.question.entity.Question;
Expand Down Expand Up @@ -42,14 +43,18 @@ public void sendBoardHeartNotification(Long actorId, Board board) {
.build();
apnsService.sendApnsToMembers(boardNotificationBody, board.getWriter().getId());
// 알림 데이터베이스 저장
Notification notification = notificationMapper.toNotification(board.getWriter().getId(), board, BOARD_HEART);
Notification notification = notificationMapper.toNotification(
board.getWriter().getId(),
notificationMapper.toNotificationLog(board),
BOARD_HEART);
notificationRepository.save(notification);
}

@Override
@Transactional
public void sendBoardCommentNotification(Long actorId, Board board, BoardComment boardComment) {
List<Member> subscribers = boardSubscribeMemberService.findBoardSubscribeMembers(board.getId());
NotificationLog notificationLog = notificationMapper.toNotificationLog(board, boardComment);
List<Long> subscriberIds = subscribers.stream()
.map(Member::getId)
.filter(id -> !id.equals(actorId))
Expand All @@ -62,7 +67,10 @@ public void sendBoardCommentNotification(Long actorId, Board board, BoardComment
.boardComment(boardComment)
.build();
apnsService.sendApnsToMembers(boardCommentNotificationBody, subscriberId);
notificationRepository.save(notificationMapper.toNotification(subscriberId, board, boardComment, BOARD_COMMENT));
notificationRepository.save(notificationMapper.toNotification(
subscriberId,
notificationLog,
BOARD_COMMENT));
}
})
.filter(id -> !id.equals(board.getWriter().getId()))
Expand All @@ -75,7 +83,10 @@ public void sendBoardCommentNotification(Long actorId, Board board, BoardComment
apnsService.sendApnsToMembers(boardCommentNotificationBody, subscriberIds);
// 알림 데이터베이스 저장
List<Notification> notifications = subscriberIds.stream()
.map(subscriberId -> notificationMapper.toNotification(subscriberId, board, boardComment, BOARD_COMMENT_DUPLICATE))
.map(subscriberId -> notificationMapper.toNotification(
subscriberId,
notificationLog,
BOARD_COMMENT_DUPLICATE))
.toList();
notificationRepository.saveAll(notifications);
}
Expand All @@ -89,7 +100,10 @@ public void sendBoardCommentHeartNotification(Long actorId, Board board, BoardCo
.build();
apnsService.sendApnsToMembers(boardCommentNotificationBody, boardComment.getWriter().getId());
// 알림 데이터베이스 저장
Notification notification = notificationMapper.toNotification(boardComment.getWriter().getId(), board, boardComment, BOARD_COMMENT_HEART);
Notification notification = notificationMapper.toNotification(
boardComment.getWriter().getId(),
notificationMapper.toNotificationLog(board, boardComment),
BOARD_COMMENT_HEART);
notificationRepository.save(notification);
}

Expand All @@ -100,7 +114,9 @@ public void sendLiveQuestionOpenNotification(Question question) {
.question(question)
.build();
apnsService.sendApnsToAllMembers(questionNotificationBody);
Notification notification = notificationMapper.toNotification(question, TODAY_QUESTION_PUBLISHED);
Notification notification = notificationMapper.toNotification(
notificationMapper.toNotificationLog(question),
TODAY_QUESTION_PUBLISHED);
notificationRepository.save(notification);
}

Expand All @@ -111,7 +127,9 @@ public void sendLiveQuestionCloseNotification(Question question) {
.question(question)
.build();
apnsService.sendApnsToAllMembers(questionNotificationBody);
Notification notification = notificationMapper.toNotification(question, TODAY_QUESTION_CLOSED);
Notification notification = notificationMapper.toNotification(
notificationMapper.toNotificationLog(question),
TODAY_QUESTION_CLOSED);
notificationRepository.save(notification);
}

Expand Down

0 comments on commit 9e67330

Please sign in to comment.