Skip to content

Commit 086ba29

Browse files
authored
Merge pull request #235 from prgrms-web-devcourse-final-project/feature/EA3-187-alarm-list-get
[EA3-187]Feature: 알림 전체 조회 엔드포인트 추가 및 읽음 여부 반환 추기
2 parents db6b06f + 793161d commit 086ba29

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed

src/main/java/grep/neogulcoder/domain/alram/controller/AlarmController.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ public class AlarmController implements AlarmSpecification {
2121
private final AlarmService alarmService;
2222

2323
@GetMapping("/my")
24-
public ApiResponse<List<AlarmResponse>> getAllAlarm(
25-
@AuthenticationPrincipal Principal userDetails) {
24+
public ApiResponse<List<AlarmResponse>> getAllAlarms(@AuthenticationPrincipal Principal userDetails) {
2625
return ApiResponse.success(alarmService.getAllAlarms(userDetails.getUserId()));
2726
}
2827

28+
@GetMapping("/unchecked/my")
29+
public ApiResponse<List<AlarmResponse>> getAllUncheckedAlarm(
30+
@AuthenticationPrincipal Principal userDetails) {
31+
return ApiResponse.success(alarmService.getAllUncheckedAlarms(userDetails.getUserId()));
32+
}
33+
2934
@PostMapping("/my/check/all")
3035
public ApiResponse<Void> checkAlarm(@AuthenticationPrincipal Principal userDetails) {
31-
alarmService.checkAllAlarm(userDetails.getUserId());
36+
alarmService.checkAllAlarmWithoutInvite(userDetails.getUserId());
3237
return ApiResponse.noContent();
3338
}
3439

src/main/java/grep/neogulcoder/domain/alram/controller/AlarmSpecification.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111
@Tag(name = "Alarm", description = "알림 관련 API 명세")
1212
public interface AlarmSpecification {
1313

14-
@Operation(summary = "내 알림 목록 조회", description = "로그인한 사용자의 알림 목록을 조회합니다.")
15-
ApiResponse<List<AlarmResponse>> getAllAlarm(@AuthenticationPrincipal Principal userDetails);
14+
@Operation(summary = "읽음 여부 관련 없이 내 전체 알림 목록 조회", description = "로그인한 사용자의 전체 알림 목록을 조회합니다.")
15+
ApiResponse<List<AlarmResponse>> getAllAlarms(@AuthenticationPrincipal Principal userDetails);
16+
17+
@Operation(summary = "내 읽지 않은 알림 목록 조회", description = "로그인한 사용자의 읽지 않은 알림 목록을 조회합니다.")
18+
ApiResponse<List<AlarmResponse>> getAllUncheckedAlarm(@AuthenticationPrincipal Principal userDetails);
1619

1720
@Operation(summary = "내 알림 전체 읽음 처리", description = "로그인한 사용자의 모든 알림을 읽음 처리합니다.")
1821
ApiResponse<Void> checkAlarm(@AuthenticationPrincipal Principal userDetails);

src/main/java/grep/neogulcoder/domain/alram/controller/dto/response/AlarmResponse.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package grep.neogulcoder.domain.alram.controller.dto.response;
22

3-
import grep.neogulcoder.domain.alram.entity.Alarm;
43
import grep.neogulcoder.domain.alram.type.AlarmType;
54
import grep.neogulcoder.domain.alram.type.DomainType;
65
import lombok.Builder;
@@ -21,26 +20,30 @@ public class AlarmResponse {
2120

2221
private String message;
2322

23+
private boolean checked;
24+
2425
public static AlarmResponse toResponse(Long id, Long receiverUserId, AlarmType alarmType, DomainType domainType,
25-
Long domainId, String message) {
26+
Long domainId, String message, boolean checked) {
2627
return AlarmResponse.builder()
27-
.id(id)
28-
.receiverUserId(receiverUserId)
29-
.alarmType(alarmType)
30-
.domainType(domainType)
31-
.domainId(domainId)
32-
.message(message)
33-
.build();
28+
.id(id)
29+
.receiverUserId(receiverUserId)
30+
.alarmType(alarmType)
31+
.domainType(domainType)
32+
.domainId(domainId)
33+
.message(message)
34+
.checked(checked)
35+
.build();
3436
}
3537

3638
@Builder
3739
private AlarmResponse(Long id, Long receiverUserId, AlarmType alarmType, DomainType domainType,
38-
Long domainId, String message) {
40+
Long domainId, String message, boolean checked) {
3941
this.id = id;
4042
this.receiverUserId = receiverUserId;
4143
this.alarmType = alarmType;
4244
this.domainType = domainType;
4345
this.domainId = domainId;
4446
this.message = message;
47+
this.checked = checked;
4548
}
4649
}

src/main/java/grep/neogulcoder/domain/alram/repository/AlarmRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
public interface AlarmRepository extends JpaRepository<Alarm, Long> {
88
List<Alarm> findAllByReceiverUserIdAndCheckedFalse(Long receiverUserId);
9+
List<Alarm> findAllByReceiverUserId(Long receiverUserId);
910

1011
}

src/main/java/grep/neogulcoder/domain/alram/service/AlarmService.java

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import grep.neogulcoder.global.exception.business.BusinessException;
2020
import grep.neogulcoder.global.exception.business.NotFoundException;
2121
import grep.neogulcoder.global.provider.finder.MessageFinder;
22+
2223
import java.util.List;
24+
2325
import lombok.RequiredArgsConstructor;
2426
import org.springframework.context.event.EventListener;
2527
import org.springframework.stereotype.Service;
@@ -45,33 +47,47 @@ public void saveAlarm(Long receiverId, AlarmType alarmType, DomainType domainTyp
4547
alarmRepository.save(Alarm.init(alarmType, receiverId, domainType, domainId, message));
4648
}
4749

48-
public List<AlarmResponse> getAllAlarms(Long receiverUserId) {
50+
public List<AlarmResponse> getAllUncheckedAlarms(Long receiverUserId) {
4951
return alarmRepository.findAllByReceiverUserIdAndCheckedFalse(receiverUserId).stream()
50-
.map(alarm -> AlarmResponse.toResponse(
51-
alarm.getId(),
52-
alarm.getReceiverUserId(),
53-
alarm.getAlarmType(),
54-
alarm.getDomainType(),
55-
alarm.getDomainId(),
56-
alarm.getMessage()))
57-
.toList();
52+
.map(alarm -> AlarmResponse.toResponse(
53+
alarm.getId(),
54+
alarm.getReceiverUserId(),
55+
alarm.getAlarmType(),
56+
alarm.getDomainType(),
57+
alarm.getDomainId(),
58+
alarm.getMessage(),
59+
alarm.isChecked()))
60+
.toList();
61+
}
62+
63+
public List<AlarmResponse> getAllAlarms(Long receiverUserId) {
64+
return alarmRepository.findAllByReceiverUserId(receiverUserId).stream()
65+
.map(alarm -> AlarmResponse.toResponse(
66+
alarm.getId(),
67+
alarm.getReceiverUserId(),
68+
alarm.getAlarmType(),
69+
alarm.getDomainType(),
70+
alarm.getDomainId(),
71+
alarm.getMessage(),
72+
alarm.isChecked()))
73+
.toList();
5874
}
5975

6076
@Transactional
61-
public void checkAllAlarm(Long receiverUserId) {
77+
public void checkAllAlarmWithoutInvite(Long receiverUserId) {
6278
List<Alarm> alarms = alarmRepository.findAllByReceiverUserIdAndCheckedFalse(receiverUserId);
6379
alarms.stream()
64-
.filter(alarm -> alarm.getAlarmType() != AlarmType.INVITE)
65-
.forEach(Alarm::checkAlarm);
80+
.filter(alarm -> alarm.getAlarmType() != AlarmType.INVITE)
81+
.forEach(Alarm::checkAlarm);
6682
}
6783

6884
@EventListener
6985
public void handleStudyInviteEvent(StudyInviteEvent event) {
7086
saveAlarm(
71-
event.targetUserId(),
72-
AlarmType.INVITE,
73-
DomainType.STUDY,
74-
event.studyId()
87+
event.targetUserId(),
88+
AlarmType.INVITE,
89+
DomainType.STUDY,
90+
event.studyId()
7591
);
7692
}
7793

@@ -83,7 +99,7 @@ public void acceptInvite(Long targetUserId, Long alarmId) {
8399
Alarm alarm = findValidAlarm(alarmId);
84100
Long studyId = alarm.getDomainId();
85101
Study study = findValidStudy(studyId);
86-
StudyMember.createMember(study,targetUserId);
102+
StudyMember.createMember(study, targetUserId);
87103
alarm.checkAlarm();
88104
}
89105

@@ -100,10 +116,10 @@ public void handleStudyExtendEvent(StudyExtendEvent event) {
100116
for (StudyMember member : members) {
101117
if (!member.isLeader()) {
102118
saveAlarm(
103-
member.getUserId(),
104-
AlarmType.STUDY_EXTEND,
105-
DomainType.STUDY,
106-
event.studyId()
119+
member.getUserId(),
120+
AlarmType.STUDY_EXTEND,
121+
DomainType.STUDY,
122+
event.studyId()
107123
);
108124
}
109125
}
@@ -112,13 +128,13 @@ public void handleStudyExtendEvent(StudyExtendEvent event) {
112128
@EventListener
113129
public void handleStudyExtensionReminderEvent(StudyExtensionReminderEvent event) {
114130
StudyMember leader = studyMemberRepository.findByStudyIdAndRoleAndActivatedTrue(event.studyId(), StudyMemberRole.LEADER)
115-
.orElseThrow(() -> new BusinessException(STUDY_LEADER_NOT_FOUND));
131+
.orElseThrow(() -> new BusinessException(STUDY_LEADER_NOT_FOUND));
116132

117133
saveAlarm(
118-
leader.getUserId(),
119-
AlarmType.STUDY_EXTENSION_REMINDER,
120-
DomainType.STUDY,
121-
event.studyId()
134+
leader.getUserId(),
135+
AlarmType.STUDY_EXTENSION_REMINDER,
136+
DomainType.STUDY,
137+
event.studyId()
122138
);
123139
}
124140

@@ -144,7 +160,7 @@ private Alarm findValidAlarm(Long alarmId) {
144160

145161
private Study findValidStudy(Long studyId) {
146162
return studyRepository.findById(studyId)
147-
.orElseThrow(() -> new NotFoundException(STUDY_NOT_FOUND));
163+
.orElseThrow(() -> new NotFoundException(STUDY_NOT_FOUND));
148164
}
149165

150166
private void validateParticipantStudyLimit(Long userId) {

0 commit comments

Comments
 (0)