Skip to content

Commit 9a0b68c

Browse files
authored
refactor : 알림 카테고리화 (#394)
1 parent 8880f49 commit 9a0b68c

File tree

16 files changed

+97
-40
lines changed

16 files changed

+97
-40
lines changed

src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.gamzabat.algohub.feature.notice.repository.NoticeRepository;
6060
import com.gamzabat.algohub.feature.notification.domain.NotificationSetting;
6161
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
62+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
6263
import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository;
6364
import com.gamzabat.algohub.feature.notification.service.NotificationService;
6465
import com.gamzabat.algohub.feature.problem.domain.Problem;
@@ -619,7 +620,8 @@ private void sendNewMemberNotification(StudyGroup studyGroup, GroupMember newMem
619620
members,
620621
null, null,
621622
NotificationCategory.NEW_MEMBER_JOINED,
622-
NotificationCategory.NEW_MEMBER_JOINED.getMessage(newMember.getUser().getNickname())
623+
NotificationCategory.NEW_MEMBER_JOINED.getMessage(newMember.getUser().getNickname()),
624+
NotificationType.STUDY_GROUP
623625
);
624626
}
625627

src/main/java/com/gamzabat/algohub/feature/notification/controller/NotificationController.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.springframework.web.bind.annotation.RequestBody;
1414
import org.springframework.web.bind.annotation.RequestHeader;
1515
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.RequestParam;
1617
import org.springframework.web.bind.annotation.RestController;
1718
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
1819

