Skip to content

Commit

Permalink
Merge pull request #131 from Team-Tiki/#130-member-refactor
Browse files Browse the repository at this point in the history
[REFACTOR] Member 도메인 리팩토링
  • Loading branch information
paragon0107 authored Jul 31, 2024
2 parents 063bbb4 + 4331610 commit 6d8d005
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private UrlBasedCorsConfigurationSource setUrlBasedCorsConfigurationSource() {
private CorsConfiguration setCorsConfiguration() {
val config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin(("https://tiki-client.vercel.app"));
config.addAllowedOrigin(("https://ti-kii.com"));
config.addAllowedOrigin("http://localhost:5173");
config.addAllowedOrigin("https://www.tiki-sopt.p-e.kr");
config.addAllowedHeader("*");
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/tiki/server/member/adapter/MemberFinder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tiki.server.member.adapter;

import static com.tiki.server.member.message.ErrorCode.CONFLICT_MEMBER;
import static com.tiki.server.member.message.ErrorCode.INVALID_MEMBER;

import com.tiki.server.common.support.RepositoryAdapter;
Expand All @@ -22,7 +23,16 @@ public Optional<Member> findByEmail(String email) {
}

public Member findById(long memberId) {
return memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(INVALID_MEMBER));
return memberRepository.findById(memberId).orElseThrow(() -> new MemberException(INVALID_MEMBER));
}

public Member checkEmpty(String email) {
return findByEmail(email).orElseThrow(() -> new MemberException(INVALID_MEMBER));
}

public void checkPresent(String email) {
findByEmail(email).ifPresent((member) -> {
throw new MemberException(CONFLICT_MEMBER);
});
}
}
24 changes: 22 additions & 2 deletions src/main/java/com/tiki/server/member/entity/Member.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.tiki.server.member.entity;

import static com.tiki.server.mail.constants.MailConstants.MAIL_FORMAT_AC_KR;
import static com.tiki.server.mail.constants.MailConstants.MAIL_FORMAT_EDU;
import static com.tiki.server.member.message.ErrorCode.INVALID_EMAIL;
import static com.tiki.server.member.message.ErrorCode.UNMATCHED_PASSWORD;
import static jakarta.persistence.EnumType.STRING;
import static jakarta.persistence.GenerationType.IDENTITY;

import com.tiki.server.member.exception.MemberException;
import java.time.LocalDate;

import com.tiki.server.common.entity.BaseTime;
Expand Down Expand Up @@ -38,17 +43,32 @@ public class Member extends BaseTime {
@Enumerated(value = STRING)
private University univ;

public static Member of(String email, String password, String name, LocalDate birth, University univ) {
return Member.builder()
public static Member of(
String email,
String password,
String passwordChecker,
String name,
LocalDate birth,
University univ) {
val member = Member.builder()
.email(email)
.password(password)
.name(name)
.birth(birth)
.univ(univ)
.build();

member.checkMailFormat();
return member;
}

public void resetPassword(String password) {
this.password = password;
}

private void checkMailFormat() {
if (!(this.email.endsWith(MAIL_FORMAT_EDU) || this.email.endsWith(MAIL_FORMAT_AC_KR))) {
throw new MemberException(INVALID_EMAIL);
}
}
}
33 changes: 9 additions & 24 deletions src/main/java/com/tiki/server/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,50 +31,35 @@ public class MemberService {

@Transactional
public void signUp(MemberProfileCreateRequest request) {
checkMailFormat(request.email());
checkMailDuplicate(request.email());
memberFinder.checkPresent(request.email());
checkPassword(request.password(), request.passwordChecker());
val member = createMember(request);
saveMember(member);
}

@Transactional
public void changePassword(PasswordChangeRequest request) {
val member = checkMemberEmpty(request);
val member = memberFinder.checkEmpty(request.email());
checkPassword(request.password(), request.passwordChecker());
member.resetPassword(passwordEncoder.encode(request.password()));
}

private Member checkMemberEmpty(PasswordChangeRequest request) {
return memberFinder.findByEmail(request.email()).orElseThrow(() -> new MemberException(INVALID_MEMBER));
}

private void checkMailDuplicate(String email) {
memberFinder.findByEmail(email).ifPresent(member -> {
throw new MemberException(CONFLICT_MEMBER);
});
}
private void checkMailFormat(String email){
if (!(email.endsWith(MAIL_FORMAT_EDU) || email.endsWith(MAIL_FORMAT_AC_KR))) {
throw new MemberException(INVALID_EMAIL);
}
}

private void checkPassword(String password, String passwordChecker) {
if (!password.equals(passwordChecker)) {
throw new MemberException(UNMATCHED_PASSWORD);
}
}

private Member createMember(MemberProfileCreateRequest request) {
return Member.of(
request.email(),
passwordEncoder.encode(request.password()),
passwordEncoder.encode(request.passwordChecker()),
request.name(),
request.birth(),
request.univ());
}

private void checkPassword(String password, String passwordChecker) {
if (password.equals(passwordChecker)) {
throw new MemberException(UNMATCHED_PASSWORD);
}
}

private void saveMember(Member member) {
memberSaver.save(member);
}
Expand Down

0 comments on commit 6d8d005

Please sign in to comment.