diff --git a/src/main/java/com/api/ttoklip/domain/notification/controller/NotificationController.java b/src/main/java/com/api/ttoklip/domain/notification/controller/NotificationController.java index 50222a9f..38d55605 100644 --- a/src/main/java/com/api/ttoklip/domain/notification/controller/NotificationController.java +++ b/src/main/java/com/api/ttoklip/domain/notification/controller/NotificationController.java @@ -9,8 +9,11 @@ import com.api.ttoklip.global.success.Message; import com.api.ttoklip.global.success.SuccessResponse; import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -38,11 +41,14 @@ public SuccessResponse updateMemberFCMToken( @GetMapping("/my-notification") public SuccessResponse getNotification( - @RequestParam final String notificationCategory + @Parameter(description = "페이지 번호 (0부터 시작, 기본값 0)", example = "0") + @RequestParam(required = false, defaultValue = "0") final int page, + @Parameter(description = "페이지당 개수 (기본값 5)", example = "0") + @RequestParam(required = false, defaultValue = "5") final int size ) { - return new SuccessResponse<>( - notificationService.findNotificationByCategory(notificationCategory) - ); + Long currentMemberId = getCurrentMember().getId(); + Pageable pageRequest = PageRequest.of(page, size); + return new SuccessResponse<>(notificationService.findNotification(currentMemberId, pageRequest)); } } diff --git a/src/main/java/com/api/ttoklip/domain/notification/repository/NotificationRepositoryImpl.java b/src/main/java/com/api/ttoklip/domain/notification/repository/NotificationRepositoryImpl.java index 4bbf52ba..82fc0288 100644 --- a/src/main/java/com/api/ttoklip/domain/notification/repository/NotificationRepositoryImpl.java +++ b/src/main/java/com/api/ttoklip/domain/notification/repository/NotificationRepositoryImpl.java @@ -2,14 +2,11 @@ import static com.api.ttoklip.domain.member.domain.QMember.member; import static com.api.ttoklip.domain.notification.entity.QNotification.notification; -import static com.api.ttoklip.global.util.SecurityUtil.getCurrentMember; -import com.api.ttoklip.domain.notification.entity.NotiCategory; import com.api.ttoklip.domain.notification.entity.Notification; import com.querydsl.jpa.impl.JPAQueryFactory; import java.util.List; import lombok.RequiredArgsConstructor; -import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Repository; @@ -19,13 +16,12 @@ public class NotificationRepositoryImpl { private final JPAQueryFactory queryFactory; - private List findRecentNotifications(NotiCategory notiCategory, Pageable pageable) { + private List findRecentNotifications(final Long currentMemberId, final Pageable pageable) { return queryFactory .selectFrom(notification) .leftJoin(notification.member, member).fetchJoin() .where( - notification.notiCategory.eq(notiCategory), - notification.member.id.eq(getCurrentMember().getId()) + notification.member.id.eq(currentMemberId) ) .orderBy(notification.createdDate.desc()) .offset(pageable.getOffset()) @@ -33,7 +29,7 @@ private List findRecentNotifications(NotiCategory notiCategory, Pa .fetch(); } - public List findTop5RecentNotifications(NotiCategory notiCategory) { - return findRecentNotifications(notiCategory, PageRequest.of(0, 5)); + public List findTop5RecentNotifications(final Long currentMemberId, final Pageable pageRequest) { + return findRecentNotifications(currentMemberId, pageRequest); } } diff --git a/src/main/java/com/api/ttoklip/domain/notification/service/NotificationService.java b/src/main/java/com/api/ttoklip/domain/notification/service/NotificationService.java index 8bf095d4..8be122fd 100644 --- a/src/main/java/com/api/ttoklip/domain/notification/service/NotificationService.java +++ b/src/main/java/com/api/ttoklip/domain/notification/service/NotificationService.java @@ -3,6 +3,7 @@ import com.api.ttoklip.domain.common.comment.Comment; import com.api.ttoklip.domain.honeytip.post.domain.HoneyTip; import com.api.ttoklip.domain.member.domain.Member; +import com.api.ttoklip.domain.member.service.MemberService; import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponse; import com.api.ttoklip.domain.notification.dto.response.NotificationFrontResponses; import com.api.ttoklip.domain.notification.entity.NotiCategory; @@ -16,6 +17,7 @@ import java.util.List; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -26,6 +28,7 @@ public class NotificationService { private final NotificationRepository notificationRepository; private final NotificationRepositoryImpl notificationRepositoryImpl; + private final MemberService memberService; @Transactional public void register(final NotiCategory notiCategory, final Member member, final Long targetClassId, @@ -64,15 +67,19 @@ private String getTargetType(Object target) { throw new ApiException(ErrorType._BAD_CATEGORY_NOTIFICATION_TYPE); } + public NotificationFrontResponses findNotification(final Long currentMemberId, final Pageable pageRequest) { + Member currentMember = memberService.findById(currentMemberId); - public NotificationFrontResponses findNotificationByCategory(final String value) { - NotiCategory category = NotiCategory.getNotificationByCategory(value); - List top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications(category); + List top5RecentNotifications = notificationRepositoryImpl.findTop5RecentNotifications( + currentMember.getId(), pageRequest + ); List responses = top5RecentNotifications.stream() - .map(noti -> NotificationFrontResponse.of(noti.getId(), noti.getTargetIndex(), noti.getTargetType(), - noti.getTitle(), - noti.getText(), noti.isStatus())).toList(); + .map(noti -> NotificationFrontResponse.of( + noti.getId(), noti.getTargetIndex(), noti.getTargetType(), + noti.getTitle(), noti.getText(), noti.isStatus() + ) + ).toList(); return NotificationFrontResponses.from(responses); }