Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.back.domain.notification.entity.Notification;
import com.back.domain.notification.enums.NotificationType;
import com.back.domain.post.post.entity.PostImage;
import java.time.LocalDateTime;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -13,18 +14,29 @@ public class NotificationItemDto {
private NotificationType type;
private Long postId;
private String postTitle;
private String postCategoryName;
private String postThumbnailUrl;
private String message;
private boolean read;
private LocalDateTime createdAt;

public static NotificationItemDto from(Notification n) {
String categoryName = n.getPost().getCategory() != null ? n.getPost().getCategory().getName() : null;
String thumbnailUrl = n.getPost().getImages().stream()
.map(PostImage::getUrl)
.findFirst()
.orElse(null);

return NotificationItemDto.builder()
.id(n.getId())
.type(n.getType())
.postId(n.getPost().getId())
.postTitle(n.getPost().getTitle())
.postCategoryName(categoryName)
.postThumbnailUrl(thumbnailUrl)
.message(n.getMessage())
.read(n.isRead())
.createdAt(n.getCreatedAt())
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req
.build();

// 게시글 작성자에게 알림 전송
String commentMessage = String.format("%s 님이 '%s' 게시글에 댓글을 남겼습니다.", user.getNickname(), post.getTitle());
notificationService.sendNotification(
post.getUser(),
post,
NotificationType.COMMENT,
user.getNickname() + " 님이 댓글을 남겼습니다."
commentMessage
);

Comment saved = commentRepository.save(comment);
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@
import com.back.global.file.dto.UploadedFileDto;
import com.back.global.file.service.FileService;
import com.back.global.rq.Rq;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PostService {
Expand Down Expand Up @@ -264,11 +261,12 @@ public PostLikeResponseDto toggleLike(Long postId) {
abvScoreService.awardForLike(user.getId());

// 게시글 작성자에게 알림 전송
String likeMessage = String.format("%s 님이 '%s' 게시글에 추천을 남겼습니다.", user.getNickname(), post.getTitle());
notificationService.sendNotification(
post.getUser(),
post,
NotificationType.LIKE,
user.getNickname() + " 님이 추천을 남겼습니다."
likeMessage
);

return new PostLikeResponseDto(postLike.getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ void getNotifications_noCursor() throws Exception {
.type(NotificationType.COMMENT)
.postId(55L)
.postTitle("새 댓글")
.postCategoryName("공지")
.postThumbnailUrl("https://example.com/thumb.png")
.message("새 댓글이 달렸습니다.")
.read(false)
.createdAt(LocalDateTime.of(2025, 1, 2, 12, 0))
.build();
Expand All @@ -154,6 +157,9 @@ void getNotifications_noCursor() throws Exception {
.andExpect(jsonPath("$.code").value(200))
.andExpect(jsonPath("$.message").value("success"))
.andExpect(jsonPath("$.data.items[0].id").value(101))
.andExpect(jsonPath("$.data.items[0].postCategoryName").value("공지"))
.andExpect(jsonPath("$.data.items[0].postThumbnailUrl").value("https://example.com/thumb.png"))
.andExpect(jsonPath("$.data.items[0].message").value("새 댓글이 달렸습니다."))
.andExpect(jsonPath("$.data.hasNext").value(false))
.andExpect(jsonPath("$.data.nextCreatedAt").doesNotExist());

Expand Down