Skip to content

Commit 8dc1b3d

Browse files
authored
[refactor] 알림: 응답 DTO 필드 추가 #289 (#290)
* Revert "chore: initData용 이미지 추가" This reverts commit ef30eef. * . * chore: 수정사항 없음 * feat: MyHistoryPostItemDto에 상세 정보 필드 추가 및 매핑 로직 업데이트 * refactor: 게시글 좋아요 알림 메시지에 게시글 제목 추가 * refactor: 댓글 작성 알림 메시지에 게시글 제목 추가 * refactor: 알림 DTO에 카테고리, 썸네일, 알림 메시지 필드 추가 * test: 알림 DTO 변경에 따른 NotificationControllerTest 수정 * chore: 수정사항 없음
1 parent 961d9fa commit 8dc1b3d

File tree

4 files changed

+27
-10
lines changed

4 files changed

+27
-10
lines changed

src/main/java/com/back/domain/notification/dto/NotificationItemDto.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.back.domain.notification.entity.Notification;
44
import com.back.domain.notification.enums.NotificationType;
5+
import com.back.domain.post.post.entity.PostImage;
56
import java.time.LocalDateTime;
67
import lombok.Builder;
78
import lombok.Getter;
@@ -13,18 +14,29 @@ public class NotificationItemDto {
1314
private NotificationType type;
1415
private Long postId;
1516
private String postTitle;
17+
private String postCategoryName;
18+
private String postThumbnailUrl;
19+
private String message;
1620
private boolean read;
1721
private LocalDateTime createdAt;
1822

1923
public static NotificationItemDto from(Notification n) {
24+
String categoryName = n.getPost().getCategory() != null ? n.getPost().getCategory().getName() : null;
25+
String thumbnailUrl = n.getPost().getImages().stream()
26+
.map(PostImage::getUrl)
27+
.findFirst()
28+
.orElse(null);
29+
2030
return NotificationItemDto.builder()
2131
.id(n.getId())
2232
.type(n.getType())
2333
.postId(n.getPost().getId())
2434
.postTitle(n.getPost().getTitle())
35+
.postCategoryName(categoryName)
36+
.postThumbnailUrl(thumbnailUrl)
37+
.message(n.getMessage())
2538
.read(n.isRead())
2639
.createdAt(n.getCreatedAt())
2740
.build();
2841
}
2942
}
30-

src/main/java/com/back/domain/post/comment/service/CommentService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ public CommentResponseDto createComment(Long postId, CommentCreateRequestDto req
4747
.build();
4848

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

5758
Comment saved = commentRepository.save(comment);

src/main/java/com/back/domain/post/post/service/PostService.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@
2424
import com.back.global.file.dto.UploadedFileDto;
2525
import com.back.global.file.service.FileService;
2626
import com.back.global.rq.Rq;
27-
import java.util.ArrayList;
28-
import java.util.List;
29-
import java.util.Map;
30-
import java.util.NoSuchElementException;
31-
import java.util.Optional;
32-
import java.util.function.Function;
33-
import java.util.stream.Collectors;
3427
import lombok.RequiredArgsConstructor;
3528
import org.springframework.stereotype.Service;
3629
import org.springframework.transaction.annotation.Transactional;
3730
import org.springframework.transaction.support.TransactionSynchronization;
3831
import org.springframework.transaction.support.TransactionSynchronizationManager;
3932
import org.springframework.web.multipart.MultipartFile;
4033

34+
import java.util.*;
35+
import java.util.function.Function;
36+
import java.util.stream.Collectors;
37+
4138
@Service
4239
@RequiredArgsConstructor
4340
public class PostService {
@@ -264,11 +261,12 @@ public PostLikeResponseDto toggleLike(Long postId) {
264261
abvScoreService.awardForLike(user.getId());
265262

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

274272
return new PostLikeResponseDto(postLike.getStatus());

src/test/java/com/back/domain/notification/controller/NotificationControllerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ void getNotifications_noCursor() throws Exception {
129129
.type(NotificationType.COMMENT)
130130
.postId(55L)
131131
.postTitle("새 댓글")
132+
.postCategoryName("공지")
133+
.postThumbnailUrl("https://example.com/thumb.png")
134+
.message("새 댓글이 달렸습니다.")
132135
.read(false)
133136
.createdAt(LocalDateTime.of(2025, 1, 2, 12, 0))
134137
.build();
@@ -154,6 +157,9 @@ void getNotifications_noCursor() throws Exception {
154157
.andExpect(jsonPath("$.code").value(200))
155158
.andExpect(jsonPath("$.message").value("success"))
156159
.andExpect(jsonPath("$.data.items[0].id").value(101))
160+
.andExpect(jsonPath("$.data.items[0].postCategoryName").value("공지"))
161+
.andExpect(jsonPath("$.data.items[0].postThumbnailUrl").value("https://example.com/thumb.png"))
162+
.andExpect(jsonPath("$.data.items[0].message").value("새 댓글이 달렸습니다."))
157163
.andExpect(jsonPath("$.data.hasNext").value(false))
158164
.andExpect(jsonPath("$.data.nextCreatedAt").doesNotExist());
159165

0 commit comments

Comments
 (0)