Skip to content

Commit

Permalink
Merge pull request #62 from Na-o-man/feature/#61/notification_swagger
Browse files Browse the repository at this point in the history
[FEAT] 알림 컨트롤러에 스웨거 추가
  • Loading branch information
bflykky authored Aug 8, 2024
2 parents 0accf70 + a45d817 commit 6e5439f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
import com.umc.naoman.domain.notification.dto.NotificationRequest;
import com.umc.naoman.domain.notification.dto.NotificationResponse;
import com.umc.naoman.domain.notification.entity.Notification;
import com.umc.naoman.domain.notification.service.FcmService;
import com.umc.naoman.domain.notification.service.NotificationService;
import com.umc.naoman.global.result.ResultResponse;
import com.umc.naoman.global.result.code.NotificationResultCode;
import com.umc.naoman.global.security.annotation.LoginMember;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -24,13 +29,25 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/notifications")
@Tag(name = "알림 관련 API", description = " 알림 전체 목록 조희, fcm 토큰 업로드, 알림 1개 삭제, 알림 전체 삭제, 읽지 않은 알림 유무, 내 알람 전체 읽음 처리를 하는 API입니다.")
public class NotificationController {
private final NotificationService notificationService;
private final FcmService fcmService;
private final NotificationConverter notificationConverter;

@PostMapping("/token")
@Operation(
summary = "fcm 토큰 업로드",
description = "안드로이드가 fcm 토큰을 서버에 업로드하는 API입니다.",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
content = @Content(
schema = @Schema(implementation = NotificationRequest.FcmToken.class)
)
)
)
public ResultResponse<Void> registerFcmToken(@RequestBody NotificationRequest.FcmToken fcmToken,
@LoginMember Member member){

fcmService.saveFcmToken(member, fcmToken.getToken());
return ResultResponse.of(NotificationResultCode.REGISTER_FCM_TOKEN,null);
}

Expand All @@ -39,39 +56,54 @@ public ResultResponse<Void> registerFcmToken(@RequestBody NotificationRequest.Fc
@Parameter(name = "page", description = "조회할 페이지를 입력해 주세요.(0번부터 시작)"),
@Parameter(name = "size", description = "한 페이지에 나타낼 알림 개수를 입력해주세요.")
})
public ResultResponse<NotificationResponse.PagedNotificationInfo> getNotifications(@LoginMember Member member,
@Operation(summary = "자신의 모든 알림 조회", description = "자신의 모든 알림을 조회하는 API입니다.")
public ResultResponse<NotificationResponse.PagedNotificationInfo> getNotificationList(@LoginMember Member member,
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC)
@Parameter(hidden = true) Pageable pageable){
Page<Notification> notificationPage = notificationService.getNotificationList(member, pageable);
return ResultResponse.of(NotificationResultCode.GET_MY_NOTIFICATION,
NotificationConverter.toNotificationInfo(notificationPage));
notificationConverter.toPagedNotificationInfo(notificationPage));
}

@GetMapping("/unread")
public ResultResponse<NotificationResponse.UnreadNotification> getIsUnread(@LoginMember Member member){
@Operation(summary = "읽지 않은 알림 유무 조회",
description = "읽지 않은 알림 유무 조회하는 API입니다.",
parameters = {})
public ResultResponse<NotificationResponse.IsUnread> getIsUnread(@LoginMember Member member){
List<Notification> notificationList = notificationService.isUnreadNotification(member);
return ResultResponse.of(NotificationResultCode.CHECK_MY_UNREAD_NOTIFICATION,
NotificationConverter.toUnreadNotification(notificationList));
notificationConverter.toIsUnread(notificationList));
}

@PostMapping("/acknowledgements")
@Operation(summary = "모든 알림 읽음 처리",
description = "자신의 모든 알림을 읽음 처리하는 API입니다.",
parameters = {})
public ResultResponse<NotificationResponse.NotificationAcknowledgeCount> setMyNotificationRead(@LoginMember Member member){
List<Notification> notificationList = notificationService.setMyNotificationRead(member);
return ResultResponse.of(NotificationResultCode.READ_ALL_MY_NOTIFICATION,
NotificationConverter.toNotificationAcknowledgedCount(notificationList));
notificationConverter.toNotificationAcknowledgedCount(notificationList));
}

