11package com .back .domain .notification .controller ;
22
3- import com .back .domain .notification .dto .NotificationCreateRequest ;
43import com .back .domain .notification .dto .NotificationResponse ;
54import com .back .domain .notification .dto .NotificationListResponse ;
65import com .back .domain .notification .entity .Notification ;
7- import com .back .domain .notification .entity .NotificationSettingType ;
86import com .back .domain .notification .service .NotificationService ;
9- import com .back .domain .studyroom .entity .Room ;
107import com .back .domain .studyroom .repository .RoomRepository ;
118import com .back .domain .user .entity .User ;
129import com .back .domain .user .repository .UserRepository ;
@@ -37,116 +34,7 @@ public class NotificationController {
3734 private final UserRepository userRepository ;
3835 private final RoomRepository roomRepository ;
3936
40- @ Operation (
41- summary = "알림 전송" ,
42- description = "USER/ROOM/COMMUNITY/SYSTEM 타입별 알림 생성 및 전송\n \n " +
43- "- USER: 개인 알림 (actorId, targetId 필수)\n " +
44- "- ROOM: 스터디룸 알림 (actorId, targetId(roomId) 필수)\n " +
45- "- COMMUNITY: 커뮤니티 알림 (actorId, targetId 필수)\n " +
46- "- SYSTEM: 시스템 전체 알림 (actorId, targetId 불필요)"
47- )
48- @ PostMapping
49- public ResponseEntity <RsData <NotificationResponse >> createNotification (
50- @ Parameter (hidden = true ) @ AuthenticationPrincipal CustomUserDetails userDetails ,
51- @ RequestBody NotificationCreateRequest request ) {
52-
53- log .info ("알림 전송 요청 - 타입: {}, 제목: {}" , request .targetType (), request .title ());
54-
55- // targetType 검증
56- if (!isValidTargetType (request .targetType ())) {
57- throw new CustomException (ErrorCode .NOTIFICATION_INVALID_TARGET_TYPE );
58- }
59-
60- // SYSTEM이 아닌 경우 필수 필드 검증
61- if (!"SYSTEM" .equals (request .targetType ())) {
62- if (request .actorId () == null ) {
63- throw new CustomException (ErrorCode .NOTIFICATION_MISSING_ACTOR );
64- }
65- if (request .targetId () == null ) {
66- throw new CustomException (ErrorCode .NOTIFICATION_MISSING_TARGET );
67- }
68- }
69-
70- Notification notification = switch (request .targetType ()) {
71- case "USER" -> {
72- // 수신자 조회
73- User receiver = userRepository .findById (request .targetId ())
74- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
75-
76- // 발신자 조회
77- User actor = userRepository .findById (request .actorId ())
78- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
79-
80- // 개인 알림 생성
81- yield notificationService .createPersonalNotification (
82- receiver ,
83- actor ,
84- request .title (),
85- request .message (),
86- request .redirectUrl (),
87- NotificationSettingType .SYSTEM
88- );
89- }
90- case "ROOM" -> {
91- // 스터디룸 조회
92- Room room = roomRepository .findById (request .targetId ())
93- .orElseThrow (() -> new CustomException (ErrorCode .ROOM_NOT_FOUND ));
94-
95- // 발신자 조회
96- User actor = userRepository .findById (request .actorId ())
97- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
98-
99- // 스터디룸 알림 생성
100- yield notificationService .createRoomNotification (
101- room ,
102- actor ,
103- request .title (),
104- request .message (),
105- request .redirectUrl (),
106- NotificationSettingType .ROOM_NOTICE
107- );
108- }
109- case "COMMUNITY" -> {
110- // 수신자 조회 (리뷰/게시글 작성자)
111- User receiver = userRepository .findById (request .targetId ())
112- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
113-
114- // 발신자 조회 (댓글/좋아요 작성자)
115- User actor = userRepository .findById (request .actorId ())
116- .orElseThrow (() -> new CustomException (ErrorCode .USER_NOT_FOUND ));
117-
118- // 커뮤니티 알림 생성
119- yield notificationService .createCommunityNotification (
120- receiver ,
121- actor ,
122- request .title (),
123- request .message (),
124- request .redirectUrl (),
125- NotificationSettingType .POST_COMMENT
126- );
127- }
128- case "SYSTEM" -> {
129- // 시스템 알림은 발신자/수신자 없음
130- yield notificationService .createSystemNotification (
131- request .title (),
132- request .message (),
133- request .redirectUrl ()
134- );
135- }
136- default -> throw new CustomException (ErrorCode .NOTIFICATION_INVALID_TARGET_TYPE );
137- };
138-
139- NotificationResponse response = NotificationResponse .from (notification );
140-
141- return ResponseEntity .ok (RsData .success ("알림 전송 성공" , response ));
142- }
143-
144- @ Operation (
145- summary = "알림 목록 조회" ,
146- description = "사용자의 알림 목록 조회 (페이징)\n \n " +
147- "- unreadOnly=true: 읽지 않은 알림만\n " +
148- "- unreadOnly=false: 모든 알림"
149- )
37+ @ Operation (summary = "알림 목록 조회" )
15038 @ GetMapping
15139 public ResponseEntity <RsData <NotificationListResponse >> getNotifications (
15240 @ Parameter (hidden = true ) @ AuthenticationPrincipal CustomUserDetails userDetails ,
@@ -157,13 +45,10 @@ public ResponseEntity<RsData<NotificationListResponse>> getNotifications(
15745 log .info ("알림 목록 조회 - 유저 ID: {}, 읽지 않은 것만: {}" , userDetails .getUserId (), unreadOnly );
15846
15947 Pageable pageable = PageRequest .of (page , size );
160- Page <Notification > notifications ;
16148
162- if (unreadOnly ) {
163- notifications = notificationService .getUnreadNotifications (userDetails .getUserId (), pageable );
164- } else {
165- notifications = notificationService .getUserNotifications (userDetails .getUserId (), pageable );
166- }
49+ Page <Notification > notifications = notificationService .getNotifications (
50+ userDetails .getUserId (), pageable , unreadOnly
51+ );
16752
16853 long unreadCount = notificationService .getUnreadCount (userDetails .getUserId ());
16954
@@ -221,14 +106,4 @@ public ResponseEntity<RsData<Void>> markAllAsRead(
221106
222107 return ResponseEntity .ok (RsData .success ("전체 알림 읽음 처리 성공" ));
223108 }
224-
225- // ==================== 헬퍼 메서드 ====================
226-
227- private boolean isValidTargetType (String targetType ) {
228- return targetType != null &&
229- (targetType .equals ("USER" ) ||
230- targetType .equals ("ROOM" ) ||
231- targetType .equals ("COMMUNITY" ) ||
232- targetType .equals ("SYSTEM" ));
233- }
234109}
0 commit comments