-
Notifications
You must be signed in to change notification settings - Fork 1
[FEAT] 닉네임 중복 방지 API 구현 (#321) #322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cbe8f4c
feat(#321): 닉네임 중복 api
lingard1234 f204d16
Merge branch 'dev' into feat/321-nickdup
lingard1234 c4fad93
feat(#321): 닉네임 중복 api 공용으로
lingard1234 cde050f
Merge branch 'dev' into feat/321-nickdup
lingard1234 e2bb378
feat(#321): 코드스탙일
lingard1234 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/main/java/com/example/RealMatch/user/application/util/NicknameValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package com.example.RealMatch.user.application.util; | ||
|
|
||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import com.example.RealMatch.global.exception.CustomException; | ||
| import com.example.RealMatch.user.domain.repository.UserRepository; | ||
| import com.example.RealMatch.user.presentation.code.UserErrorCode; | ||
|
|
||
| import lombok.RequiredArgsConstructor; | ||
| /** | ||
| * 닉네임 검증을 위한 공통 유틸리티 클래스 | ||
| * - 형식, 길이, 중복 검증을 한 곳에서 처리 | ||
| */ | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class NicknameValidator { | ||
|
|
||
| private final UserRepository userRepository; | ||
|
|
||
| private static final int MIN_LENGTH = 2; | ||
| private static final int MAX_LENGTH = 10; | ||
| private static final String NICKNAME_PATTERN = "^[가-힣a-zA-Z0-9]+$"; | ||
|
|
||
| /** | ||
| * 닉네임 사용 가능 여부 확인 (중복 체크만) | ||
| * - 형식/길이 검증 후 중복 여부 반환 | ||
| * - 검증 실패 시 예외 발생 | ||
| * | ||
| * @param nickname 검증할 닉네임 | ||
| * @return 사용 가능하면 true, 중복이면 false | ||
| * @throws CustomException 형식/길이가 잘못된 경우 | ||
| */ | ||
| public boolean isAvailable(String nickname) { | ||
| validateFormat(nickname); | ||
| return !userRepository.existsByNickname(nickname.trim()); | ||
| } | ||
|
|
||
| /** | ||
| * 닉네임 검증 (형식, 길이, 중복 모두 체크) | ||
| * - 검증 실패 시 예외 발생 | ||
| * | ||
| * @param nickname 검증할 닉네임 | ||
| * @throws CustomException 검증 실패 시 | ||
| */ | ||
| public void validate(String nickname) { | ||
| validateFormat(nickname); | ||
| validateDuplicate(nickname); | ||
| } | ||
|
|
||
| /** | ||
| * 닉네임 변경 시 검증 (기존 닉네임과 비교) | ||
| * - 기존 닉네임과 같으면 검증 통과 | ||
| * - 다르면 형식, 길이, 중복 체크 | ||
| * | ||
| * @param newNickname 새 닉네임 | ||
| * @param currentNickname 현재 닉네임 | ||
| * @throws CustomException 검증 실패 시 | ||
| */ | ||
| public void validateForUpdate(String newNickname, String currentNickname) { | ||
| // 기존 닉네임과 동일하면 검증 통과 | ||
| if (newNickname.equals(currentNickname)) { | ||
| return; | ||
| } | ||
|
|
||
| // 형식 검증 | ||
| validateFormat(newNickname); | ||
|
|
||
| // 중복 검증 | ||
| validateDuplicate(newNickname); | ||
| } | ||
|
|
||
| /** | ||
| * 닉네임 형식 및 길이 검증 | ||
| */ | ||
| private void validateFormat(String nickname) { | ||
| // null 체크 | ||
| if (nickname == null || nickname.trim().isEmpty()) { | ||
| throw new CustomException(UserErrorCode.INVALID_NICKNAME_FORMAT); | ||
| } | ||
|
|
||
| String trimmedNickname = nickname.trim(); | ||
|
|
||
| // 길이 체크 (2~10자) | ||
| int length = trimmedNickname.codePointCount(0, trimmedNickname.length()); | ||
| if (length < MIN_LENGTH || length > MAX_LENGTH) { | ||
| throw new CustomException(UserErrorCode.INVALID_NICKNAME_LENGTH); | ||
| } | ||
|
|
||
| // 형식 체크 (한글, 영문, 숫자만) | ||
| if (!trimmedNickname.matches(NICKNAME_PATTERN)) { | ||
| throw new CustomException(UserErrorCode.INVALID_NICKNAME_FORMAT); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 닉네임 중복 검증 | ||
| */ | ||
| private void validateDuplicate(String nickname) { | ||
| if (userRepository.existsByNickname(nickname.trim())) { | ||
| throw new CustomException(UserErrorCode.DUPLICATE_NICKNAME); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
...va/com/example/RealMatch/user/presentation/dto/response/NicknameAvailableResponseDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.example.RealMatch.user.presentation.dto.response; | ||
|
|
||
| public record NicknameAvailableResponseDto(boolean available) { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Swagger 문서(@ApiResponses)에 따르면 닉네임의 형식이나 길이가 유효하지 않을 경우
400 Bad Request를 반환해야 합니다. 하지만 현재isNicknameAvailable메소드는 유효성 검사에 실패할 경우false를 반환하여, 컨트롤러에서 항상200 OK와{ "available": false }응답을 보내게 됩니다. 이는 API 명세와 구현 간의 불일치입니다.유효성 검사에 실패했을 때
false를 반환하는 대신CustomException을 발생시켜 전역 예외 처리기에서 적절한 400번대 에러 응답을 생성하도록 수정하는 것을 권장합니다. 이렇게 하면 API 명세를 준수하고 클라이언트에게 더 명확한 에러 원인을 전달할 수 있습니다.더불어, 이 닉네임 유효성 검사 로직은
updateMyInfo메소드에도 중복으로 존재합니다. 이번 기회에 중복되는 로직을 별도의 private 메소드로 추출하여 재사용성을 높이는 리팩토링을 고려해보시는 것도 좋겠습니다.References