@DeleteMapping("/{notificationId}")
@Operation(summary = "특정 알림 삭제",
description = "특정 알림을 삭제하는 API입니다.",
parameters = {
@Parameter(name = "notificationId", description = "삭제할 알림의 Id", required = true, schema = @Schema(type = "long"))
})
public ResultResponse<NotificationResponse.NotificationAcknowledgeCount> deleteNotification(@PathVariable Long notificationId,
@LoginMember Member member){
long deletedCount = notificationService.deleteNotification(member,notificationId);
return ResultResponse.of(NotificationResultCode.DELETE_MY_NOTIFICATION,
NotificationConverter.toNotificationAcknowledgedCount(deletedCount));
notificationConverter.toNotificationAcknowledgedCount(deletedCount));
}
@DeleteMapping
@Operation(summary = "모든 알림 삭제",
description = "자신의 모든 알림을 삭제하는 API입니다.",
parameters = {})
public ResultResponse<NotificationResponse.NotificationDeletedCount> deleteAllNotification(@LoginMember Member member){
long deletedCount = notificationService.deleteNotificationAll(member);
return ResultResponse.of(NotificationResultCode.DELETE_MY_NOTIFICATION,
NotificationConverter.toNotificationDeletedCount(deletedCount));
notificationConverter.toNotificationDeletedCount(deletedCount));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
import com.umc.naoman.domain.notification.entity.DeviceToken;
import com.umc.naoman.domain.notification.entity.Notification;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class NotificationConverter {

public static NotificationResponse.NotificationInfo notificationInfo(Notification notification){
public NotificationResponse.NotificationInfo toNotificationInfo(Notification notification){
return NotificationResponse.NotificationInfo.builder()
.body(notification.getMessage())
.createdAt(notification.getCreatedAt())
.isChecked(notification.isChecked())
.url(notification.makeNotificationInfoURL()) //다형성으로 각기 다른 알림이 적절한 URL 만들도록 오버라이딩.
.build();
}
public static NotificationResponse.PagedNotificationInfo toNotificationInfo(
public NotificationResponse.PagedNotificationInfo toPagedNotificationInfo(
Page<Notification> notificationList){
List<NotificationResponse.NotificationInfo> notificationInfoList
= notificationList.stream().map(NotificationConverter::notificationInfo).collect(Collectors.toList());
= notificationList.stream().map(this::toNotificationInfo).collect(Collectors.toList());

return NotificationResponse.PagedNotificationInfo.builder()
.isLast(notificationList.isLast())
Expand All @@ -34,30 +36,30 @@ public static NotificationResponse.PagedNotificationInfo toNotificationInfo(
.build();
}

public static NotificationResponse.UnreadNotification toUnreadNotification(List<Notification> notificationList){
return NotificationResponse.UnreadNotification.builder()
public NotificationResponse.IsUnread toIsUnread(List<Notification> notificationList){
return NotificationResponse.IsUnread.builder()
.isUnread(!notificationList.isEmpty())
.build();
}

public static NotificationResponse.NotificationAcknowledgeCount toNotificationAcknowledgedCount(List<Notification> notificationList){
public NotificationResponse.NotificationAcknowledgeCount toNotificationAcknowledgedCount(List<Notification> notificationList){
return NotificationResponse.NotificationAcknowledgeCount.builder()
.acknowledgedCount((long)notificationList.size())
.build();
}
public static NotificationResponse.NotificationAcknowledgeCount toNotificationAcknowledgedCount(Long updateCount){
public NotificationResponse.NotificationAcknowledgeCount toNotificationAcknowledgedCount(Long updateCount){
return NotificationResponse.NotificationAcknowledgeCount.builder()
.acknowledgedCount(updateCount)
.build();
}

public static NotificationResponse.NotificationDeletedCount toNotificationDeletedCount(Long updateCount){
public NotificationResponse.NotificationDeletedCount toNotificationDeletedCount(Long updateCount){
return NotificationResponse.NotificationDeletedCount.builder()
.deletedCount(updateCount)
.build();
}

public static DeviceToken toDeviceToken(Member member, NotificationRequest.FcmToken fcmToken){
public DeviceToken toDeviceToken(Member member, NotificationRequest.FcmToken fcmToken){
return DeviceToken.builder()
.member(member)
.fcmToken(fcmToken.getToken())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static class NotificationInfo {
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class UnreadNotification {
public static class IsUnread {
Boolean isUnread;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.umc.naoman.domain.notification.service;

import com.umc.naoman.domain.member.entity.Member;

public interface FcmService {
void saveFcmToken(Long memberId, String fcmToken);
void saveFcmToken(Member member, String fcmToken);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.umc.naoman.domain.notification.service;

import com.umc.naoman.domain.member.entity.Member;
import com.umc.naoman.domain.notification.entity.DeviceToken;
import com.umc.naoman.domain.notification.repository.FcmTokenRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,10 +14,9 @@ public class FcmServiceImpl implements FcmService{
private final FcmTokenRepository fcmTokenRepository;

@Override
public void saveFcmToken(Long memberId, String fcmToken) {
//멤버 리파지토리 개발 후 수정해야됨
public void saveFcmToken(Member member, String fcmToken) {
DeviceToken deviceToken = DeviceToken.builder()
.member(null)
.member(member)
.fcmToken(fcmToken)
.build();
fcmTokenRepository.save(deviceToken);
Expand Down

0 comments on commit 6e5439f

Please sign in to comment.