Skip to content

Commit

Permalink
refactor(persist): refactor persist login (#92)
Browse files Browse the repository at this point in the history
* refactor(member): move imageAuth field into ProfileImage

* refactor(persist): change PersistResponseDto field

* refactor(persist): remove PersistRepository from MemberRepository

* refactor(test): change PersistLoginRepository bean type

* test: add test before query refactoring

* fix(test): make test red to green (#91)

* test: fix test authorization to green

* test: fix test red to green

* refactor(auth): make load Member code clean

* test(auth): add findByPhoneNumber query test

* fix(auth): fix bug MemberPrincipal-toString()

* feat(code): add CodePk format validator

* test(code): test validateCodePkFormat method

* feat(code): add query find Code list by CodePk list

* refactor(code): refactoring findCodeList and test

* style(test): change display name

---------

Co-authored-by: KAispread <[email protected]>

* feat(persist): implement persist login

* fix(persist): fix compile error

* feat(profileImage): Add a method that returns boolean value

---------

Co-authored-by: KAispread <[email protected]>
  • Loading branch information
KAispread and KAispread authored Aug 10, 2023
1 parent 771edcd commit b64826a
Show file tree
Hide file tree
Showing 16 changed files with 260 additions and 124 deletions.
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 @@ -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 @@ -6,7 +6,6 @@
import com.e2i.wemeet.domain.member.Member;
import com.e2i.wemeet.domain.member.MemberRepository;
import com.e2i.wemeet.domain.member.data.Role;
import com.e2i.wemeet.dto.response.persist.PersistResponseDto;
import com.e2i.wemeet.exception.unauthorized.CreditNotEnoughException;
import com.e2i.wemeet.exception.unauthorized.UnAuthorizedRoleException;
import com.e2i.wemeet.security.manager.CreditAuthorizationManager;
Expand Down Expand Up @@ -307,10 +306,6 @@ public Page<Member> findAll(Pageable pageable) {
return null;
}

@Override
public PersistResponseDto findPersistResponseById(Long memberId) {
return null;
}
}

static class RoleHierarchyImpl implements RoleHierarchy {
Expand Down
36 changes: 21 additions & 15 deletions src/test/java/com/e2i/wemeet/controller/TokenControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,13 @@ class TokenControllerTest extends AbstractControllerUnitTest {
@MockBean
private TokenService tokenService;

@DisplayName("ACCESS TOKEN을 통해 유저의 상태 정보를 조회할 수 있다.")
@WithCustomMockUser
@DisplayName("Persist Login 요청을 통해 유저의 상태 정보를 반환받는다.")
@Test
void persist() throws Exception {
// given
given(tokenService.persistLogin(1L)).willReturn(
PersistResponseDto.builder()
.nickname("kai")
.hasTeam(true)
.emailAuthenticated(true)
.profileImageAuthenticated(false)
.hasMainProfileImage(true)
.preferenceCompleted(true)
.build()
);
given(tokenService.persistLogin(1L))
.willReturn(createPersistResponseDto());

// when
ResultActions perform = mockMvc.perform(
Expand Down Expand Up @@ -74,15 +66,29 @@ void persist() throws Exception {
.description("사용자의 닉네임을 반환합니다."),
fieldWithPath("data.emailAuthenticated").type(JsonFieldType.BOOLEAN)
.description("이메일 인증 여부를 반환합니다."),
fieldWithPath("data.preferenceCompleted").type(JsonFieldType.BOOLEAN)
fieldWithPath("data.profileImageAuthenticated").type(JsonFieldType.BOOLEAN)
.description("선호도 조사 여부를 반환합니다."),
fieldWithPath("data.hasMainProfileImage").type(JsonFieldType.BOOLEAN)
fieldWithPath("data.hasTeam").type(JsonFieldType.BOOLEAN)
.description("프로필 이미지의 등록 여부를 반환합니다."),
fieldWithPath("data.profileImageAuthenticated").type(JsonFieldType.BOOLEAN)
fieldWithPath("data.hasMainProfileImage").type(JsonFieldType.BOOLEAN)
.description("프로필 이미지 인증 여부를 반환합니다."),
fieldWithPath("data.hasTeam").type(JsonFieldType.BOOLEAN)
fieldWithPath("data.basicProfileImage").type(JsonFieldType.STRING)
.description("팀의 존재 여부를 반환합니다."),
fieldWithPath("data.lowProfileImage").type(JsonFieldType.STRING)
.description("팀의 존재 여부를 반환합니다.")
)
));
}

private PersistResponseDto createPersistResponseDto() {
return PersistResponseDto.builder()
.nickname("닉네임")
.emailAuthenticated(true)
.profileImageAuthenticated(true)
.hasTeam(true)
.hasMainProfileImage(true)
.lowProfileImage("lowUrl")
.basicProfileImage("basicUrl")
.build();
}
}
Loading

0 comments on commit b64826a

Please sign in to comment.