Skip to content

Commit e054be3

Browse files
committed
feat: 랜덤 유저 id 생성기 추가
1 parent ae9284c commit e054be3

File tree

2 files changed

+74
-5
lines changed

2 files changed

+74
-5
lines changed

backend/src/main/java/com/woowacourse/ternoko/auth/application/AuthService.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.IOException;
2323
import java.util.NoSuchElementException;
2424
import java.util.Optional;
25-
import java.util.concurrent.atomic.AtomicLong;
2625
import org.springframework.beans.factory.annotation.Value;
2726
import org.springframework.stereotype.Service;
2827
import org.springframework.transaction.annotation.Transactional;
@@ -44,8 +43,7 @@ public class AuthService {
4443
private final String clientId;
4544
private final String clientSecret;
4645

47-
private final static AtomicLong ATOMIC_COACH_ID = new AtomicLong(0L);
48-
private final static AtomicLong ATOMIC_CREW_ID = new AtomicLong(0L);
46+
private final RandomMemberIdGenerator memberIdGenerator = RandomMemberIdGenerator.of(111, 210, 11, 110);
4947

5048
public AuthService(final MethodsClientImpl slackMethodClient,
5149
final MemberRepository memberRepository,
@@ -64,15 +62,15 @@ public AuthService(final MethodsClientImpl slackMethodClient,
6462
}
6563

6664
public LoginResponse loginCoach() {
67-
long coachId = (ATOMIC_COACH_ID.getAndAdd(1)) % 100 + 11;
65+
final Long coachId = memberIdGenerator.getRandomCoachId();
6866
final Member member = memberRepository.findById(coachId)
6967
.orElseThrow(() -> new NoSuchElementException("로그인할 coachId가 존재하지 않습니다."));
7068

7169
return LoginResponse.of(COACH, jwtProvider.createToken(member), true);
7270
}
7371

7472
public LoginResponse loginCrew() {
75-
long crewId = (ATOMIC_CREW_ID.getAndAdd(1)) % 100 + 111;
73+
final Long crewId = memberIdGenerator.getRandomCrewId();
7674
final Member member = memberRepository.findById(crewId)
7775
.orElseThrow(() -> new NoSuchElementException("로그인할 crewId가 존재하지 않습니다."));
7876

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.woowacourse.ternoko.auth.application;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
import java.util.Random;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
public class RandomMemberIdGenerator {
10+
11+
final static Random RANDOM = new Random(System.currentTimeMillis());
12+
13+
final long startCrewId;
14+
final long endCrewId;
15+
final long startCoachId;
16+
final long endCoachId;
17+
List<Long> crewIds;
18+
List<Long> coachIds;
19+
20+
public RandomMemberIdGenerator(final long startCrewId, final long endCrewId, final long startCoachId,
21+
final long endCoachId,
22+
final List<Long> crewIds, final List<Long> coachIds) {
23+
this.startCrewId = startCrewId;
24+
this.endCrewId = endCrewId;
25+
this.startCoachId = startCoachId;
26+
this.endCoachId = endCoachId;
27+
this.crewIds = crewIds;
28+
this.coachIds = coachIds;
29+
}
30+
31+
public static RandomMemberIdGenerator of(final long startCrewId,
32+
final long endCrewId,
33+
final long startCoachId,
34+
final long endCoachId) {
35+
final List<Long> crewIds = getIds(startCrewId, endCrewId);
36+
37+
final List<Long> coachIds = new ArrayList<>();
38+
for (long i = startCoachId; i <= endCoachId; i++) {
39+
coachIds.add(i);
40+
}
41+
42+
return new RandomMemberIdGenerator(startCrewId, endCrewId, startCoachId, endCoachId, crewIds, coachIds);
43+
}
44+
45+
@NotNull
46+
private static List<Long> getIds(final long startId, final long endId) {
47+
final List<Long> ids = Collections.synchronizedList(new ArrayList<>());
48+
for (long i = startId; i <= endId; i++) {
49+
ids.add(i);
50+
}
51+
return ids;
52+
}
53+
54+
public Long getRandomCrewId() {
55+
if (crewIds.size() == 0) {
56+
crewIds = getIds(startCrewId, endCrewId);
57+
}
58+
59+
final int randomIndex = RANDOM.nextInt(crewIds.size());
60+
return crewIds.remove(randomIndex);
61+
}
62+
63+
public Long getRandomCoachId() {
64+
if (coachIds.size() == 0) {
65+
coachIds = getIds(startCoachId, endCoachId);
66+
}
67+
68+
final int randomIndex = RANDOM.nextInt(coachIds.size());
69+
return coachIds.remove(randomIndex);
70+
}
71+
}

0 commit comments

Comments
 (0)