Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(persist): refactor persist login #92

Merged
merged 9 commits into from
Aug 10, 2023
3 changes: 3 additions & 0 deletions src/main/java/com/e2i/wemeet/domain/code/CodePk.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.e2i.wemeet.domain.code;

import com.e2i.wemeet.util.validator.CustomFormatValidator;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.io.Serializable;
Expand Down Expand Up @@ -27,6 +28,8 @@ public CodePk(String codeId, String groupCodeId) {

// CE-001 -> CODE PK 생성
public static CodePk of(final String groupCodeIdWithCodeId) {
CustomFormatValidator.validateCodePkFormat(groupCodeIdWithCodeId);

final String[] split = groupCodeIdWithCodeId.split("-");
return new CodePk(split[1], split[0]);
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/e2i/wemeet/domain/code/CodeRepository.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.e2i.wemeet.domain.code;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -9,4 +10,6 @@ public interface CodeRepository extends JpaRepository<Code, CodePk> {

Optional<Code> findByCodePk(CodePk codePk);

List<Code> findByCodePkIn(List<CodePk> codePkList);

}
4 changes: 0 additions & 4 deletions src/main/java/com/e2i/wemeet/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public class Member extends BaseTimeEntity {
@Column(nullable = false)
private Integer credit;

@Column
private Boolean imageAuth;

@Embedded
private ProfileImage profileImage;

Expand All @@ -99,7 +96,6 @@ public Member(String nickname, Gender gender, String phoneNumber, String email,
this.collegeInfo = collegeInfo;
this.mbti = mbti;
this.credit = credit;
this.imageAuth = imageAuth;
this.profileImage = profileImage;
this.role = role;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.e2i.wemeet.domain.member;

import com.e2i.wemeet.domain.member.persist.PersistLoginRepository;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

public interface MemberRepository extends JpaRepository<Member, Long>, PersistLoginRepository {
public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByPhoneNumber(String phoneNumber);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ public class ProfileImage {

private String basicUrl;
private String lowUrl;
private Boolean imageAuth;

@Builder
public ProfileImage(String basicUrl, String lowUrl) {
public ProfileImage(String basicUrl, String lowUrl, Boolean imageAuth) {
this.basicUrl = basicUrl;
this.lowUrl = lowUrl;
this.imageAuth = imageAuth;
}

public boolean hasProfileImage() {
return basicUrl != null;
}

public boolean isImageAuthenticated() {
return imageAuth;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.e2i.wemeet.domain.member.persist;

import com.e2i.wemeet.domain.member.data.ProfileImage;

public record PersistData(
String nickname,
String email,
ProfileImage profileImage,
Long teamId
) {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.e2i.wemeet.domain.member.persist;

import com.e2i.wemeet.dto.response.persist.PersistResponseDto;
import java.util.Optional;

public interface PersistLoginRepository {

PersistResponseDto findPersistResponseById(Long memberId);
Optional<PersistResponseDto> findPersistResponseById(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,39 @@
package com.e2i.wemeet.domain.member.persist;

import static com.e2i.wemeet.domain.member.QMember.member;
import static com.e2i.wemeet.domain.team.QTeam.team;

import com.e2i.wemeet.dto.response.persist.PersistResponseDto;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

// TODO :: service refactoring
@Repository
@RequiredArgsConstructor
public class PersistLoginRepositoryImpl implements PersistLoginRepository {

private final JPAQueryFactory queryFactory;

// TODO :: service refactoring
@Override
public PersistResponseDto findPersistResponseById(Long memberId) {
// Optional<PersistLoginData> persistLoginData = Optional.ofNullable(queryFactory
// .select(Projections.constructor(
// PersistLoginData.class,
// member.nickname,
// member.collegeInfo.mail.as("email"),
// team.teamId
// ))
// .from(member)
// .leftJoin(member.team, team)
// .where(member.memberId.eq(memberId))
// .fetchOne());
public Optional<PersistResponseDto> findPersistResponseById(Long memberId) {
PersistData findPersistData = queryFactory
.select(Projections.constructor(
PersistData.class,
member.nickname,
member.email,
member.profileImage,
team.teamId
))
.from(member)
.leftJoin(member.team, team)
.where(member.memberId.eq(memberId))
.fetchOne();

return null;
if (findPersistData == null) {
return Optional.empty();
}
return Optional.ofNullable(PersistResponseDto.of(findPersistData));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,64 @@
package com.e2i.wemeet.dto.response.persist;

import com.e2i.wemeet.domain.member.data.ProfileImage;
import com.e2i.wemeet.domain.member.persist.PersistData;
import java.util.Objects;
import lombok.Builder;

// TODO :: service refactoring
@Builder
public record PersistResponseDto(
String nickname,
boolean emailAuthenticated,
boolean preferenceCompleted,
boolean hasMainProfileImage,
String basicProfileImage,
String lowProfileImage,
boolean profileImageAuthenticated,
boolean hasTeam
) {

public static PersistResponseDto of(PersistData persistData) {
final boolean emailAuthenticated = persistData.email() != null;
final boolean hasTeam = persistData.teamId() != null;

PersistResponseDtoBuilder builder = PersistResponseDto.builder()
.nickname(persistData.nickname())
.emailAuthenticated(emailAuthenticated)
.hasTeam(hasTeam);

if (Objects.isNull(persistData.profileImage())) {
return createResponseHasNoImage(builder);
}
return createResponse(persistData.profileImage(), builder);
}

private static PersistResponseDto createResponse(ProfileImage profileImage, PersistResponseDtoBuilder builder) {
return builder
.hasMainProfileImage(profileImage.hasProfileImage())
.profileImageAuthenticated(profileImage.isImageAuthenticated())
.basicProfileImage(profileImage.getBasicUrl())
.lowProfileImage(profileImage.getLowUrl())
.build();
}

private static PersistResponseDto createResponseHasNoImage(PersistResponseDtoBuilder builder) {
return builder
.hasMainProfileImage(false)
.profileImageAuthenticated(false)
.basicProfileImage(null)
.lowProfileImage(null)
.build();
}

@Override
public String toString() {
return "PersistResponseDto(nickname=" + this.nickname() + ", emailAuthenticated=" + this.emailAuthenticated() + ", preferenceCompleted="
+ this.preferenceCompleted() + ", hasMainProfileImage=" + this.hasMainProfileImage() + ", profileImageAuthenticated="
+ this.profileImageAuthenticated() + ", hasTeam=" + this.hasTeam() + ")";
return "PersistResponseDto{" +
"nickname='" + nickname + '\'' +
", emailAuthenticated=" + emailAuthenticated +
", hasMainProfileImage=" + hasMainProfileImage +
", basicProfileImage='" + basicProfileImage + '\'' +
", lowProfileImage='" + lowProfileImage + '\'' +
", profileImageAuthenticated=" + profileImageAuthenticated +
", hasTeam=" + hasTeam +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String toString() {
.map(GrantedAuthority::getAuthority)
.toList()
);
final String format = "MemberPrincipal(memberId=%d, role=%s, registrationType=%s)";
final String format = "MemberPrincipal(memberId=%d, role=%s)";

return String.format(format, memberId, role);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.e2i.wemeet.domain.member.Member;
import com.e2i.wemeet.domain.member.MemberRepository;
import com.e2i.wemeet.security.model.MemberPrincipal;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
Expand All @@ -18,12 +19,10 @@ public class SmsUserDetailsService implements UserDetailsService {
*/
@Override
public UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
Member member = memberRepository.findByPhoneNumber(username).orElse(null);
Optional<Member> member = memberRepository.findByPhoneNumber(username);

// SMS 인증을 요청한 사용자가 회원가입이 되어있지 않을 경우
if (member == null) {
return new MemberPrincipal();
}
return new MemberPrincipal(member);
return member.map(MemberPrincipal::new)
.orElseGet(MemberPrincipal::new);
}
}
30 changes: 7 additions & 23 deletions src/main/java/com/e2i/wemeet/service/code/CodeServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,23 @@
import com.e2i.wemeet.domain.code.Code;
import com.e2i.wemeet.domain.code.CodePk;
import com.e2i.wemeet.domain.code.CodeRepository;
import com.e2i.wemeet.exception.ErrorCode;
import com.e2i.wemeet.exception.badrequest.InvalidDataFormatException;
import com.e2i.wemeet.exception.notfound.CodeNotFoundException;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Transactional(readOnly = true)
@Service
public class CodeServiceImpl implements CodeService {

private final CodeRepository codeRepository;

public List<Code> findCodeList(List<String> dataList) {
List<Code> result = new ArrayList<>();
for (String data : dataList) {
String[] splitCode = splitString(data);
Code code = codeRepository.findById(new CodePk(splitCode[1], splitCode[0]))
.orElseThrow(CodeNotFoundException::new);
public List<Code> findCodeList(List<String> codeList) {
List<CodePk> codePks = codeList.stream()
.map(CodePk::of)
.toList();

result.add(code);
}

return result;
}

private String[] splitString(String input) {
String[] splitArray = input.split("_");
if (splitArray.length != 2) {
throw new InvalidDataFormatException(ErrorCode.INVALID_CODE_FORMAT);
}

return splitArray;
return codeRepository.findByCodePkIn(codePks);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.e2i.wemeet.domain.member.persist.PersistLoginRepository;
import com.e2i.wemeet.dto.response.persist.PersistResponseDto;
import com.e2i.wemeet.exception.notfound.MemberNotFoundException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -19,6 +20,7 @@ public TokenServiceImpl(
@Transactional(readOnly = true)
@Override
public PersistResponseDto persistLogin(final Long memberId) {
return persistLoginRepository.findPersistResponseById(memberId);
return persistLoginRepository.findPersistResponseById(memberId)
.orElseThrow(MemberNotFoundException::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static com.e2i.wemeet.exception.ErrorCode.INVALID_NICKNAME_FORMAT;
import static com.e2i.wemeet.exception.ErrorCode.INVALID_PHONE_FORMAT;

import com.e2i.wemeet.exception.ErrorCode;
import com.e2i.wemeet.exception.badrequest.InvalidDataFormatException;
import java.util.regex.Pattern;

Expand All @@ -22,6 +23,7 @@ private CustomFormatValidator() {
private static final Pattern SMS_CREDENTIAL_REG = Pattern.compile("^\\d{6}$");
private static final Pattern EMAIL_CREDENTIAL_REG = Pattern.compile("^\\d{6}$");
private static final Pattern NICKNAME_PATTERN = Pattern.compile("^[가-힣\\s]{2,10}$");
private static final Pattern CODE_PK_PATTERN = Pattern.compile("^[A-Z]{2}-\\d{3}$");

public static void validatePhoneFormat(final String phone) {
if (!PHONE_REG.matcher(phone).matches()) {
Expand Down Expand Up @@ -52,4 +54,10 @@ public static void validateNicknameFormat(final String nickname) {
throw new InvalidDataFormatException(INVALID_NICKNAME_FORMAT);
}
}

public static void validateCodePkFormat(final String groupCodeIdWithCodeId) {
if (!CODE_PK_PATTERN.matcher(groupCodeIdWithCodeId).matches()) {
throw new InvalidDataFormatException(ErrorCode.INVALID_DATA_FORMAT);
}
}
}
Loading
Loading