-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
회원가입 시 닉네임 생성 로직 수정 완료
- Loading branch information
Showing
9 changed files
with
141 additions
and
58 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
102 changes: 102 additions & 0 deletions
102
src/main/java/com/munecting/api/domain/user/service/UserNicknameService.java
This file contains 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,102 @@ | ||
package com.munecting.api.domain.user.service; | ||
|
||
import com.munecting.api.domain.user.dao.UserRepository; | ||
import com.munecting.api.domain.user.entity.User; | ||
import com.munecting.api.global.error.exception.InternalServerException; | ||
import com.munecting.api.global.error.exception.InvalidValueException; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.util.StringUtils; | ||
|
||
import java.util.Random; | ||
|
||
import static com.munecting.api.global.common.dto.response.Status.*; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class UserNicknameService { | ||
|
||
private final UserRepository userRepository; | ||
|
||
private static final int MIN_LENGTH = 2; | ||
private static final int MAX_LENGTH = 15; | ||
|
||
private static final String NAME_VALUE_RULES = "^(?=.*[a-zA-Z0-9가-힣])[a-zA-Z가-힣][a-zA-Z0-9가-힣_]*$"; | ||
|
||
private static final String DEFAULT_NICKNAME = "뮤넥터"; | ||
private static final String DELIMITER = "_"; | ||
private static final int MAX_UNIQUE_STRING_LENGTH = 10; | ||
private static final int MAX_NICKNAME_RETRY_COUNT = 100; | ||
private static final String CHARACTERS_POOL = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
private final Random random = new Random(); | ||
|
||
public String updateNickname(User user, String nickname) { | ||
if (!StringUtils.hasText(nickname)) { | ||
return user.getNickname(); | ||
} | ||
|
||
validateNickname(user, nickname); | ||
return user.updateNickname(nickname); | ||
} | ||
|
||
private void validateNickname(User existingUser, String nickname) { | ||
if (isInvalidNicknameLength(nickname)) { | ||
throw new InvalidValueException(INVALID_NICKNAME_LENGTH); | ||
} | ||
|
||
if (isInvalidNamingRule(nickname)) { | ||
throw new InvalidValueException(INVALID_NICKNAME_VALUE); | ||
} | ||
|
||
if (isDuplicatedNickname(existingUser, nickname)) { | ||
throw new InvalidValueException(DUPLICATED_NICKNAME); | ||
} | ||
} | ||
|
||
private boolean isInvalidNicknameLength(String nickname) { | ||
return nickname.length() < MIN_LENGTH || nickname.length() > MAX_LENGTH; | ||
} | ||
|
||
private boolean isInvalidNamingRule(String nickname) { | ||
return !nickname.matches(NAME_VALUE_RULES); | ||
} | ||
|
||
private boolean isDuplicatedNickname(User existingUser, String nickname) { | ||
// 기존 닉네임과 일치하는 경우 중복 체크 제외 | ||
if (existingUser.getNickname().equals(nickname)) { | ||
return false; | ||
} | ||
|
||
return userRepository.existsByNickname(nickname); | ||
} | ||
|
||
public String generateUniqueNickname() { | ||
String nickname; | ||
int attemptCount = 0; | ||
|
||
do { | ||
if (attemptCount >= MAX_NICKNAME_RETRY_COUNT) { | ||
throw new InternalServerException("닉네임 생성에 실패하였습니다."); | ||
} | ||
nickname = DEFAULT_NICKNAME + DELIMITER + generateUniqueString(); | ||
attemptCount++; | ||
|
||
} while (userRepository.existsByNickname(nickname)); | ||
|
||
return nickname; | ||
} | ||
|
||
private String generateUniqueString() { | ||
int uniqueStringLength = random.nextInt(MAX_UNIQUE_STRING_LENGTH); | ||
log.info("uniqueStringLength : {}", uniqueStringLength); | ||
|
||
StringBuilder result = new StringBuilder(); | ||
for (int i = 0; i < uniqueStringLength; i++) { | ||
result.append(CHARACTERS_POOL.charAt(random.nextInt(CHARACTERS_POOL.length()))); | ||
} | ||
|
||
return result.toString(); | ||
} | ||
} |
This file contains 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 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