From 66efc43d068bf62fdd9a1630adc32861e5114d0b Mon Sep 17 00:00:00 2001 From: jihoon-jang Date: Fri, 27 Oct 2023 01:33:17 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=95=8C=EB=A6=BC=20=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=97=90=20=EC=95=88=20=EC=9D=BD=EC=9D=80=20=EA=B0=9C?= =?UTF-8?q?=EC=88=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/NotificationController.java | 4 +-- .../dto/NotificationResponseWithCount.java | 8 +++++ .../repository/NotificationRepository.java | 6 ++++ .../service/NotificationService.java | 7 ++-- .../NotificationControllerTest.java | 34 +++++++++++-------- .../polzzak/support/NotificationFixtures.java | 6 ++-- 6 files changed, 43 insertions(+), 22 deletions(-) create mode 100644 src/main/java/com/polzzak/domain/notification/dto/NotificationResponseWithCount.java diff --git a/src/main/java/com/polzzak/domain/notification/controller/NotificationController.java b/src/main/java/com/polzzak/domain/notification/controller/NotificationController.java index a27ac27..17ccaba 100644 --- a/src/main/java/com/polzzak/domain/notification/controller/NotificationController.java +++ b/src/main/java/com/polzzak/domain/notification/controller/NotificationController.java @@ -12,7 +12,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; -import com.polzzak.domain.notification.dto.NotificationResponse; +import com.polzzak.domain.notification.dto.NotificationResponseWithCount; import com.polzzak.domain.notification.dto.NotificationSettingDto; import com.polzzak.domain.notification.dto.UpdateNotificationSetting; import com.polzzak.domain.notification.service.NotificationService; @@ -32,7 +32,7 @@ public class NotificationController { private final String longMaxValue = "9223372036854775807"; @GetMapping - public ResponseEntity> getNotifications(final @LoginId Long memberId, + public ResponseEntity> getNotifications(final @LoginId Long memberId, @RequestParam(required = false, defaultValue = longMaxValue) final long startId) { return ResponseEntity.ok( ApiResponse.ok( diff --git a/src/main/java/com/polzzak/domain/notification/dto/NotificationResponseWithCount.java b/src/main/java/com/polzzak/domain/notification/dto/NotificationResponseWithCount.java new file mode 100644 index 0000000..9eb79ec --- /dev/null +++ b/src/main/java/com/polzzak/domain/notification/dto/NotificationResponseWithCount.java @@ -0,0 +1,8 @@ +package com.polzzak.domain.notification.dto; + +public record NotificationResponseWithCount(NotificationResponse response, int unreadNotificationCount) { + + public static NotificationResponseWithCount from(NotificationResponse response, int unreadNotificationCount) { + return new NotificationResponseWithCount(response, unreadNotificationCount); + } +} diff --git a/src/main/java/com/polzzak/domain/notification/repository/NotificationRepository.java b/src/main/java/com/polzzak/domain/notification/repository/NotificationRepository.java index 2a8d160..95188eb 100644 --- a/src/main/java/com/polzzak/domain/notification/repository/NotificationRepository.java +++ b/src/main/java/com/polzzak/domain/notification/repository/NotificationRepository.java @@ -35,4 +35,10 @@ Long selectIdBySenderIdAndReceiverIdAndStatus(@Param("senderId") Long senderId, void deleteByIdIn(List ids); void deleteByReceiver(Member receiver); + + @Query(nativeQuery = true, value = """ + SELECT COUNT(1) + FROM notification n + WHERE n.receiver_id = :receiverId AND `status` = 'UNREAD'""") + int countByStatusIsUnRead(@Param("receiverId") Long receiverId); } diff --git a/src/main/java/com/polzzak/domain/notification/service/NotificationService.java b/src/main/java/com/polzzak/domain/notification/service/NotificationService.java index b5f0e21..c4db70e 100644 --- a/src/main/java/com/polzzak/domain/notification/service/NotificationService.java +++ b/src/main/java/com/polzzak/domain/notification/service/NotificationService.java @@ -13,6 +13,7 @@ import com.polzzak.domain.notification.dto.MemberDtoForNotification; import com.polzzak.domain.notification.dto.NotificationDto; import com.polzzak.domain.notification.dto.NotificationResponse; +import com.polzzak.domain.notification.dto.NotificationResponseWithCount; import com.polzzak.domain.notification.dto.NotificationSettingDto; import com.polzzak.domain.notification.dto.UpdateNotificationSetting; import com.polzzak.domain.notification.entity.Notification; @@ -57,7 +58,7 @@ public Notification addNotification(final Long senderId, final Long receiverId, } @Transactional - public NotificationResponse getNotificationsAndChangeStatus(final Long memberId, final int size, + public NotificationResponseWithCount getNotificationsAndChangeStatus(final Long memberId, final int size, final long startId) { NotificationResponse notificationResponse = getNotificationResponse(memberId, size, startId); @@ -66,8 +67,8 @@ public NotificationResponse getNotificationsAndChangeStatus(final Long memberId, .map(NotificationDto::id) .toList(); notificationRepository.updateStatusByIds(notificationIds, Notification.Status.READ); - - return notificationResponse; + int count = notificationRepository.countByStatusIsUnRead(memberId); + return NotificationResponseWithCount.from(notificationResponse, count); } @Transactional diff --git a/src/test/java/com/polzzak/domain/notification/controller/NotificationControllerTest.java b/src/test/java/com/polzzak/domain/notification/controller/NotificationControllerTest.java index 1bcf0c7..28aad3e 100644 --- a/src/test/java/com/polzzak/domain/notification/controller/NotificationControllerTest.java +++ b/src/test/java/com/polzzak/domain/notification/controller/NotificationControllerTest.java @@ -46,7 +46,7 @@ public void setup() { @DisplayName("알림 목록 조회 테스트") void getNotificationsTest() throws Exception { when(notificationService.getNotificationsAndChangeStatus(anyLong(), anyInt(), anyLong())).thenReturn( - NotificationFixtures.NOTIFICATION_RESPONSE); + NotificationFixtures.NOTIFICATION_RESPONSE_WITH_COUNT); mockMvc.perform( get(BASE_URL) @@ -67,20 +67,24 @@ void getNotificationsTest() throws Exception { fieldWithPath("code").description("응답 코드"), fieldWithPath("messages").description("응답 메시지"), fieldWithPath("data").description("응답 데이터"), - fieldWithPath("data.startId").description("다음 조회 ID (null이면 끝)").optional(), - fieldWithPath("data.notificationDtoList[]").description("알림 목록"), - fieldWithPath("data.notificationDtoList[].id").description("알림 ID").optional(), - fieldWithPath("data.notificationDtoList[].type").description("알림 타").optional(), - fieldWithPath("data.notificationDtoList[].status").description("알림 상태").optional(), - fieldWithPath("data.notificationDtoList[].title").description("알림 제목").optional(), - fieldWithPath("data.notificationDtoList[].message").description("알림 내용").optional(), - fieldWithPath("data.notificationDtoList[].sender").description("전송자 정보").optional(), - fieldWithPath("data.notificationDtoList[].sender.id").description("전송자 ID").optional(), - fieldWithPath("data.notificationDtoList[].sender.nickname").description("전송자 닉네임").optional(), - fieldWithPath("data.notificationDtoList[].sender.profileUrl").description("전송자 이미지").optional(), - fieldWithPath("data.notificationDtoList[].link").description("알림 링크").optional(), - fieldWithPath("data.notificationDtoList[].requestFamilyId").description("연동 요청 ID").optional(), - fieldWithPath("data.notificationDtoList[].createdDate").description("알림 생성 시간").optional() + fieldWithPath("data.response.startId").description("다음 조회 ID (null이면 끝)").optional(), + fieldWithPath("data.response.notificationDtoList[]").description("알림 목록"), + fieldWithPath("data.response.notificationDtoList[].id").description("알림 ID").optional(), + fieldWithPath("data.response.notificationDtoList[].type").description("알림 타").optional(), + fieldWithPath("data.response.notificationDtoList[].status").description("알림 상태").optional(), + fieldWithPath("data.response.notificationDtoList[].title").description("알림 제목").optional(), + fieldWithPath("data.response.notificationDtoList[].message").description("알림 내용").optional(), + fieldWithPath("data.response.notificationDtoList[].sender").description("전송자 정보").optional(), + fieldWithPath("data.response.notificationDtoList[].sender.id").description("전송자 ID").optional(), + fieldWithPath("data.response.notificationDtoList[].sender.nickname").description("전송자 닉네임") + .optional(), + fieldWithPath("data.response.notificationDtoList[].sender.profileUrl").description("전송자 이미지") + .optional(), + fieldWithPath("data.response.notificationDtoList[].link").description("알림 링크").optional(), + fieldWithPath("data.response.notificationDtoList[].requestFamilyId").description("연동 요청 ID") + .optional(), + fieldWithPath("data.response.notificationDtoList[].createdDate").description("알림 생성 시간").optional(), + fieldWithPath("data.unreadNotificationCount").description("남은 알림 수").optional() ))); } diff --git a/src/test/java/com/polzzak/support/NotificationFixtures.java b/src/test/java/com/polzzak/support/NotificationFixtures.java index cdf501e..b946e60 100644 --- a/src/test/java/com/polzzak/support/NotificationFixtures.java +++ b/src/test/java/com/polzzak/support/NotificationFixtures.java @@ -6,6 +6,7 @@ import com.polzzak.domain.notification.dto.MemberDtoForNotification; import com.polzzak.domain.notification.dto.NotificationDto; import com.polzzak.domain.notification.dto.NotificationResponse; +import com.polzzak.domain.notification.dto.NotificationResponseWithCount; import com.polzzak.domain.notification.dto.NotificationSettingDto; import com.polzzak.domain.notification.dto.UpdateNotificationSetting; import com.polzzak.domain.notification.entity.Notification; @@ -18,10 +19,11 @@ public class NotificationFixtures { new NotificationDto(1, NotificationType.CREATED_STAMP_BOARD, Notification.Status.UNREAD, "title1", "message1", MEMBER1, "link1", 22L, LocalDateTime.now()), new NotificationDto(1, NotificationType.ISSUED_COUPON, Notification.Status.READ, "title2", "message2", MEMBER2, - "link2", 23L, LocalDateTime.now()) - ); + "link2", 23L, LocalDateTime.now())); public static final NotificationResponse NOTIFICATION_RESPONSE = new NotificationResponse(null, NOTIFICATION_DTO_LIST); + public static final NotificationResponseWithCount NOTIFICATION_RESPONSE_WITH_COUNT = + new NotificationResponseWithCount(NOTIFICATION_RESPONSE, 12); public static final NotificationSettingDto NOTIFICATION_SETTING_DTO = new NotificationSettingDto(true, true, true, false, false, true, true, false, true, true);