diff --git a/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java b/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java index 6f388be..b312c44 100644 --- a/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java @@ -15,6 +15,7 @@ import yerong.wedle.employmentRate.exception.EmploymentRateNotFoundException; import yerong.wedle.member.exception.MemberNotFoundException; import yerong.wedle.member.exception.MemberDuplicateException; +import yerong.wedle.notification.exception.DuplicateNotificationException; import yerong.wedle.notification.exception.NotificationNotFoundException; import yerong.wedle.oauth.exception.InvalidAuthorizationHeaderException; import yerong.wedle.oauth.exception.InvalidRefreshTokenException; @@ -73,6 +74,16 @@ public ResponseEntity handleNotificationNotFoundException(Notific return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(DuplicateNotificationException.class) + public ResponseEntity handleDuplicateNotificationException(DuplicateNotificationException ex) { + ErrorResponse errorResponse = new ErrorResponse( + ResponseCode.DUPLICATION_NOTIFICATION.getCode(), + ResponseCode.DUPLICATION_NOTIFICATION.getMessage(), + LocalDateTime.now().format(FORMATTER) + ); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); + } + @ExceptionHandler(InvalidRefreshTokenException.class) public ResponseEntity handleInvalidRefreshTokenException(InvalidRefreshTokenException ex) { ErrorResponse errorResponse = new ErrorResponse( diff --git a/src/main/java/yerong/wedle/common/exception/ResponseCode.java b/src/main/java/yerong/wedle/common/exception/ResponseCode.java index dc3b819..af9eec0 100644 --- a/src/main/java/yerong/wedle/common/exception/ResponseCode.java +++ b/src/main/java/yerong/wedle/common/exception/ResponseCode.java @@ -11,8 +11,9 @@ public enum ResponseCode { MEMBER_NOT_FOUND("404", "회원이 존재하지 않습니다."), MEMBER_DUPLICATE("409", "이미 존재하는 회원입니다."), - // Member + // Notification NOTIFICATION_NOT_FOUND("404", "알림이 존재하지 않습니다."), + DUPLICATION_NOTIFICATION("409", "이미 존재하는 알림입니다."), // OAuth INVALID_REFRESH_TOKEN("400", "유효하지 않은 Refresh Token입니다."), diff --git a/src/main/java/yerong/wedle/notification/controller/NotificationApiController.java b/src/main/java/yerong/wedle/notification/controller/NotificationApiController.java index 8679cbe..0b51100 100644 --- a/src/main/java/yerong/wedle/notification/controller/NotificationApiController.java +++ b/src/main/java/yerong/wedle/notification/controller/NotificationApiController.java @@ -22,9 +22,9 @@ public class NotificationApiController { @Operation(summary = "알림 생성", description = "이벤트에 대한 새로운 알림을 생성합니다.") @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "알림이 성공적으로 생성되었습니다."), - @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터") + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터"), + @ApiResponse(responseCode = "409", description = "이미 존재하는 알림입니다.") }) - @PostMapping public ResponseEntity createNotification(@RequestBody @Valid CreateNotificationRequest request){ diff --git a/src/main/java/yerong/wedle/notification/exception/DuplicateNotificationException.java b/src/main/java/yerong/wedle/notification/exception/DuplicateNotificationException.java new file mode 100644 index 0000000..4671205 --- /dev/null +++ b/src/main/java/yerong/wedle/notification/exception/DuplicateNotificationException.java @@ -0,0 +1,10 @@ +package yerong.wedle.notification.exception; + +import yerong.wedle.common.exception.CustomException; +import yerong.wedle.common.exception.ResponseCode; + +public class DuplicateNotificationException extends CustomException { + public DuplicateNotificationException () { + super(ResponseCode.DUPLICATION_NOTIFICATION); + } +} diff --git a/src/main/java/yerong/wedle/notification/repository/NotificationRepository.java b/src/main/java/yerong/wedle/notification/repository/NotificationRepository.java index cec8d66..cf9b3c1 100644 --- a/src/main/java/yerong/wedle/notification/repository/NotificationRepository.java +++ b/src/main/java/yerong/wedle/notification/repository/NotificationRepository.java @@ -16,4 +16,6 @@ public interface NotificationRepository extends JpaRepository findByMember(Member member); + + boolean existsByMemberAndEvent(Member member, CalendarEvent calendarEvent); } diff --git a/src/main/java/yerong/wedle/notification/service/NotificationService.java b/src/main/java/yerong/wedle/notification/service/NotificationService.java index ff80c98..e0f5ca4 100644 --- a/src/main/java/yerong/wedle/notification/service/NotificationService.java +++ b/src/main/java/yerong/wedle/notification/service/NotificationService.java @@ -20,6 +20,7 @@ import yerong.wedle.notification.domain.Notification; import yerong.wedle.notification.dto.CreateNotificationRequest; import yerong.wedle.notification.dto.NotificationResponse; +import yerong.wedle.notification.exception.DuplicateNotificationException; import yerong.wedle.notification.repository.NotificationRepository; @Slf4j @@ -39,6 +40,10 @@ public NotificationResponse createNotification(CreateNotificationRequest request CalendarEvent calendarEvent = getCalendarEventById(request.getEventId()); + if (notificationRepository.existsByMemberAndEvent(member, calendarEvent)) { + throw new DuplicateNotificationException(); + } + Notification notification = Notification.builder() .notificationDate(request.getNotificationDate()) .event(calendarEvent)