Skip to content

Commit

Permalink
feat: 알림 리스트에 안 읽은 개수 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jihoon-jang committed Oct 26, 2023
1 parent b1c78fe commit 66efc43
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,7 +32,7 @@ public class NotificationController {
private final String longMaxValue = "9223372036854775807";

@GetMapping
public ResponseEntity<ApiResponse<NotificationResponse>> getNotifications(final @LoginId Long memberId,
public ResponseEntity<ApiResponse<NotificationResponseWithCount>> getNotifications(final @LoginId Long memberId,
@RequestParam(required = false, defaultValue = longMaxValue) final long startId) {
return ResponseEntity.ok(
ApiResponse.ok(
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ Long selectIdBySenderIdAndReceiverIdAndStatus(@Param("senderId") Long senderId,
void deleteByIdIn(List<Long> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
)));
}

Expand Down
6 changes: 4 additions & 2 deletions src/test/java/com/polzzak/support/NotificationFixtures.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 66efc43

Please sign in to comment.