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 12, 2025
1 parent 4216304 commit c252420
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/main/java/com/goolbitg/api/service/UserServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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 @@ -55,6 +56,7 @@
import com.goolbitg.api.security.AppleLoginManager;
import com.goolbitg.api.security.JwtManager;
import com.goolbitg.api.util.FormatUtil;
import com.goolbitg.api.util.RandomIdGenerator;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -90,9 +92,7 @@ public class UserServiceImpl implements UserService {
private final AppleLoginManager appleLoginManager;
@Autowired
private final TimeService timeService;

// 1 ~ 9 is reserved for default user
private int idSeq = 10;
private final JdbcTemplate jdbcTemplate;


/* --------------- API Implements ----------------------*/
Expand Down Expand Up @@ -150,7 +150,7 @@ public void register(AuthRequestDto request) {
throw UserException.alreadyRegistered(result.get().getId());
}

String userId = String.format("id%04d", idSeq++);
String userId = generateId();
User.UserBuilder userBuilder = User.builder()
.id(userId)
.registerDate(timeService.getToday());
Expand Down Expand Up @@ -539,4 +539,13 @@ private Long determineSpendingType(UserSurvey survey) {
return 5L;
}

@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);
}

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

public class RandomIdGenerator {

private static final int ID_LENGTH = 10;

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

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

return sb.toString();
}
}

8 changes: 8 additions & 0 deletions src/main/resources/db/migrations/V5__add_id_sequence.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE `sequences` (
`name` VARCHAR(50) NOT NULL,
`curval` BIGINT UNSIGNED DEFAULT 0,
PRIMARY KEY (`name`)
);

INSERT INTO `sequences` ( `name` )
VALUES ( 'id_seq' );
20 changes: 20 additions & 0 deletions src/test/java/com/goolbitg/api/util/RandomIdGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.goolbitg.api.util;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

/**
* RandomIdGeneratorTest
*/
public class RandomIdGeneratorTest {

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

assertEquals("abaaaaaaaa", randomId);
}

}

0 comments on commit c252420

Please sign in to comment.