Skip to content

Commit b3c71aa

Browse files
authored
fix: 알림 종류 구분 없이 알림 내역 페이징 처리 (#241) (#242)
1 parent a792222 commit b3c71aa

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

src/main/java/com/api/ttoklip/domain/notification/controller/NotificationController.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99
import com.api.ttoklip.global.success.Message;
1010
import com.api.ttoklip.global.success.SuccessResponse;
1111
import io.swagger.v3.oas.annotations.Operation;
12+
import io.swagger.v3.oas.annotations.Parameter;
1213
import jakarta.validation.Valid;
1314
import lombok.RequiredArgsConstructor;
15+
import org.springframework.data.domain.PageRequest;
16+
import org.springframework.data.domain.Pageable;
1417
import org.springframework.web.bind.annotation.GetMapping;
1518
import org.springframework.web.bind.annotation.PatchMapping;
1619
import org.springframework.web.bind.annotation.RequestBody;
@@ -38,11 +41,14 @@ public SuccessResponse<Message> updateMemberFCMToken(
3841

3942
@GetMapping("/my-notification")
4043
public SuccessResponse<NotificationFrontResponses> getNotification(
41-
@RequestParam final String notificationCategory
44+
@Parameter(description = "페이지 번호 (0부터 시작, 기본값 0)", example = "0")
45+
@RequestParam(required = false, defaultValue = "0") final int page,
46+
@Parameter(description = "페이지당 개수 (기본값 5)", example = "0")
47+
@RequestParam(required = false, defaultValue = "5") final int size
4248
) {
43-
return new SuccessResponse<>(
44-
notificationService.findNotificationByCategory(notificationCategory)
45-
);
49+
Long currentMemberId = getCurrentMember().getId();
50+
Pageable pageRequest = PageRequest.of(page, size);
51+
return new SuccessResponse<>(notificationService.findNotification(currentMemberId, pageRequest));
4652
}
4753

4854
}

src/main/java/com/api/ttoklip/domain/notification/repository/NotificationRepositoryImpl.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22

33
import static com.api.ttoklip.domain.member.domain.QMember.member;
44
import static com.api.ttoklip.domain.notification.entity.QNotification.notification;
5-
import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember;
65

7-
import com.api.ttoklip.domain.notification.entity.NotiCategory;
86
import com.api.ttoklip.domain.notification.entity.Notification;
97
import com.querydsl.jpa.impl.JPAQueryFactory;
108
import java.util.List;
119
import lombok.RequiredArgsConstructor;
12-
import org.springframework.data.domain.PageRequest;
1310
import org.springframework.data.domain.Pageable;
1411
import org.springframework.stereotype.Repository;
1512

@@ -19,21 +16,20 @@ public class NotificationRepositoryImpl {
1916

2017
private final JPAQueryFactory queryFactory;
2118

22-
private List<Notification> findRecentNotifications(NotiCategory notiCategory, Pageable pageable) {
19+
private List<Notification> findRecentNotifications(final Long currentMemberId, final Pageable pageable) {
2320
return queryFactory
2421
.selectFrom(notification)
2522
.leftJoin(notification.member, member).fetchJoin()
2623
.where(
27-
notification.notiCategory.eq(notiCategory),
28-
notification.member.id.eq(getCurrentMember().getId())
24+
notification.member.id.eq(currentMemberId)
2925
)
3026
.orderBy(notification.createdDate.desc())
3127
.offset(pageable.getOffset())
3228
.limit(pageable.getPageSize())
3329
.fetch();
3430
}
3531

36-
public List<Notification> findTop5RecentNotifications(NotiCategory notiCategory) {
37-
return findRecentNotifications(notiCategory, PageRequest.of(0, 5));
32+
public List<Notification> findTop5RecentNotifications(final Long currentMemberId, final Pageable pageRequest) {
33+
return findRecentNotifications(currentMemberId, pageRequest);
3834
}
3935
}

src/main/java/com/api/ttoklip/domain/notification/service/NotificationService.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.api.ttoklip.domain.common.comment.Comment;
44
import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip;
55
import com.api.ttoklip.domain.member.domain.Member;
6+
import com.api.ttoklip.domain.member.service.MemberService;
67
import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponse;
78
import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponses;
89
import com.api.ttoklip.domain.notification.entity.NotiCategory;
@@ -16,6 +17,7 @@
1617
import java.util.List;
1718
import lombok.RequiredArgsConstructor;
1819
import lombok.extern.slf4j.Slf4j;
20+
import org.springframework.data.domain.Pageable;
1921
import org.springframework.stereotype.Service;
2022
import org.springframework.transaction.annotation.Transactional;
2123

@@ -26,6 +28,7 @@ public class NotificationService {
2628

2729
private final NotificationRepository notificationRepository;
2830
private final NotificationRepositoryImpl notificationRepositoryImpl;
31+
private final MemberService memberService;
2932

3033
@Transactional
3134
public void register(final NotiCategory notiCategory, final Member member, final Long targetClassId,
@@ -64,15 +67,19 @@ private String getTargetType(Object target) {
6467
throw new ApiException(ErrorType._BAD_CATEGORY_NOTIFICATION_TYPE);
6568
}
6669

70+
public NotificationFrontResponses findNotification(final Long currentMemberId, final Pageable pageRequest) {
71+
Member currentMember = memberService.findById(currentMemberId);
6772

68-
public NotificationFrontResponses findNotificationByCategory(final String value) {
69-
NotiCategory category = NotiCategory.getNotificationByCategory(value);
70-
List<Notification> top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications(category);
73+
List<Notification> top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications(
74+
currentMember.getId(), pageRequest
75+
);
7176

7277
List<NotificationFrontResponse> responses = top5RecentNotifications.stream()
73-
.map(noti -> NotificationFrontResponse.of(noti.getId(), noti.getTargetIndex(), noti.getTargetType(),
74-
noti.getTitle(),
75-
noti.getText(), noti.isStatus())).toList();
78+
.map(noti -> NotificationFrontResponse.of(
79+
noti.getId(), noti.getTargetIndex(), noti.getTargetType(),
80+
noti.getTitle(), noti.getText(), noti.isStatus()
81+
)
82+
).toList();
7683

7784
return NotificationFrontResponses.from(responses);
7885
}

0 commit comments

Comments
 (0)