Skip to content

Commit

Permalink
Merge pull request #156 from UnivApp/feature/notification-refactor8
Browse files Browse the repository at this point in the history
feat: 알림 중복 설정 예외처리
  • Loading branch information
nyeroni authored Oct 29, 2024
2 parents 4e500f3 + dc515d3 commit d18ed5b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,16 @@ public ResponseEntity<ErrorResponse> handleNotificationNotFoundException(Notific
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(DuplicateNotificationException.class)
public ResponseEntity<ErrorResponse> 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<ErrorResponse> handleInvalidRefreshTokenException(InvalidRefreshTokenException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<NotificationResponse> createNotification(@RequestBody @Valid CreateNotificationRequest request){

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
Notification findByEventAndMember(CalendarEvent calendarEvent, Member member);

List<Notification> findByMember(Member member);

boolean existsByMemberAndEvent(Member member, CalendarEvent calendarEvent);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit d18ed5b

Please sign in to comment.