Skip to content

Commit

Permalink
feat: CodeGenerator 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
redcarrot1 committed Mar 17, 2024
1 parent cd2a8e3 commit 0816868
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Random;
import java.util.EnumSet;

@Service
@RequiredArgsConstructor
Expand All @@ -32,12 +32,16 @@ public class AuthEmailSendService {
@Value("${authentication.email.code-length}")
private Long codeLength;

@Value("${authentication.email.code-expiration-seconds}")
private Long codeExpirationSeconds;

@Transactional
public AuthEmailInfo sendAuthEmail(String target) {
validateEmailDomain(target);
long sendingCount = validateSendingCount(target);

String authenticationCode = createCode();
CodeGenerator codeGenerator = new CodeGenerator();
String authenticationCode = codeGenerator.generateCode(EnumSet.of(CodeGenerator.CodeType.NUMBER), codeLength);
LocalDateTime expiredAt = saveAuthEmail(target, authenticationCode);
sendEmail(target, authenticationCode);

Expand All @@ -53,29 +57,15 @@ private void validateEmailDomain(String target) {

private long validateSendingCount(String target) {
LocalDateTime today = LocalDate.now().atStartOfDay();
Long sendingCount = authEmailRepository.countByTargetAndCreatedAtAfter(target, today);
long sendingCount = authEmailRepository.countByTargetAndCreatedAtAfter(target, today);
if (sendingCount >= maxSendingCount) {
throw new SendingLimitExceededException();
}
return sendingCount;
}

private String createCode() {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder codeBuilder = new StringBuilder();

Random random = new Random();
for (int i = 0; i < codeLength; i++) {
int index = random.nextInt(characters.length());
char randomChar = characters.charAt(index);
codeBuilder.append(randomChar);
}

return codeBuilder.toString();
}

private LocalDateTime saveAuthEmail(String target, String authenticationCode) {
LocalDateTime expiredAt = LocalDateTime.now().plusMinutes(5);
LocalDateTime expiredAt = LocalDateTime.now().plusSeconds(codeExpirationSeconds);
AuthEmail authEmail = AuthEmail.createAuthEmail(target, authenticationCode, expiredAt);
authEmailRepository.save(authEmail);
return expiredAt;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.playkuround.playkuroundserver.domain.auth.email.application;

import java.util.Objects;
import java.util.Random;
import java.util.Set;

public class CodeGenerator {

public String generateCode(Set<CodeType> codeTypeSet, long codeLength) {
validateParameters(codeTypeSet, codeLength);

String characters = createCharacters(codeTypeSet);

StringBuilder codeBuilder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < codeLength; i++) {
int index = random.nextInt(characters.length());
char randomChar = characters.charAt(index);
codeBuilder.append(randomChar);
}
return codeBuilder.toString();
}

private void validateParameters(Set<CodeType> codeTypeSet, long codeLength) {
Objects.requireNonNull(codeTypeSet, "codeTypeSet must not be null");
if (codeTypeSet.isEmpty()) {
throw new IllegalArgumentException("codeTypeSet must not be empty");
}
if (codeLength <= 0) {
throw new IllegalArgumentException("codeLength must be greater than 0");
}
}

private String createCharacters(Set<CodeType> codeTypeSet) {
StringBuilder characters = new StringBuilder();
if (codeTypeSet.contains(CodeType.NUMBER)) {
characters.append("0123456789");
}
if (codeTypeSet.contains(CodeType.ALPHABET_LOWERCASE)) {
characters.append("abcdefghijklmnopqrstuvwxyz");
}
if (codeTypeSet.contains(CodeType.ALPHABET_UPPERCASE)) {
characters.append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
return characters.toString();
}

public enum CodeType {
NUMBER, ALPHABET_LOWERCASE, ALPHABET_UPPERCASE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface AuthEmailRepository extends JpaRepository<AuthEmail, Long> {

Optional<AuthEmail> findFirstByTargetOrderByCreatedAtDesc(String target);

Long countByTargetAndCreatedAtAfter(String target, LocalDateTime localDateTime);
long countByTargetAndCreatedAtAfter(String target, LocalDateTime localDateTime);

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void setUp() {
ReflectionTestUtils.setField(authEmailSendService, "codeLength", 6L);
ReflectionTestUtils.setField(authEmailSendService, "maxSendingCount", 3L);
ReflectionTestUtils.setField(authEmailSendService, "emailDomain", "test.com");
ReflectionTestUtils.setField(authEmailSendService, "codeExpirationSeconds", 300L);
}

@Test
Expand Down

0 comments on commit 0816868

Please sign in to comment.