From 14826630bc20b518ea1a9e1516caf95212e4c4dd Mon Sep 17 00:00:00 2001 From: Sin Ye Rin <91180366+nyeroni@users.noreply.github.com> Date: Thu, 21 Nov 2024 01:44:56 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EA=B3=B5=EB=B0=B1=20=ED=97=88=EC=9A=A9=20=EB=B6=88=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/GlobalExceptionHandler.java | 43 +++++++++++++++---- .../wedle/common/exception/ResponseCode.java | 2 +- .../controller/MemberApiController.java | 14 +++--- .../exception/InvalidNicknameException.java | 11 +++++ .../wedle/member/service/MemberService.java | 12 +++++- 5 files changed, 67 insertions(+), 15 deletions(-) create mode 100644 src/main/java/yerong/wedle/member/exception/InvalidNicknameException.java diff --git a/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java b/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java index 3d717f9..99b7ee5 100644 --- a/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java +++ b/src/main/java/yerong/wedle/common/exception/GlobalExceptionHandler.java @@ -1,5 +1,7 @@ package yerong.wedle.common.exception; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; @@ -14,9 +16,10 @@ import yerong.wedle.department.exception.DepartmentNotFoundException; import yerong.wedle.employmentRate.exception.EmploymentRateNotFoundException; import yerong.wedle.member.exception.ExistingNicknameException; +import yerong.wedle.member.exception.InvalidNicknameException; +import yerong.wedle.member.exception.MemberDuplicateException; import yerong.wedle.member.exception.MemberNicknameDuplicateException; import yerong.wedle.member.exception.MemberNotFoundException; -import yerong.wedle.member.exception.MemberDuplicateException; import yerong.wedle.notification.exception.DuplicateNotificationException; import yerong.wedle.notification.exception.NotificationDateOutOfRangeException; import yerong.wedle.notification.exception.NotificationNotFoundException; @@ -28,9 +31,6 @@ import yerong.wedle.tuitionfee.exception.TuitionFeeNotFoundException; import yerong.wedle.university.exception.UniversityNotFoundException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; - @RestControllerAdvice public class GlobalExceptionHandler { @@ -77,6 +77,16 @@ public ResponseEntity handleMemberNicknameDuplicateException(Memb return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse); } + @ExceptionHandler(InvalidNicknameException.class) + public ResponseEntity handleInvalidNicknameException(InvalidNicknameException ex) { + ErrorResponse errorResponse = new ErrorResponse( + ResponseCode.NICKNAME_BLANK.getCode(), + ResponseCode.NICKNAME_BLANK.getMessage(), + LocalDateTime.now().format(FORMATTER) + ); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); + } + @ExceptionHandler(NotificationNotFoundException.class) public ResponseEntity handleNotificationNotFoundException(NotificationNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -94,8 +104,9 @@ public ResponseEntity handleDuplicateNotificationException(Duplic ResponseCode.DUPLICATION_NOTIFICATION.getMessage(), LocalDateTime.now().format(FORMATTER) ); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); + return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse); } + @ExceptionHandler(ExistingNicknameException.class) public ResponseEntity handleExistingNicknameException(ExistingNicknameException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -108,13 +119,14 @@ public ResponseEntity handleExistingNicknameException(ExistingNic @ExceptionHandler(NotificationDateOutOfRangeException.class) - public ResponseEntity handleNotificationDateOutOfRangeException(NotificationDateOutOfRangeException ex) { + public ResponseEntity handleNotificationDateOutOfRangeException( + NotificationDateOutOfRangeException ex) { ErrorResponse errorResponse = new ErrorResponse( ResponseCode.NOTIFICATION_DATE_OUT_OF_RANGE.getCode(), ResponseCode.NOTIFICATION_DATE_OUT_OF_RANGE.getMessage(), LocalDateTime.now().format(FORMATTER) ); - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); } @ExceptionHandler(InvalidRefreshTokenException.class) @@ -126,6 +138,7 @@ public ResponseEntity handleInvalidRefreshTokenException(InvalidR ); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); } + @ExceptionHandler(InvalidTokenException.class) public ResponseEntity handleInvalidTokenException(InvalidTokenException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -135,6 +148,7 @@ public ResponseEntity handleInvalidTokenException(InvalidTokenExc ); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse); } + @ExceptionHandler(OAuthProcessingException.class) public ResponseEntity handleOAuthProcessingException(OAuthProcessingException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -144,6 +158,7 @@ public ResponseEntity handleOAuthProcessingException(OAuthProcess ); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); } + @ExceptionHandler(ExpoCategoryNotFoundException.class) public ResponseEntity handleExpoCategoryNotFoundException(ExpoCategoryNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -153,6 +168,7 @@ public ResponseEntity handleExpoCategoryNotFoundException(ExpoCat ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(ExpoNotFoundException.class) public ResponseEntity handleExpoNotFoundException(ExpoNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -172,6 +188,7 @@ public ResponseEntity handleCompetitionRateNotFoundException(Comp ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(EmploymentRateNotFoundException.class) public ResponseEntity handleEmploymentRateNotFoundException(EmploymentRateNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -181,6 +198,7 @@ public ResponseEntity handleEmploymentRateNotFoundException(Emplo ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(NewsNotFoundException.class) public ResponseEntity handleNewsNotFoundException(NewsNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -190,8 +208,10 @@ public ResponseEntity handleNewsNotFoundException(NewsNotFoundExc ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(InvalidAuthorizationHeaderException.class) - public ResponseEntity handleInvalidAuthorizationHeaderException(InvalidAuthorizationHeaderException ex) { + public ResponseEntity handleInvalidAuthorizationHeaderException( + InvalidAuthorizationHeaderException ex) { ErrorResponse errorResponse = new ErrorResponse( ResponseCode.INVALID_AUTHORIZATION_HEADER.getCode(), ResponseCode.INVALID_AUTHORIZATION_HEADER.getMessage(), @@ -199,6 +219,7 @@ public ResponseEntity handleInvalidAuthorizationHeaderException(I ); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); } + @ExceptionHandler(CalendarEventNotFoundException.class) public ResponseEntity handleCalendarEventNotFoundException(CalendarEventNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -208,6 +229,7 @@ public ResponseEntity handleCalendarEventNotFoundException(Calend ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(UniversityNotFoundException.class) public ResponseEntity handleUniversityNotFoundException(UniversityNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -217,6 +239,7 @@ public ResponseEntity handleUniversityNotFoundException(Universit ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(RestaurantNotFoundException.class) public ResponseEntity handleRestaurantNotFoundException(RestaurantNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -236,6 +259,7 @@ public ResponseEntity handleMatchingResultNotFoundException(Match ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(StarNotFoundException.class) public ResponseEntity handleStarNotFoundException(StarNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -245,6 +269,7 @@ public ResponseEntity handleStarNotFoundException(StarNotFoundExc ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(DepartmentNotFoundException.class) public ResponseEntity handleDepartmentNotFoundException(DepartmentNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -254,6 +279,7 @@ public ResponseEntity handleDepartmentNotFoundException(Departmen ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(TuitionFeeNotFoundException.class) public ResponseEntity handleDTuitionFeeNotFoundException(TuitionFeeNotFoundException ex) { ErrorResponse errorResponse = new ErrorResponse( @@ -263,6 +289,7 @@ public ResponseEntity handleDTuitionFeeNotFoundException(TuitionF ); return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse); } + @ExceptionHandler(Exception.class) public ResponseEntity handleException(Exception 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 95f9354..c3b8773 100644 --- a/src/main/java/yerong/wedle/common/exception/ResponseCode.java +++ b/src/main/java/yerong/wedle/common/exception/ResponseCode.java @@ -12,7 +12,7 @@ public enum ResponseCode { MEMBER_DUPLICATE("409", "이미 존재하는 회원입니다."), MEMBER_NICKNAME_DUPLICATE("409", "이미 존재하는 닉네임입니다."), EXISTING_NICKNAME("400", "기존 닉네임과 동일합니다."), - + NICKNAME_BLANK("400", "닉네임에 공백을 포함할 수 없습니다."), // Notification NOTIFICATION_NOT_FOUND("404", "알림이 존재하지 않습니다."), DUPLICATION_NOTIFICATION("409", "이미 존재하는 알림입니다."), diff --git a/src/main/java/yerong/wedle/member/controller/MemberApiController.java b/src/main/java/yerong/wedle/member/controller/MemberApiController.java index c61174d..5c0d319 100644 --- a/src/main/java/yerong/wedle/member/controller/MemberApiController.java +++ b/src/main/java/yerong/wedle/member/controller/MemberApiController.java @@ -6,16 +6,18 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; -import yerong.wedle.department.dto.DepartmentResponse; -import yerong.wedle.department.service.DepartmentService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +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 yerong.wedle.member.dto.NicknameDuplicateResponse; import yerong.wedle.member.dto.NicknameRequest; import yerong.wedle.member.dto.NicknameResponse; import yerong.wedle.member.service.MemberService; -import java.util.List; - @Tag(name = "Member API", description = "회원 관련 API") @RequiredArgsConstructor @RestController @@ -27,6 +29,8 @@ public class MemberApiController { @Operation(summary = "회원 닉네임 등록", description = "신규 회원의 닉네임을 등록합니다") @ApiResponses({ @ApiResponse(responseCode = "200", description = "닉네임 등록 성공"), + @ApiResponse(responseCode = "400", description = "닉네임에 공백이 포함됨"), + @ApiResponse(responseCode = "400", description = "기존 닉네임과 동일함"), @ApiResponse(responseCode = "404", description = "회원을 찾을 수 없음"), @ApiResponse(responseCode = "409", description = "중복된 닉네임이 있음") diff --git a/src/main/java/yerong/wedle/member/exception/InvalidNicknameException.java b/src/main/java/yerong/wedle/member/exception/InvalidNicknameException.java new file mode 100644 index 0000000..7fc459b --- /dev/null +++ b/src/main/java/yerong/wedle/member/exception/InvalidNicknameException.java @@ -0,0 +1,11 @@ +package yerong.wedle.member.exception; + +import yerong.wedle.common.exception.CustomException; +import yerong.wedle.common.exception.ResponseCode; + +public class InvalidNicknameException extends CustomException { + public InvalidNicknameException() { + super(ResponseCode.NICKNAME_BLANK); + } + +} diff --git a/src/main/java/yerong/wedle/member/service/MemberService.java b/src/main/java/yerong/wedle/member/service/MemberService.java index 0c6c677..d46d51c 100644 --- a/src/main/java/yerong/wedle/member/service/MemberService.java +++ b/src/main/java/yerong/wedle/member/service/MemberService.java @@ -8,6 +8,7 @@ import yerong.wedle.member.dto.NicknameRequest; import yerong.wedle.member.dto.NicknameResponse; import yerong.wedle.member.exception.ExistingNicknameException; +import yerong.wedle.member.exception.InvalidNicknameException; import yerong.wedle.member.exception.MemberNicknameDuplicateException; import yerong.wedle.member.exception.MemberNotFoundException; import yerong.wedle.member.repository.MemberRepository; @@ -22,11 +23,19 @@ private boolean isNicknameDuplicate(String nickname) { return memberRepository.existsByNickname(nickname); } + private void validateNickname(String nickname) { + if (nickname == null || nickname.trim().isEmpty()) { + throw new InvalidNicknameException(); + } + } + public NicknameResponse setNickname(NicknameRequest nicknameRequest) { String socialId = getCurrentUserId(); Member member = memberRepository.findBySocialId(socialId) .orElseThrow(MemberNotFoundException::new); + validateNickname(nicknameRequest.getNickName()); + if (member.getNickname() != null && member.getNickname().equals(nicknameRequest.getNickName())) { throw new ExistingNicknameException(); } @@ -58,7 +67,7 @@ private String getCurrentUserId() { return socialId; } - public NicknameDuplicateResponse checkNicknameDuplicate(String nickname) { + public NicknameDuplicateResponse checkNicknameDuplicate(String nickname) { String socialId = getCurrentUserId(); Member member = memberRepository.findBySocialId(socialId) .orElseThrow(MemberNotFoundException::new); @@ -82,4 +91,5 @@ public NicknameDuplicateResponse checkNicknameDuplicate(String nickname) { .message(message) .build(); } + } From d07119d18a3b259f0c7fc1135e328e994aa4532e Mon Sep 17 00:00:00 2001 From: Sin Ye Rin <91180366+nyeroni@users.noreply.github.com> Date: Thu, 21 Nov 2024 01:50:16 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EA=B3=B5=EB=B0=B1=20=ED=97=88=EC=9A=A9=20=EB=B6=88=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/yerong/wedle/member/service/MemberService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/yerong/wedle/member/service/MemberService.java b/src/main/java/yerong/wedle/member/service/MemberService.java index d46d51c..afba644 100644 --- a/src/main/java/yerong/wedle/member/service/MemberService.java +++ b/src/main/java/yerong/wedle/member/service/MemberService.java @@ -24,7 +24,7 @@ private boolean isNicknameDuplicate(String nickname) { } private void validateNickname(String nickname) { - if (nickname == null || nickname.trim().isEmpty()) { + if (nickname == null || nickname.trim().isEmpty() || nickname.contains(" ")) { throw new InvalidNicknameException(); } }