Skip to content

Commit

Permalink
feat: 알림 읽기 apI 추가
Browse files Browse the repository at this point in the history
1
  • Loading branch information
jihoon-jang committed Jan 30, 2024
1 parent 66efc43 commit acb2f2d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,12 @@ operation::notification/settings-get-success[snippets='http-request,request-head

operation::notification/settings-update-success[snippets='http-request,request-headers,request-fields,http-response']

== *5. 알림 읽기 API*

=== *1-1* 성공

operation::notification/notification-read-success[snippets='http-request,request-headers,request-fields,http-response,response-fields']

[[푸시-API]]
== *1. 푸시 토큰 전송 API*

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.polzzak.domain.notification.dto.NotificationResponseWithCount;
import com.polzzak.domain.notification.dto.NotificationSettingDto;
import com.polzzak.domain.notification.dto.ReadNotificationId;
import com.polzzak.domain.notification.dto.UpdateNotificationSetting;
import com.polzzak.domain.notification.entity.Notification;
import com.polzzak.domain.notification.service.NotificationService;
import com.polzzak.global.common.ApiResponse;
import com.polzzak.global.security.LoginId;
Expand Down Expand Up @@ -47,6 +50,14 @@ public ResponseEntity<ApiResponse<Void>> deleteNotifications(
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@PostMapping("/read")
public ResponseEntity<ApiResponse<Integer>> readNotification(
final @LoginId Long memberId,
final @RequestBody ReadNotificationId readNotificationId) {
notificationService.changeNotificationStatus(readNotificationId.notificationId(), Notification.Status.READ);
return ResponseEntity.ok(ApiResponse.ok(notificationService.getUnreadNotificationCount(memberId)));
}

@GetMapping("/settings")
public ResponseEntity<ApiResponse<NotificationSettingDto>> getNotificationSettings(final @LoginId Long memberId) {
return ResponseEntity.ok(ApiResponse.ok(notificationService.getNotificationSetting(memberId)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.polzzak.domain.notification.dto;

public record ReadNotificationId(long notificationId) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Slice<Notification> findNotificationsByReceiverIdAndIdLessThan(final long member
@Query("UPDATE Notification n SET n.status = :status WHERE n.id IN :ids")
void updateStatusByIds(@Param("ids") List<Long> ids, @Param("status") Notification.Status status);

@Modifying
@Query("UPDATE Notification n SET n.status = :status WHERE n.id = :id")
void updateStatusById(@Param("id") long id, @Param("status") Notification.Status status);

@Query(nativeQuery = true, value = """
SELECT n.id
FROM notification n
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,22 @@ public NotificationResponseWithCount getNotificationsAndChangeStatus(final Long
return NotificationResponseWithCount.from(notificationResponse, count);
}

public int getUnreadNotificationCount(final long memberId) {
return notificationRepository.countByStatusIsUnRead(memberId);
}

@Transactional
public void changeRequestNotificationStatus(final Long senderId, final Long receiverId,
final Notification.Status status) {
Long notificationId = notificationRepository.selectIdBySenderIdAndReceiverIdAndStatus(senderId, receiverId);
notificationRepository.updateStatusByIds(List.of(notificationId), status);
}

@Transactional
public void changeNotificationStatus(final long notificationId, final Notification.Status status) {
notificationRepository.updateStatusById(notificationId, status);
}

@Transactional
public void deleteNotifications(final List<Long> notificationIds) {
List<Notification> notifications = notificationRepository.findByIdIn(notificationIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

import com.polzzak.domain.notification.dto.ReadNotificationId;
import com.polzzak.domain.notification.service.NotificationService;
import com.polzzak.domain.user.service.UserService;
import com.polzzak.support.NotificationFixtures;
Expand Down Expand Up @@ -109,6 +110,33 @@ void deleteNotificationTest() throws Exception {
)));
}

@Test
@DisplayName("알림 읽기 테스트")
void readNotificationTest() throws Exception {
doNothing().when(notificationService).changeNotificationStatus(anyLong(), any());
when(notificationService.getUnreadNotificationCount(anyLong())).thenReturn(12);

mockMvc.perform(
post(BASE_URL + "/read")
.header(HttpHeaders.AUTHORIZATION, TOKEN_TYPE + USER_ACCESS_TOKEN)
.contentType(MediaType.APPLICATION_JSON)
.content(objectToString(new ReadNotificationId(45))))
.andDo(print())
.andExpect(status().isOk())
.andDo(document("notification/notification-read-success",
requestHeaders(
headerWithName(HttpHeaders.AUTHORIZATION).description("엑세스 토큰")
),
requestFields(
fieldWithPath("notificationId").description("읽을 알림 id")
),
responseFields(
fieldWithPath("code").description("응답 코드"),
fieldWithPath("messages").description("응답 메시지"),
fieldWithPath("data").description("남은 안 읽은 알림 수")
)));
}

@Test
@DisplayName("알림 설정 조회 테스트")
void getNotificationSettings() throws Exception {
Expand Down

0 comments on commit acb2f2d

Please sign in to comment.