-
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.
Merge pull request #14 from TeamUStory/feat/user
Feat: 닉네임 조건을 추가하고, 맞지 않는 경우 자체적으로 수정해서 DB에 저장함.
- Loading branch information
Showing
11 changed files
with
117 additions
and
39 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
5 changes: 4 additions & 1 deletion
5
src/main/java/com/elice/ustory/domain/user/dto/ValidateNicknameRequest.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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package com.elice.ustory.domain.user.dto; | ||
|
||
import com.elice.ustory.domain.user.constant.RegexPatterns; | ||
import com.elice.ustory.domain.user.constant.UserMessageConstants; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class ValidateNicknameRequest { | ||
// TODO: 닉네임 규칙 생길 경우, 별도 메서드 없이 dto에서 확인 | ||
@Pattern(regexp = RegexPatterns.NICKNAME_REG_FULL, message = UserMessageConstants.NOT_APPROPIRATE_NICKNAME_MSSAGE) | ||
private String nickname; | ||
} |
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
86 changes: 86 additions & 0 deletions
86
src/main/java/com/elice/ustory/global/util/NicknameGenerator.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,86 @@ | ||
package com.elice.ustory.global.util; | ||
|
||
import com.elice.ustory.domain.user.constant.RegexPatterns; | ||
import com.elice.ustory.domain.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Random; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class NicknameGenerator { | ||
private final UserRepository userRepository; | ||
|
||
private static final int NICKNAME_MAX_LENGTH = RegexPatterns.NICKNAME_MAX_LENGTH; | ||
public static final String SEPARATOR = "#"; // 닉네임과 임의값 사이의 구분자 | ||
|
||
private static final int POSTFIX_LENGTH = 2; | ||
private static final int POSTFIX_LENGTH_WITH_SEPARATOR = POSTFIX_LENGTH + 1; | ||
|
||
public String formatNickname(String nickname) { | ||
// 닉네임에 포함된 특수문자를 제거한다. | ||
String formattedNickname = normalizeNicknameForOAuth(nickname); | ||
// 닉네임을 규정된 길이에 맞게 자른다. | ||
formattedNickname = trimNicknameForOAuth(nickname); | ||
// 중복 여부를 조회한다. 중복이면 닉네임을 7자 이내로 자르고, 겹치지 않을 때까지 임의의 postfix 3글자를 만들어 붙인다. | ||
if (checkDuplicateNickname(nickname)) { | ||
formattedNickname = trimNicknameForPostfix(formattedNickname); | ||
|
||
do { | ||
formattedNickname = formattedNickname + SEPARATOR + generateRandomPostfix(); | ||
} while (checkDuplicateNickname(formattedNickname)); | ||
} | ||
|
||
return formattedNickname; | ||
} | ||
|
||
public String generateRandomPostfix() { | ||
int leftLimit = 48; // 숫자 '0'의 ASCII 코드 | ||
int rightLimit = 122; // 알파벳 'z'의 ASCII 코드 | ||
int postfixLength = POSTFIX_LENGTH; | ||
Random random = new Random(); | ||
|
||
return random.ints(leftLimit, rightLimit + 1) // leftLimit(포함) 부터 rightLimit+1(불포함) 사이의 난수 스트림 생성 | ||
.filter(i -> (i < 57 || i >= 65) && ( i <= 90 || i >= 97)) // ASCII 테이블에서 숫자, 대문자, 소문자만 사용함 | ||
.limit(postfixLength) // 생성된 난수를 지정된 길이로 잘라냄 | ||
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append) // 생성된 난수를 ASCII 테이블에서 대응되는 문자로 변환 | ||
.toString(); // StringBuilder 객체를 문자열로 변환해 반환 | ||
} | ||
|
||
public String normalizeNicknameForOAuth(String socialNickname) { | ||
// 정규식에 맞지 않는 글자를 제외 | ||
String cleanedNickname = socialNickname.replaceAll(RegexPatterns.NICKNAME_REG_LETTER_FOR_REPLACE, ""); | ||
return cleanedNickname; | ||
} | ||
|
||
public String 임의의값을_추가하는_함수(String cleanedNickname) { | ||
//TODO | ||
return "String"; | ||
} | ||
|
||
public String trimNicknameForOAuth(String cleanedNickname) { | ||
if (cleanedNickname == null) { | ||
return null; | ||
} | ||
String trimmedNickname = cleanedNickname.length() > NICKNAME_MAX_LENGTH ? cleanedNickname.substring(0, NICKNAME_MAX_LENGTH) : cleanedNickname; | ||
return trimmedNickname; | ||
} | ||
|
||
public String trimNicknameForPostfix(String nickname) { | ||
if (nickname == null) { | ||
return null; | ||
} | ||
int availableLength = NICKNAME_MAX_LENGTH - POSTFIX_LENGTH_WITH_SEPARATOR; | ||
String trimmedNickname = nickname.length() > (availableLength) ? nickname.substring(0, availableLength) : nickname; | ||
return trimmedNickname; | ||
} | ||
|
||
public boolean checkDuplicateNickname(String nickname) { | ||
if (userRepository.countByNicknameWithSoftDeleted(nickname) > 0) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
21 changes: 0 additions & 21 deletions
21
src/main/java/com/elice/ustory/global/util/RandomGenerator.java
This file was deleted.
Oops, something went wrong.