-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…am-refactor [REFACTOR] 메일 도메인 리팩토링
- Loading branch information
Showing
34 changed files
with
321 additions
and
325 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
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,29 @@ | ||
package com.tiki.server.common.entity; | ||
|
||
import com.tiki.server.member.exception.MemberException; | ||
import jakarta.persistence.Embeddable; | ||
import lombok.*; | ||
import org.apache.commons.validator.routines.EmailValidator; | ||
|
||
import static com.tiki.server.emailverification.constants.EmailConstants.MAIL_FORMAT_AC_KR; | ||
import static com.tiki.server.emailverification.constants.EmailConstants.MAIL_FORMAT_EDU; | ||
import static com.tiki.server.member.message.ErrorCode.INVALID_EMAIL; | ||
|
||
@Getter | ||
@Embeddable | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
public class Email { | ||
|
||
private String email; | ||
public static Email from(String email){ | ||
checkMailFormat(email); | ||
return new Email(email); | ||
} | ||
|
||
private static void checkMailFormat(String email) { | ||
if (!EmailValidator.getInstance().isValid(email) || !(email.endsWith(MAIL_FORMAT_EDU) || email.endsWith(MAIL_FORMAT_AC_KR))) { | ||
throw new MemberException(INVALID_EMAIL); | ||
} | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/tiki/server/emailverification/adapter/EmailVerificationFinder.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,20 @@ | ||
package com.tiki.server.emailverification.adapter; | ||
|
||
import com.tiki.server.common.support.RepositoryAdapter; | ||
import com.tiki.server.emailverification.domain.EmailVerification; | ||
import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
import com.tiki.server.emailverification.repository.EmailVerificationRepository; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import static com.tiki.server.emailverification.message.ErrorCode.INVALID_REQUEST; | ||
|
||
@RepositoryAdapter | ||
@RequiredArgsConstructor | ||
public class EmailVerificationFinder { | ||
|
||
private final EmailVerificationRepository mailRepository; | ||
|
||
public EmailVerification findById(String email) { | ||
return mailRepository.findById(email).orElseThrow(() -> new EmailVerificationException(INVALID_REQUEST)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/tiki/server/emailverification/adapter/EmailVerificationSaver.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,17 @@ | ||
package com.tiki.server.emailverification.adapter; | ||
|
||
import com.tiki.server.common.support.RepositoryAdapter; | ||
import com.tiki.server.emailverification.domain.EmailVerification; | ||
import com.tiki.server.emailverification.repository.EmailVerificationRepository; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@RepositoryAdapter | ||
@RequiredArgsConstructor | ||
public class EmailVerificationSaver { | ||
|
||
private final EmailVerificationRepository mailRepository; | ||
|
||
public void save(EmailVerification mail) { | ||
mailRepository.save(mail); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package com.tiki.server.mail.constants; | ||
package com.tiki.server.emailverification.constants; | ||
|
||
public class MailConstants { | ||
public class EmailConstants { | ||
|
||
public static final String TIKI_EMAIL = "[email protected]"; | ||
public static final String MAIL_SUBJECT_SIGN_UP = "[Ti.Ki] 회원가입: 이메일 인증번호 안내"; | ||
|
@@ -9,4 +9,9 @@ public class MailConstants { | |
public static final String MAIL_FORMAT_EDU = ".edu"; | ||
public static final String MAIL_FORMAT_AC_KR = ".ac.kr"; | ||
public static final String TEMPLATE_NAME = "certification"; | ||
public static final int INIT_NUM = 0; | ||
public static final int CODE_LENGTH = 6; | ||
public static final int CODE_NUM_MAX_VALUE_PER_WORD = 10; | ||
public static final String CERTIFICATION_PAGE_LOGO_IMAGE_VAR = "image"; | ||
public static final String CERTIFICATION_PAGE_CODEE_VAR = "code"; | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/tiki/server/emailverification/domain/EmailVerification.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,37 @@ | ||
package com.tiki.server.emailverification.domain; | ||
|
||
import com.tiki.server.common.entity.Email; | ||
import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
import static com.tiki.server.emailverification.message.ErrorCode.INVALID_MATCHED; | ||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
import static lombok.AccessLevel.PRIVATE; | ||
|
||
@Getter | ||
@AllArgsConstructor(access = PRIVATE) | ||
@Builder | ||
@RedisHash(value = "mailVerification", timeToLive = 180) | ||
public class EmailVerification { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private String id; | ||
|
||
private String code; | ||
|
||
public static EmailVerification of(Email email, String code) { | ||
return EmailVerification.builder().id(email.getEmail()).code(code).build(); | ||
} | ||
|
||
public void verify(String code){ | ||
if(!this.code.equals(code)){ | ||
throw new EmailVerificationException(INVALID_MATCHED); | ||
} | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/tiki/server/emailverification/domain/MailSender.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,60 @@ | ||
package com.tiki.server.emailverification.domain; | ||
|
||
import com.tiki.server.common.entity.Email; | ||
import com.tiki.server.emailverification.exception.EmailVerificationException; | ||
import jakarta.mail.internet.MimeMessage; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.mail.javamail.JavaMailSender; | ||
import org.springframework.mail.javamail.MimeMessageHelper; | ||
import org.springframework.stereotype.Component; | ||
import org.thymeleaf.context.Context; | ||
import org.thymeleaf.spring6.SpringTemplateEngine; | ||
|
||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
import static com.tiki.server.emailverification.constants.EmailConstants.*; | ||
import static com.tiki.server.emailverification.message.ErrorCode.MESSAGE_HELPER_ERROR; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class MailSender { | ||
|
||
private final SpringTemplateEngine templateEngine; | ||
private final JavaMailSender javaMailSender; | ||
|
||
public EmailVerification sendVerificationMail(Email email, String subject) { | ||
String code = generateRandomValue(); | ||
MimeMessage message = makeMessage(email, code, subject); | ||
javaMailSender.send(message); | ||
return EmailVerification.of(email, code); | ||
} | ||
|
||
private static String generateRandomValue() { | ||
Random random = new Random(); | ||
return IntStream.range(INIT_NUM, CODE_LENGTH).mapToObj(i -> String.valueOf(random.nextInt(CODE_NUM_MAX_VALUE_PER_WORD))).collect(Collectors.joining()); | ||
} | ||
|
||
private MimeMessage makeMessage(Email email, String code, String subject) { | ||
MimeMessage message = javaMailSender.createMimeMessage(); | ||
try { | ||
MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8"); | ||
helper.setFrom(TIKI_EMAIL); | ||
helper.setTo(email.getEmail()); | ||
helper.setSubject(subject); | ||
helper.setText(setContext(code), true); | ||
helper.addInline(CERTIFICATION_PAGE_LOGO_IMAGE_VAR, new ClassPathResource(IMG_PATH)); | ||
return message; | ||
} catch (Exception e) { | ||
throw new EmailVerificationException(MESSAGE_HELPER_ERROR); | ||
} | ||
} | ||
|
||
private String setContext(String code) { | ||
Context context = new Context(); | ||
context.setVariable(CERTIFICATION_PAGE_CODEE_VAR, code); | ||
return templateEngine.process(TEMPLATE_NAME, context); | ||
} | ||
} |
Oops, something went wrong.