Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.jiwon.mylog.domain.comment.dto.response.CommentResponse;
import com.jiwon.mylog.domain.comment.entity.Comment;
import com.jiwon.mylog.domain.comment.repository.CommentRepository;
import com.jiwon.mylog.domain.event.dto.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.comment.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.comment.CommentDeletedEvent;
import com.jiwon.mylog.domain.post.entity.Post;
import com.jiwon.mylog.domain.user.entity.User;
import com.jiwon.mylog.global.common.error.ErrorCode;
Expand Down Expand Up @@ -41,24 +42,24 @@ public CommentResponse createComment(Long userId, Long postId, CommentCreateRequ

Comment parent = null;
if (request.getParentId() != null) {
parent = getComment(request.getParentId());
parent = getComment(request.getParentId());
}

Comment comment = Comment.create(request, parent, user, post);
Comment savedComment = commentRepository.save(comment);

Long receiverId = post.getUser().getId();

if (!receiverId.equals(userId)) {
eventPublisher.publishEvent(
new CommentCreatedEvent(
postId,
post.getUser().getId(),
comment.getId(),
userId,
user.getUsername())
);
}
eventPublisher.publishEvent(
new CommentCreatedEvent(
postId,
receiverId,
savedComment.getId(),
userId,
user.getUsername(),
savedComment.getCreatedAt()
)
);

return CommentResponse.fromComment(savedComment);
}
Expand All @@ -77,6 +78,19 @@ public CommentResponse updateComment(Long userId, Long postId, Long commentId, C
public void deleteComment(Long userId, Long postId, Long commentId) {
Comment comment = getComment(commentId);
validateOwner(userId, comment);

Long receiverId = comment.getPost().getUser().getId();

eventPublisher.publishEvent(
new CommentDeletedEvent(
postId,
receiverId,
comment.getId(),
userId,
comment.getCreatedAt()
)
);

comment.delete();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.jiwon.mylog.domain.event;

import com.jiwon.mylog.domain.event.dto.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.FollowCreatedEvent;
import com.jiwon.mylog.domain.event.dto.FollowDeletedEvent;
import com.jiwon.mylog.domain.event.dto.LikeCreatedEvent;
import com.jiwon.mylog.domain.event.dto.comment.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.follow.FollowCreatedEvent;
import com.jiwon.mylog.domain.event.dto.follow.FollowDeletedEvent;
import com.jiwon.mylog.domain.event.dto.like.LikeCreatedEvent;
import com.jiwon.mylog.domain.notification.entity.Notification;
import com.jiwon.mylog.domain.notification.repository.NotificationRepository;
import com.jiwon.mylog.domain.notification.service.NotificationService;
Expand All @@ -30,66 +30,70 @@ public class NotificationEventListener {
@Transactional
@EventListener
public void handleCommentCreated(CommentCreatedEvent event) {
Long receiverId = event.getPostWriterId();
User receiver = getReceiver(receiverId);

Notification notification = Notification.create(
receiver,
event.getCommentWriterName() + "왹이 외계 수집물에 발 사를 남기다!",
"/posts/" + event.getPostId(),
NotificationType.COMMENT
);
notificationRepository.save(notification);
if (event.postWriterId().equals(event.commentWriterId())) {
return;
}

sendSSE(receiverId, notification);
try {
handleNotification(
event.postWriterId(),
event.commentWriterName() + "왹이 외계 수집물에 발 사를 남기다!",
"/posts/" + event.postId(),
NotificationType.COMMENT
);
} catch (Exception e) {
log.warn("댓글 생성 이벤트 알림 처리 실패: {}", event, e);
}
}

@Transactional
@EventListener
public void handleLikeCreated(LikeCreatedEvent event) {
Long receiverId = event.getPostWriterId();
User receiver = getReceiver(receiverId);

Notification notification = Notification.create(
receiver,
event.getLikeWriterName() + "왹이 외계 수집물에 푸 딩을 달았다!",
"/posts/" + event.getPostId(),
NotificationType.LIKE
);
notificationRepository.save(notification);

sendSSE(receiverId, notification);
try {
handleNotification(
event.postWriterId(),
event.likeWriterName() + "왹이 외계 수집물에 푸 딩을 달았다!",
"/posts/" + event.postId(),
NotificationType.LIKE
);
} catch (Exception e) {
log.warn("좋아요 생성 이벤트 알림 처리 실패: {}", event, e);
}
}

@Transactional
@EventListener
public void handleFollowCreated(FollowCreatedEvent event) {
Long receiverId = event.getReceiverId();
User receiver = getReceiver(event.getReceiverId());

Notification notification = Notification.create(
receiver,
event.getFollowerName() + "왹이 잡 았다! 너 잡혔다!",
"/" + event.getFollowerId(),
NotificationType.FOLLOW
);
notificationRepository.save(notification);

sendSSE(receiverId, notification);
try {
handleNotification(
event.receiverId(),
event.followerName() + "왹이 잡 았다! 너 잡혔다!",
"/" + event.followerId(),
NotificationType.FOLLOW
);
} catch (Exception e) {
log.warn("팔로우 이벤트 알림 처리 실패: {}", event, e);
}
}

@Transactional
@EventListener
public void handleUnFollowCreated(FollowDeletedEvent event) {
Long receiverId = event.getReceiverId();
User receiver = getReceiver(receiverId);
try {
handleNotification(
event.receiverId(),
"오오자비로운" + event.followerName() + "왹께서널놓아주시니",
"/" + event.followerId(),
NotificationType.FOLLOW
);
} catch (Exception e) {
log.warn("언팔로우 이벤트 알림 처리 실패: {}", event, e);
}
}

Notification notification = Notification.create(
receiver,
"오오자비로운" + event.getFollowerName() + "왹께서널놓아주시니",
"/" + event.getFollowerId(),
NotificationType.FOLLOW
);
private void handleNotification(Long receiverId, String content, String url, NotificationType type) {
User receiver = getReceiver(receiverId);
Notification notification = Notification.create(receiver, content, url, type);
notificationRepository.save(notification);

sendSSE(receiverId, notification);
Expand Down
50 changes: 34 additions & 16 deletions src/main/java/com/jiwon/mylog/domain/event/PointEventListener.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package com.jiwon.mylog.domain.event;

import com.jiwon.mylog.domain.event.dto.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.comment.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.DailyLoginEvent;
import com.jiwon.mylog.domain.event.dto.PostCreatedEvent;
import com.jiwon.mylog.domain.event.dto.post.PostCreatedEvent;
import com.jiwon.mylog.domain.point.repository.PointHistoryRepository;
import com.jiwon.mylog.domain.point.service.PointService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Slf4j
@RequiredArgsConstructor
@Component
public class PointEventListener {
Expand All @@ -27,33 +29,49 @@ public class PointEventListener {
@Transactional
@EventListener
public void handleDailyLogin(DailyLoginEvent event) {
if(historyRepository.countDailyPointByDescription(
event.getUserId(),
DAILY_LOGIN_DESCRIPTION) >= DAILY_LOGIN_LIMIT) {
return;
try {
if (historyRepository.countDailyPointByDescription(
event.userId(),
DAILY_LOGIN_DESCRIPTION) >= DAILY_LOGIN_LIMIT) {
return;
}
pointService.earnPoint(event.userId(), 404, DAILY_LOGIN_DESCRIPTION);
} catch (Exception e) {
log.warn("로그인 포인트 이벤트 처리 실패: {}", event, e);
}
pointService.earnPoint(event.getUserId(), 404, DAILY_LOGIN_DESCRIPTION);
}

@Transactional
@EventListener
public void handlePostCreated(PostCreatedEvent event) {
if (historyRepository.countDailyPointByDescription(
event.getUserId(),
POST_EARN_DESCRIPTION) >= POST_POINT_LIMIT) {
return;
try {
if (historyRepository.countDailyPointByDescription(
event.userId(),
POST_EARN_DESCRIPTION) >= POST_POINT_LIMIT) {
return;
}
pointService.earnPoint(event.userId(), 77, POST_EARN_DESCRIPTION);
} catch (Exception e) {
log.warn("게시글 작성 포인트 이벤트 처리 실패: {}", event, e);
}
pointService.earnPoint(event.getUserId(), 77, POST_EARN_DESCRIPTION);
}

@Transactional
@EventListener
public void handleCommentCreated(CommentCreatedEvent event) {
if (historyRepository.countDailyPointByDescription(
event.getCommentWriterId(),
COMMENT_EARN_DESCRIPTION) >= COMMENT_POINT_LIMIT) {
if (!event.postWriterId().equals(event.commentWriterId())) {
return;
}
pointService.earnPoint(event.getCommentWriterId(), 44, COMMENT_EARN_DESCRIPTION);

try {
if (historyRepository.countDailyPointByDescription(
event.commentWriterId(),
COMMENT_EARN_DESCRIPTION) >= COMMENT_POINT_LIMIT) {
return;
}
pointService.earnPoint(event.commentWriterId(), 44, COMMENT_EARN_DESCRIPTION);
} catch (Exception e) {
log.warn("댓글 작성 포인트 이벤트 처리 실패: {}", event, e);
}
}
}
100 changes: 100 additions & 0 deletions src/main/java/com/jiwon/mylog/domain/event/UserStatsEventListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.jiwon.mylog.domain.event;

import com.jiwon.mylog.domain.event.dto.comment.CommentCreatedEvent;
import com.jiwon.mylog.domain.event.dto.comment.CommentDeletedEvent;
import com.jiwon.mylog.domain.event.dto.like.LikeCreatedEvent;
import com.jiwon.mylog.domain.event.dto.like.LikeDeletedEvent;
import com.jiwon.mylog.domain.event.dto.post.PostCreatedEvent;
import com.jiwon.mylog.domain.event.dto.post.PostDeletedEvent;
import com.jiwon.mylog.domain.statistic.UserStatsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@RequiredArgsConstructor
@Component
public class UserStatsEventListener {

private final UserStatsService userStatsService;

@EventListener
public void handleCommentCreated(CommentCreatedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
if (!event.postWriterId().equals(event.commentWriterId())) {
userStatsService.updateReceivedComments(event.postWriterId(), 1);
}
userStatsService.updateCreatedComments(event.commentWriterId(), 1);
} catch (Exception e) {
log.warn("댓글 생성 이벤트 통계 처리 실패: {}", event, e);
}
}

@EventListener
public void handleCommentDeleted(CommentDeletedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
if (!event.postWriterId().equals(event.commentWriterId())) {
userStatsService.updateReceivedComments(event.postWriterId(), -1);
}
userStatsService.updateCreatedComments(event.commentWriterId(), -1);
} catch (Exception e) {
log.warn("댓글 삭제 이벤트 통계 처리 실패: {}", event, e);
}
}

@EventListener
public void handleLikeCreated(LikeCreatedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateReceivedLikes(event.postWriterId(), 1);
} catch (Exception e) {
log.warn("좋아요 생성 이벤트 통계 처리 실패: {}", event, e);
}
}

@EventListener
public void handleLikeDeleted(LikeDeletedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateReceivedLikes(event.postWriterId(), -1);
} catch (Exception e) {
log.warn("좋아요 삭제 이벤트 통계 처리 실패: {}", event, e);
}
}

@EventListener
public void handlePostCreated(PostCreatedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateCreatedPosts(event.userId(), 1);
} catch (Exception e) {
log.warn("게시글 생성 이벤트 통계 처리 실패: {}", event, e);
}
}

@EventListener
public void handlePostDeleted(PostDeletedEvent event) {
if (!validateIsToday(event.createdAt())) return;

try {
userStatsService.updateCreatedPosts(event.userId(), -1);
} catch (Exception e) {
log.warn("게시글 삭제 이벤트 통계 처리 실패: {}", event, e);
}
}

private boolean validateIsToday(LocalDateTime event) {
return event.toLocalDate().equals(LocalDate.now());
}
}

This file was deleted.

Loading