Skip to content

Commit

Permalink
Merge pull request #189 from UnivApp/feature/nickname-refactor3
Browse files Browse the repository at this point in the history
feat: 닉네임 공백 허용 불가
  • Loading branch information
nyeroni authored Nov 20, 2024
2 parents 0ecc57c + d07119d commit 3326afb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -77,6 +77,16 @@ public ResponseEntity<ErrorResponse> handleMemberNicknameDuplicateException(Memb
return ResponseEntity.status(HttpStatus.CONFLICT).body(errorResponse);
}

@ExceptionHandler(InvalidNicknameException.class)
public ResponseEntity<ErrorResponse> 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<ErrorResponse> handleNotificationNotFoundException(NotificationNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -94,8 +104,9 @@ public ResponseEntity<ErrorResponse> 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<ErrorResponse> handleExistingNicknameException(ExistingNicknameException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -108,13 +119,14 @@ public ResponseEntity<ErrorResponse> handleExistingNicknameException(ExistingNic


@ExceptionHandler(NotificationDateOutOfRangeException.class)
public ResponseEntity<ErrorResponse> handleNotificationDateOutOfRangeException(NotificationDateOutOfRangeException ex) {
public ResponseEntity<ErrorResponse> 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)
Expand All @@ -126,6 +138,7 @@ public ResponseEntity<ErrorResponse> handleInvalidRefreshTokenException(InvalidR
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}

@ExceptionHandler(InvalidTokenException.class)
public ResponseEntity<ErrorResponse> handleInvalidTokenException(InvalidTokenException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -135,6 +148,7 @@ public ResponseEntity<ErrorResponse> handleInvalidTokenException(InvalidTokenExc
);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(errorResponse);
}

@ExceptionHandler(OAuthProcessingException.class)
public ResponseEntity<ErrorResponse> handleOAuthProcessingException(OAuthProcessingException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -144,6 +158,7 @@ public ResponseEntity<ErrorResponse> handleOAuthProcessingException(OAuthProcess
);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
}

@ExceptionHandler(ExpoCategoryNotFoundException.class)
public ResponseEntity<ErrorResponse> handleExpoCategoryNotFoundException(ExpoCategoryNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -153,6 +168,7 @@ public ResponseEntity<ErrorResponse> handleExpoCategoryNotFoundException(ExpoCat
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(ExpoNotFoundException.class)
public ResponseEntity<ErrorResponse> handleExpoNotFoundException(ExpoNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -172,6 +188,7 @@ public ResponseEntity<ErrorResponse> handleCompetitionRateNotFoundException(Comp
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(EmploymentRateNotFoundException.class)
public ResponseEntity<ErrorResponse> handleEmploymentRateNotFoundException(EmploymentRateNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -181,6 +198,7 @@ public ResponseEntity<ErrorResponse> handleEmploymentRateNotFoundException(Emplo
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(NewsNotFoundException.class)
public ResponseEntity<ErrorResponse> handleNewsNotFoundException(NewsNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -190,15 +208,18 @@ public ResponseEntity<ErrorResponse> handleNewsNotFoundException(NewsNotFoundExc
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(InvalidAuthorizationHeaderException.class)
public ResponseEntity<ErrorResponse> handleInvalidAuthorizationHeaderException(InvalidAuthorizationHeaderException ex) {
public ResponseEntity<ErrorResponse> handleInvalidAuthorizationHeaderException(
InvalidAuthorizationHeaderException ex) {
ErrorResponse errorResponse = new ErrorResponse(
ResponseCode.INVALID_AUTHORIZATION_HEADER.getCode(),
ResponseCode.INVALID_AUTHORIZATION_HEADER.getMessage(),
LocalDateTime.now().format(FORMATTER)
);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
}

@ExceptionHandler(CalendarEventNotFoundException.class)
public ResponseEntity<ErrorResponse> handleCalendarEventNotFoundException(CalendarEventNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -208,6 +229,7 @@ public ResponseEntity<ErrorResponse> handleCalendarEventNotFoundException(Calend
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(UniversityNotFoundException.class)
public ResponseEntity<ErrorResponse> handleUniversityNotFoundException(UniversityNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -217,6 +239,7 @@ public ResponseEntity<ErrorResponse> handleUniversityNotFoundException(Universit
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(RestaurantNotFoundException.class)
public ResponseEntity<ErrorResponse> handleRestaurantNotFoundException(RestaurantNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -236,6 +259,7 @@ public ResponseEntity<ErrorResponse> handleMatchingResultNotFoundException(Match
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(StarNotFoundException.class)
public ResponseEntity<ErrorResponse> handleStarNotFoundException(StarNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -245,6 +269,7 @@ public ResponseEntity<ErrorResponse> handleStarNotFoundException(StarNotFoundExc
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(DepartmentNotFoundException.class)
public ResponseEntity<ErrorResponse> handleDepartmentNotFoundException(DepartmentNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -254,6 +279,7 @@ public ResponseEntity<ErrorResponse> handleDepartmentNotFoundException(Departmen
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(TuitionFeeNotFoundException.class)
public ResponseEntity<ErrorResponse> handleDTuitionFeeNotFoundException(TuitionFeeNotFoundException ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand All @@ -263,6 +289,7 @@ public ResponseEntity<ErrorResponse> handleDTuitionFeeNotFoundException(TuitionF
);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorResponse);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception ex) {
ErrorResponse errorResponse = new ErrorResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", "이미 존재하는 알림입니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = "중복된 닉네임이 있음")

Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
12 changes: 11 additions & 1 deletion src/main/java/yerong/wedle/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,11 +23,19 @@ private boolean isNicknameDuplicate(String nickname) {
return memberRepository.existsByNickname(nickname);
}

private void validateNickname(String nickname) {
if (nickname == null || nickname.trim().isEmpty() || nickname.contains(" ")) {
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();
}
Expand Down Expand Up @@ -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);
Expand All @@ -82,4 +91,5 @@ public NicknameDuplicateResponse checkNicknameDuplicate(String nickname) {
.message(message)
.build();
}

}

0 comments on commit 3326afb

Please sign in to comment.