Skip to content

Commit

Permalink
feat(user) 유저 ID 생성을 무작위 기반으로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
wwingyou committed Feb 13, 2025
1 parent 218c8a3 commit 592f186
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
25 changes: 13 additions & 12 deletions src/main/java/com/goolbitg/api/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.jwt.JwtDecoder;
import org.springframework.security.oauth2.jwt.NimbusJwtDecoder;
Expand Down Expand Up @@ -90,15 +89,12 @@ public class UserServiceImpl implements UserService {
private AppleLoginManager appleLoginManager;
@Autowired
private TimeService timeService;
@Autowired
private JdbcTemplate jdbcTemplate;


/* --------------- API Implements ----------------------*/

@Override
public UserDto getUser(String userId) throws Exception {
log.info("User id: " + userId);
User user = userRepository.findById(userId)
.orElseThrow(() -> UserException.userNotExist(userId));
UserSurvey survey = userSurveyRepository.findById(userId)
Expand Down Expand Up @@ -149,7 +145,8 @@ public void register(AuthRequestDto request) {
throw UserException.alreadyRegistered(result.get().getId());
}

String userId = generateId();
String userId = generateUserId();

User.UserBuilder userBuilder = User.builder()
.id(userId)
.registerDate(timeService.getToday());
Expand All @@ -165,6 +162,8 @@ public void register(AuthRequestDto request) {

UserStat stat = UserStat.getDefault(userId);
userStatsRepository.save(stat);

log.info("New User Created - " + userId);
}

@Override
Expand Down Expand Up @@ -306,6 +305,8 @@ public void unregister(String userId, UnregisterDto request, LocalDate date) {
userSurveyRepository.deleteById(userId);
userStatsRepository.deleteById(userId);
userRepository.deleteById(userId);

log.info("User Unregisterd - " + userId);
}

@Override
Expand Down Expand Up @@ -543,13 +544,13 @@ private boolean isNicknameExistInner(String nickname) {
return result.isPresent();
}

@Transactional
private String generateId() {
String select = "SELECT * FROM `sequences` WHERE name = 'id_seq'";
String update = "UPDATE `sequences` SET curval = curval + 1 WHERE name = 'id_seq'";
int curval = jdbcTemplate.query(select, (rs, rowNum) -> rs.getInt("curval")).get(0);
jdbcTemplate.update(update);
return RandomIdGenerator.generate(curval);
private String generateUserId() {
String userId = RandomIdGenerator.generate();
while (userRepository.existsById(userId)) {
userId = RandomIdGenerator.generate();
}
return userId;
}


}
9 changes: 5 additions & 4 deletions src/main/java/com/goolbitg/api/util/RandomIdGenerator.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.goolbitg.api.util;

import java.util.Random;

public class RandomIdGenerator {

private static final int ID_LENGTH = 10;

public static String generate(int input) {
public static String generate() {
Random random = new Random();
StringBuilder sb;
String chars = "abcdefghijklmnopqrstuvwxyz";

sb = new StringBuilder();
int value = input;
for (int i = 0; i < ID_LENGTH; i++) {
int index = value % chars.length();
int index = random.nextInt(26);
sb.append(chars.charAt(index));
value /= chars.length();
}

return sb.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ public class RandomIdGeneratorTest {

@Test
void get_random_id() {
int src = 26;
String randomId = RandomIdGenerator.generate(src);
String randomId = RandomIdGenerator.generate();

assertEquals("abaaaaaaaa", randomId);
assertEquals(10, randomId.length());
}

}

0 comments on commit 592f186

Please sign in to comment.