diff --git a/src/main/java/com/back/domain/notification/dto/NotificationItemDto.java b/src/main/java/com/back/domain/notification/dto/NotificationItemDto.java index 1e54ae9d..7bd7d85e 100644 --- a/src/main/java/com/back/domain/notification/dto/NotificationItemDto.java +++ b/src/main/java/com/back/domain/notification/dto/NotificationItemDto.java @@ -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; @@ -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(); } } - diff --git a/src/main/java/com/back/domain/post/comment/service/CommentService.java b/src/main/java/com/back/domain/post/comment/service/CommentService.java index 1d0aaca6..adf191e7 100644 --- a/src/main/java/com/back/domain/post/comment/service/CommentService.java +++ b/src/main/java/com/back/domain/post/comment/service/CommentService.java @@ -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); diff --git a/src/main/java/com/back/domain/post/post/service/PostService.java b/src/main/java/com/back/domain/post/post/service/PostService.java index 59e8d366..0b7a7a4e 100644 --- a/src/main/java/com/back/domain/post/post/service/PostService.java +++ b/src/main/java/com/back/domain/post/post/service/PostService.java @@ -24,13 +24,6 @@ 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; @@ -38,6 +31,10 @@ 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 { @@ -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()); diff --git a/src/test/java/com/back/domain/notification/controller/NotificationControllerTest.java b/src/test/java/com/back/domain/notification/controller/NotificationControllerTest.java index 98aa5ef1..bf7bf050 100644 --- a/src/test/java/com/back/domain/notification/controller/NotificationControllerTest.java +++ b/src/test/java/com/back/domain/notification/controller/NotificationControllerTest.java @@ -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(); @@ -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());