@@ -21,6 +22,8 @@
2122
import com.gamzabat.algohub.feature.notification.dto.EditNotificationSettingRequest;
2223
import com.gamzabat.algohub.feature.notification.dto.GetNotificationResponse;
2324
import com.gamzabat.algohub.feature.notification.dto.GetNotificationSettingResponse;
25+
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
26+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
2427
import com.gamzabat.algohub.feature.notification.service.NotificationService;
2528
import com.gamzabat.algohub.feature.notification.service.NotificationSettingService;
2629
import com.gamzabat.algohub.feature.user.domain.User;
@@ -47,8 +50,9 @@ public SseEmitter streamNotifications(@AuthedUser User user,
4750

4851
@GetMapping
4952
@Operation(summary = "알림 목록 조회 API")
50-
public ResponseEntity<List<GetNotificationResponse>> getNotifications(@AuthedUser User user) {
51-
return ResponseEntity.ok().body(notificationService.getNotifications(user));
53+
public ResponseEntity<List<GetNotificationResponse>> getNotifications(@AuthedUser User user, @RequestParam(required = false)
54+
NotificationType type) {
55+
return ResponseEntity.ok().body(notificationService.getNotifications(user, type));
5256
}
5357

5458
@PatchMapping

src/main/java/com/gamzabat/algohub/feature/notification/domain/Notification.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
import java.time.LocalDateTime;
44

55
import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
6+
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
7+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
68
import com.gamzabat.algohub.feature.problem.domain.Problem;
79
import com.gamzabat.algohub.feature.solution.domain.Solution;
810
import com.gamzabat.algohub.feature.user.domain.User;
911

12+
import jakarta.persistence.Column;
1013
import jakarta.persistence.Entity;
14+
import jakarta.persistence.EnumType;
15+
import jakarta.persistence.Enumerated;
1116
import jakarta.persistence.FetchType;
1217
import jakarta.persistence.GeneratedValue;
1318
import jakarta.persistence.GenerationType;
@@ -39,13 +44,16 @@ public class Notification {
3944
@JoinColumn(name = "solution_id")
4045
private Solution solution;
4146
private String message;
47+
@Enumerated(EnumType.STRING)
48+
@Column(nullable = false)
49+
private NotificationType type;
4250
private boolean isRead;
4351
private String subContent;
4452
private LocalDateTime createdAt;
4553

4654
@Builder
4755
public Notification(User user, StudyGroup studyGroup, Problem problem, Solution solution, String message,
48-
boolean isRead, String subContent) {
56+
boolean isRead, String subContent, NotificationType type) {
4957
this.user = user;
5058
this.studyGroup = studyGroup;
5159
this.problem = problem;
@@ -54,6 +62,7 @@ public Notification(User user, StudyGroup studyGroup, Problem problem, Solution
5462
this.isRead = isRead;
5563
this.subContent = subContent;
5664
this.createdAt = LocalDateTime.now();
65+
this.type = type;
5766
}
5867

5968
public void updateIsRead() {
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package com.gamzabat.algohub.feature.notification.enums;
22

33
public enum NotificationCategory {
4-
PROBLEM_STARTED("[%s] 문제가 시작되었습니다! 지금 도전해보세요!"),
5-
NEW_SOLUTION_POSTED("%s님이 새로운 풀이를 등록했습니다! 풀이를 확인하고 의견을 나눠보세요."),
6-
NEW_MEMBER_JOINED("%s님이 스터디에 합류했습니다!"),
7-
NEW_COMMENT_POSTED("%s님이 내 풀이에 코멘트를 남겼습니다! 어떤 리뷰인지 확인해보세요."),
8-
PROBLEM_DEADLINE_REACHED("[%s] 문제의 마감이 오늘입니다! 아직 해결하지 못했다면 지금 도전해보세요!");
94

5+
PROBLEM_STARTED(NotificationType.PROBLEM, "[%s] 문제가 시작되었습니다! 지금 도전해보세요!"),
6+
PROBLEM_DEADLINE_REACHED(NotificationType.PROBLEM, "[%s] 문제의 마감이 오늘입니다! 아직 해결하지 못했다면 지금 도전해보세요!"),
7+
8+
NEW_SOLUTION_POSTED(NotificationType.STUDY_GROUP, "%s님이 새로운 풀이를 등록했습니다! 풀이를 확인하고 의견을 나눠보세요."),
9+
NEW_MEMBER_JOINED(NotificationType.STUDY_GROUP, "%s님이 스터디에 합류했습니다!"),
10+
NEW_MEMBER_REQUESTED(NotificationType.STUDY_GROUP, "%s님이 스터디에 합류를 요청했습니다!"),
11+
NEW_GROUP_JOINED(NotificationType.STUDY_GROUP, "[%s] 스터디 가입 요청이 승인되었습니다."),
12+
13+
NEW_COMMENT_POSTED(NotificationType.COMMENT, "%s님이 내 풀이에 코멘트를 남겼습니다! 어떤 리뷰인지 확인해보세요.");
14+
15+
private final NotificationType type;
1016
private final String message;
1117

12-
NotificationCategory(String message) {
18+
NotificationCategory(NotificationType type, String message) {
19+
this.type = type;
1320
this.message = message;
1421
}
1522

23+
public NotificationType getType() { return type; }
24+
1625
public String getMessage(Object... args) {
1726
return String.format(message, args);
1827
}
19-
}
28+
29+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.gamzabat.algohub.feature.notification.enums;
2+
3+
public enum NotificationType {
4+
5+
PROBLEM, STUDY_GROUP, COMMENT
6+
7+
}

src/main/java/com/gamzabat/algohub/feature/notification/repository/NotificationRepository.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@
88

99
import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup;
1010
import com.gamzabat.algohub.feature.notification.domain.Notification;
11+
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
12+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
1113
import com.gamzabat.algohub.feature.problem.domain.Problem;
1214
import com.gamzabat.algohub.feature.user.domain.User;
1315

1416
public interface NotificationRepository extends JpaRepository<Notification, Long> {
1517
List<Notification> findAllByUser(User user);
1618

19+
List<Notification> findAllByUserAndType(User user, NotificationType type);
20+
1721
List<Notification> findAllByUserAndIsRead(User user, boolean isRead);
1822

1923
@Modifying

src/main/java/com/gamzabat/algohub/feature/notification/service/NotificationService.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.gamzabat.algohub.feature.notification.domain.NotificationSetting;
2323
import com.gamzabat.algohub.feature.notification.dto.GetNotificationResponse;
2424
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
25+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
2526
import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationException;
2627
import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException;
2728
import com.gamzabat.algohub.feature.notification.exception.NotificationValidationException;
@@ -102,8 +103,8 @@ private void sendLostData(String lastEventId, String email, String emitterId, Ss
102103

103104
@Transactional
104105
public void send(String receiver, String message, StudyGroup studyGroup, Problem problem, Solution solution,
105-
String subContent) {
106-
Notification notification = createNotification(receiver, message, studyGroup, subContent, problem, solution);
106+
String subContent, NotificationType type) {
107+
Notification notification = createNotification(receiver, message, studyGroup, subContent, problem, solution, type);
107108
notificationRepository.save(notification);
108109
Map<String, SseEmitter> sseEmitter = emitterRepository.findAllEmitterStartWithByEmail(receiver);
109110
sseEmitter.forEach(
@@ -115,14 +116,14 @@ public void send(String receiver, String message, StudyGroup studyGroup, Problem
115116
}
116117

117118
private void sendList(List receiverList, String message, StudyGroup studyGroup, String subContent, Problem problem,
118-
Solution solution) {
119+
Solution solution, NotificationType type) {
119120
List<Notification> notifications = new ArrayList<>();
120121
Map<String, SseEmitter> sseEmitters;
121122
for (int i = 0; i < receiverList.size(); i++) {
122123
int finalI = i;
123124
sseEmitters = new HashMap<>();
124125
Notification notification = createNotification(receiverList.get(i).toString(), message, studyGroup,
125-
subContent, problem, solution);
126+
subContent, problem, solution, type);
126127
notifications.add(notification);
127128
notificationRepository.save(notification);
128129
sseEmitters.putAll(emitterRepository.findAllEmitterStartWithByEmail(receiverList.get(i).toString()));
@@ -136,7 +137,7 @@ private void sendList(List receiverList, String message, StudyGroup studyGroup,
136137
}
137138

138139
private Notification createNotification(String receiver, String message, StudyGroup studyGroup, String subContent,
139-
Problem problem, Solution solution) {
140+
Problem problem, Solution solution, NotificationType type) {
140141
return Notification.builder()
141142
.user(
142143
userRepository.findByEmail(receiver).orElseThrow(() -> new UserValidationException("존재 하지 않는 회원 입니다.")))
@@ -146,6 +147,7 @@ private Notification createNotification(String receiver, String message, StudyGr
146147
.solution(solution)
147148
.subContent(subContent)
148149
.isRead(false)
150+
.type(type)
149151
.build();
150152
}
151153

@@ -162,8 +164,14 @@ private void sendToClient(SseEmitter emitter, String id, Notification notificati
162164
}
163165

164166
@Transactional(readOnly = true)
165-
public List<GetNotificationResponse> getNotifications(User user) {
166-
List<Notification> notifications = notificationRepository.findAllByUser(user);
167+
public List<GetNotificationResponse> getNotifications(User user, NotificationType type) {
168+
List<Notification> notifications;
169+
if (type == null) {
170+
notifications = notificationRepository.findAllByUser(user);
171+
} else {
172+
notifications = notificationRepository.findAllByUserAndType(user, type);
173+
174+
}
167175
notifications.sort(Comparator.comparingLong(Notification::getId).reversed());
168176
return notifications.stream().map(GetNotificationResponse::toDTO).toList();
169177
}
@@ -188,7 +196,7 @@ public void readNotification(User user, Long notificationId) {
188196
@Transactional
189197
public void sendNotificationToMembers(StudyGroup group, List<GroupMember> receiver, Problem problem,
190198
Solution solution,
191-
NotificationCategory category, String message) {
199+
NotificationCategory category, String message, NotificationType type) {
192200
List<String> users = new ArrayList<>();
193201
for (GroupMember member : receiver) {
194202
NotificationSetting setting = notificationSettingRepository.findByMember(member)
@@ -203,7 +211,7 @@ public void sendNotificationToMembers(StudyGroup group, List<GroupMember> receiv
203211
}
204212

205213
try {
206-
sendList(users, message, group, null, problem, solution);
214+
sendList(users, message, group, null, problem, solution, type);
207215
} catch (Exception e) {
208216
log.warn("failed to send notification", e);
209217
}
@@ -216,6 +224,8 @@ private boolean isSettingOn(NotificationSetting setting, NotificationCategory ca
216224
case NotificationCategory.NEW_COMMENT_POSTED -> setting.isNewComment();
217225
case NotificationCategory.NEW_MEMBER_JOINED -> setting.isNewMember();
218226
case NotificationCategory.NEW_SOLUTION_POSTED -> setting.isNewSolution();
227+
case NotificationCategory.NEW_MEMBER_REQUESTED -> setting.isNewMember();
228+
case NotificationCategory.NEW_GROUP_JOINED -> setting.isNewMember();
219229
};
220230
}
221231

src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
3131
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
3232
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
33+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
3334
import com.gamzabat.algohub.feature.notification.repository.NotificationRepository;
3435
import com.gamzabat.algohub.feature.notification.service.NotificationService;
3536
import com.gamzabat.algohub.feature.problem.domain.Problem;
@@ -91,7 +92,8 @@ public void createProblem(User user, Long groupId, CreateProblemRequest request)
9192
problem,
9293
null,
9394
NotificationCategory.PROBLEM_STARTED,
94-
NotificationCategory.PROBLEM_STARTED.getMessage(title)
95+
NotificationCategory.PROBLEM_STARTED.getMessage(title),
96+
NotificationType.PROBLEM
9597
);
9698

9799
log.info("success to create problem user_id={} , group_id = {}", user.getId(), groupId);
@@ -322,7 +324,8 @@ private void notifyProblemStartsToday(LocalDate now) {
322324
problem,
323325
null,
324326
NotificationCategory.PROBLEM_STARTED,
325-
NotificationCategory.PROBLEM_STARTED.getMessage(problem.getTitle())
327+
NotificationCategory.PROBLEM_STARTED.getMessage(problem.getTitle()),
328+
NotificationType.PROBLEM
326329
);
327330
}
328331
}
@@ -336,7 +339,8 @@ private void notifyProblemEndsToday(LocalDate now) {
336339
problem,
337340
null,
338341
NotificationCategory.PROBLEM_DEADLINE_REACHED,
339-
NotificationCategory.PROBLEM_DEADLINE_REACHED.getMessage(problem.getTitle())
342+
NotificationCategory.PROBLEM_DEADLINE_REACHED.getMessage(problem.getTitle()),
343+
NotificationType.PROBLEM
340344
);
341345
}
342346
}

src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionCommentService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
2020
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
2121
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
22+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
2223
import com.gamzabat.algohub.feature.notification.service.NotificationService;
2324
import com.gamzabat.algohub.feature.problem.domain.Problem;
2425
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
@@ -74,7 +75,8 @@ private void sendCommentNotification(User commenter, Solution solution) {
7475
null,
7576
solution,
7677
NotificationCategory.NEW_COMMENT_POSTED,
77-
NotificationCategory.NEW_COMMENT_POSTED.getMessage(commenter.getNickname())
78+
NotificationCategory.NEW_COMMENT_POSTED.getMessage(commenter.getNickname()),
79+
NotificationType.COMMENT
7880
);
7981
}
8082

src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionService.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
3030
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
3131
import com.gamzabat.algohub.feature.notification.enums.NotificationCategory;
32+
import com.gamzabat.algohub.feature.notification.enums.NotificationType;
3233
import com.gamzabat.algohub.feature.notification.service.NotificationService;
3334
import com.gamzabat.algohub.feature.problem.domain.Problem;
3435
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
@@ -256,7 +257,8 @@ private void sendNewSolutionNotification(StudyGroup group, GroupMember solver, P
256257
problem,
257258
null,
258259
NotificationCategory.NEW_SOLUTION_POSTED,
259-
NotificationCategory.NEW_SOLUTION_POSTED.getMessage(solver.getUser().getNickname())
260+
NotificationCategory.NEW_SOLUTION_POSTED.getMessage(solver.getUser().getNickname()),
261+
NotificationType.STUDY_GROUP
260262
);
261263
}
262264

0 commit comments

Comments
 (0)