This repository was archived by the owner on Jan 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNotificationQueryServiceImpl.java
More file actions
117 lines (98 loc) · 5.17 KB
/
NotificationQueryServiceImpl.java
File metadata and controls
117 lines (98 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.example.spot.service.notification;
import com.example.spot.api.code.status.ErrorStatus;
import com.example.spot.api.exception.GeneralException;
import com.example.spot.domain.Notification;
import com.example.spot.domain.enums.NotifyType;
import com.example.spot.web.dto.notification.NotificationResponseDTO.NotificationListDTO;
import com.example.spot.web.dto.notification.NotificationResponseDTO.NotificationListDTO.NotificationDTO;
import com.example.spot.web.dto.notification.NotificationResponseDTO.StudyNotificationListDTO;
import com.example.spot.web.dto.notification.NotificationResponseDTO.StudyNotificationListDTO.StudyNotificationDTO;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.example.spot.repository.NotificationRepository;
import org.springframework.transaction.annotation.Transactional;
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class NotificationQueryServiceImpl implements NotificationQueryService {
private final NotificationRepository notificationRepository;
/**
* 회원이 참가 신청한 스터디에 대한 알림을 조회합니다.
* @param memberId 사용자 ID
* @param pageable 페이징 정보
* @return 참가 신청한 스터디에 대한 알림 목록
* @throws GeneralException 알림이 존재하지 않는 경우
*/
@Override
public StudyNotificationListDTO getAllAppliedStudyNotification(Long memberId, Pageable pageable) {
// 특정 회원이 참가 신청한 스터디에 대한 알림 조회
List<Notification> notifications = notificationRepository.findByMemberIdAndTypeAndIsChecked(
memberId, pageable, NotifyType.STUDY_APPLY, false);
// 알림이 존재하지 않는 경우
if (notifications.isEmpty())
throw new GeneralException(ErrorStatus._NOTIFICATION_NOT_FOUND);
// DTO를 담을 리스트 생성
List<StudyNotificationDTO> notificationDTOs = new ArrayList<>();
// 알림을 DTO로 변환하여 리스트에 추가
notifications.forEach(notification -> {
StudyNotificationDTO notificationDTO = StudyNotificationDTO.builder()
.notificationId(notification.getId())
.studyId(notification.getStudy().getId())
.createdAt(notification.getCreatedAt())
.type(notification.getType())
.studyTitle(notification.getStudy().getTitle())
.studyProfileImage(notification.getStudy().getProfileImage())
.isChecked(notification.getIsChecked())
.build();
notificationDTOs.add(notificationDTO);
});
// DTO 리스트를 반환
return StudyNotificationListDTO.builder()
.notifications(notificationDTOs)
.totalNotificationCount((long) notificationDTOs.size())
.uncheckedNotificationCount((long) (int) notificationDTOs.stream().filter(notificationDTO -> !notificationDTO.getIsChecked()).count())
.build();
}
/**
* 회원에게 생성된 알림을 전체 조회합니다.
* @param memberId 사용자 ID
* @param pageable 페이징 정보
* @return 알림 목록
* @throws GeneralException 알림이 존재하지 않는 경우
*/
@Override
public NotificationListDTO getAllNotifications(Long memberId, Pageable pageable) {
// 특정 회원에게 생성된 알림을 조회
List<Notification> notifications = notificationRepository.findByMemberIdAndTypeNot(
memberId, pageable, NotifyType.STUDY_APPLY);
// 알림이 존재하지 않는 경우
if (notifications.isEmpty())
throw new GeneralException(ErrorStatus._NOTIFICATION_NOT_FOUND);
// DTO를 담을 리스트 생성
List<NotificationDTO> notificationDTOs = new ArrayList<>();
// 알림을 DTO로 변환하여 리스트에 추가
notifications.forEach(notification -> {
NotificationDTO notificationDTO = NotificationDTO.builder()
.notificationId(notification.getId())
.createdAt(notification.getCreatedAt())
.type(notification.getType())
.studyId(notification.getStudy().getId())
.studyPostId(notification.getStudyPostId())
.studyTitle(notification.getStudy().getTitle())
.studyProfileImage(notification.getStudy().getProfileImage())
.notifierName(notification.getNotifierName()) // 알림 생성한 회원 이름
.isChecked(notification.getIsChecked())
.build();
notificationDTOs.add(notificationDTO);
});
// DTO 리스트를 반환
return NotificationListDTO.builder()
.notifications(notificationDTOs)
.totalNotificationCount((long) notificationDTOs.size())
.uncheckedNotificationCount((long) (int) notificationDTOs.stream().filter(notificationDTO -> !notificationDTO.getIsChecked()).count())
.build();
}
}