Skip to content

Commit 100c95a

Browse files
authored
Merge pull request #117 from JA-yeong-eop-JA-moeu-JA/feat/#116-notifications-page
✨ Feat: 알림 목록 조회 API 페이징 추가
2 parents 7281cdd + e6540ea commit 100c95a

File tree

12 files changed

+78
-24
lines changed

12 files changed

+78
-24
lines changed

src/main/java/com/jajaja/domain/notification/controller/NotificationController.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.jajaja.domain.notification.controller;
22

3-
import com.jajaja.domain.notification.dto.NotificationResponseDto;
4-
import com.jajaja.domain.notification.dto.UnreadCountResponseDto;
3+
import com.jajaja.domain.notification.dto.response.NotificationResponseDto;
4+
import com.jajaja.domain.notification.dto.response.PagingNotificationResponseDto;
5+
import com.jajaja.domain.notification.dto.response.UnreadCountResponseDto;
56
import com.jajaja.domain.notification.repository.NotificationSseEmitterRepository;
67
import com.jajaja.domain.notification.service.NotificationService;
78
import com.jajaja.global.apiPayload.ApiResponse;
89
import com.jajaja.global.security.annotation.Auth;
910
import io.swagger.v3.oas.annotations.Operation;
11+
import io.swagger.v3.oas.annotations.Parameter;
1012
import lombok.RequiredArgsConstructor;
1113
import org.springframework.http.MediaType;
1214
import org.springframework.web.bind.annotation.*;
@@ -33,11 +35,19 @@ public SseEmitter subscribe(@Auth Long memberId) {
3335

3436
@Operation(
3537
summary = "알림 목록 조회 API | by 구름/윤윤지",
36-
description = "사용자의 알림 목록을 조회합니다."
38+
description = "사용자의 알림 목록을 페이징 처리하여 조회합니다."
3739
)
3840
@GetMapping
39-
public ApiResponse<List<NotificationResponseDto>> getNotifications(@Auth Long memberId) {
40-
return ApiResponse.onSuccess(notificationService.getNotifications(memberId));
41+
public ApiResponse<PagingNotificationResponseDto> getNotifications(
42+
@Auth Long memberId,
43+
44+
@Parameter(description = "페이지 번호 (0부터 시작)", example = "0")
45+
@RequestParam(defaultValue = "0") int page,
46+
47+
@Parameter(description = "페이지 크기", example = "10")
48+
@RequestParam(defaultValue = "10") int size
49+
) {
50+
return ApiResponse.onSuccess(notificationService.getNotifications(memberId, page, size));
4151
}
4252

4353
@Operation(

src/main/java/com/jajaja/domain/notification/dto/NotificationCreateRequestDto.java renamed to src/main/java/com/jajaja/domain/notification/dto/request/NotificationCreateRequestDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jajaja.domain.notification.dto;
1+
package com.jajaja.domain.notification.dto.request;
22

33
import com.jajaja.domain.notification.entity.enums.NotificationType;
44

src/main/java/com/jajaja/domain/notification/dto/NotificationResponseDto.java renamed to src/main/java/com/jajaja/domain/notification/dto/response/NotificationResponseDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jajaja.domain.notification.dto;
1+
package com.jajaja.domain.notification.dto.response;
22

33
import com.jajaja.domain.notification.entity.Notification;
44
import com.jajaja.domain.notification.entity.enums.NotificationType;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.jajaja.domain.notification.dto.response;
2+
3+
import com.jajaja.global.apiPayload.PageResponse;
4+
import lombok.Builder;
5+
import org.springframework.data.domain.Page;
6+
7+
import java.util.List;
8+
9+
@Builder
10+
public record PagingNotificationResponseDto(
11+
PageResponse page,
12+
List<NotificationResponseDto> notifications
13+
) {
14+
public static PagingNotificationResponseDto of(Page<?> page, List<NotificationResponseDto> notificationDtos) {
15+
return PagingNotificationResponseDto.builder()
16+
.page(PageResponse.from(page))
17+
.notifications(notificationDtos)
18+
.build();
19+
}
20+
}

src/main/java/com/jajaja/domain/notification/dto/UnreadCountResponseDto.java renamed to src/main/java/com/jajaja/domain/notification/dto/response/UnreadCountResponseDto.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.jajaja.domain.notification.dto;
1+
package com.jajaja.domain.notification.dto.response;
22

33
public record UnreadCountResponseDto(int unreadCount) {
44
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.jajaja.domain.notification.repository;
22

33
import com.jajaja.domain.notification.entity.Notification;
4+
import org.springframework.data.domain.Page;
45

56
import java.util.List;
67

78
public interface NotificationRepositoryCustom {
8-
List<Notification> findNotificationsByMemberId(Long memberId);
9+
Page<Notification> findPageByMemberId(Long memberId, int page, int size);
910
int markAllAsReadByMemberId(Long memberId);
1011
int countUnreadByMemberId(Long memberId);
1112
}

src/main/java/com/jajaja/domain/notification/repository/NotificationRepositoryImpl.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import com.jajaja.domain.notification.entity.QNotification;
55
import com.querydsl.jpa.impl.JPAQueryFactory;
66
import lombok.RequiredArgsConstructor;
7+
import org.springframework.data.domain.PageImpl;
8+
import org.springframework.data.domain.PageRequest;
79
import org.springframework.stereotype.Repository;
10+
import org.springframework.data.domain.Page;
811

912
import java.util.List;
13+
import java.util.Optional;
1014

1115
@Repository
1216
@RequiredArgsConstructor
@@ -15,14 +19,28 @@ public class NotificationRepositoryImpl implements NotificationRepositoryCustom
1519
private final JPAQueryFactory queryFactory;
1620

1721
@Override
18-
public List<Notification> findNotificationsByMemberId(Long memberId) {
22+
public Page<Notification> findPageByMemberId(Long memberId, int page, int size) {
1923
QNotification n = QNotification.notification;
20-
return queryFactory.selectFrom(n)
24+
25+
List<Notification> content = queryFactory
26+
.selectFrom(n)
2127
.where(n.member.id.eq(memberId))
22-
.orderBy(n.id.desc())
28+
.orderBy(n.createdAt.desc())
29+
.offset((long) page * size)
30+
.limit(size)
2331
.fetch();
32+
33+
long total = Optional.ofNullable(
34+
queryFactory.select(n.count())
35+
.from(n)
36+
.where(n.member.id.eq(memberId))
37+
.fetchOne()
38+
).orElse(0L);
39+
40+
return new PageImpl<>(content, PageRequest.of(page, size), total);
2441
}
2542

43+
2644
@Override
2745
public int markAllAsReadByMemberId(Long memberId) {
2846
QNotification n = QNotification.notification;

src/main/java/com/jajaja/domain/notification/service/NotificationService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.jajaja.domain.notification.service;
22

3-
import com.jajaja.domain.notification.dto.NotificationCreateRequestDto;
4-
import com.jajaja.domain.notification.dto.NotificationResponseDto;
5-
import com.jajaja.domain.notification.dto.UnreadCountResponseDto;
3+
import com.jajaja.domain.notification.dto.request.NotificationCreateRequestDto;
4+
import com.jajaja.domain.notification.dto.response.PagingNotificationResponseDto;
5+
import com.jajaja.domain.notification.dto.response.UnreadCountResponseDto;
66

77
import java.util.List;
88

99
public interface NotificationService {
1010
Long createNotification(NotificationCreateRequestDto requestDto);
11-
List<NotificationResponseDto> getNotifications(Long memberId);
11+
PagingNotificationResponseDto getNotifications(Long memberId, int page, int size);
1212
void markAsRead(Long notificationId, Long memberId);
1313
void markAllAsRead(Long memberId);
1414
UnreadCountResponseDto getUnreadCount(Long memberId);

src/main/java/com/jajaja/domain/notification/service/NotificationServiceImpl.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
import com.jajaja.domain.member.entity.Member;
44
import com.jajaja.domain.member.repository.MemberRepository;
5-
import com.jajaja.domain.notification.dto.NotificationCreateRequestDto;
6-
import com.jajaja.domain.notification.dto.NotificationResponseDto;
7-
import com.jajaja.domain.notification.dto.UnreadCountResponseDto;
5+
import com.jajaja.domain.notification.dto.request.NotificationCreateRequestDto;
6+
import com.jajaja.domain.notification.dto.response.NotificationResponseDto;
7+
import com.jajaja.domain.notification.dto.response.PagingNotificationResponseDto;
8+
import com.jajaja.domain.notification.dto.response.UnreadCountResponseDto;
89
import com.jajaja.domain.notification.entity.Notification;
910
import com.jajaja.domain.notification.repository.NotificationRepository;
1011
import com.jajaja.domain.notification.repository.NotificationSseEmitterRepository;
1112
import com.jajaja.global.apiPayload.code.status.ErrorStatus;
1213
import com.jajaja.global.apiPayload.exception.custom.BadRequestException;
1314
import lombok.RequiredArgsConstructor;
15+
import org.springframework.data.domain.Page;
1416
import org.springframework.security.access.AccessDeniedException;
1517
import org.springframework.stereotype.Service;
1618
import org.springframework.transaction.annotation.Transactional;
@@ -47,12 +49,15 @@ public Long createNotification(NotificationCreateRequestDto requestDto) {
4749
}
4850

4951
@Override
50-
public List<NotificationResponseDto> getNotifications(Long memberId) {
51-
return notificationRepository.findNotificationsByMemberId(memberId).stream()
52+
public PagingNotificationResponseDto getNotifications(Long memberId, int page, int size) {
53+
Page<Notification> notificationPage = notificationRepository.findPageByMemberId(memberId, page, size);
54+
List<NotificationResponseDto> dtos = notificationPage.getContent().stream()
5255
.map(NotificationResponseDto::from)
5356
.collect(Collectors.toList());
57+
return PagingNotificationResponseDto.of(notificationPage, dtos);
5458
}
5559

60+
5661
@Override
5762
@Transactional
5863
public void markAllAsRead(Long memberId) {

src/main/java/com/jajaja/domain/team/service/TeamCommonService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.jajaja.domain.team.service;
22

3-
import com.jajaja.domain.notification.dto.NotificationCreateRequestDto;
3+
import com.jajaja.domain.notification.dto.request.NotificationCreateRequestDto;
44
import com.jajaja.domain.notification.entity.enums.NotificationType;
55
import com.jajaja.domain.notification.service.NotificationService;
66
import com.jajaja.domain.team.entity.Team;

0 commit comments

Comments
 (0)