Skip to content

Commit

Permalink
feat: 회원 정보 조회 API (#226)
Browse files Browse the repository at this point in the history
* feat: 회원 정보 조회 API

* fix: oauth Id -> oauth email로 조회 조건 변경

* fix: 테스트 코드 수정

* fix: oauth 로직 수정
  • Loading branch information
char-yb authored Aug 26, 2024
1 parent d69767a commit 78d6bb7
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ logs
*.DS_Store
/src/main/generated
.*-database

### 멀티모듈 임시 주석 ###
walwal-*
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public SocialClientResponse authenticateFromProvider(OAuthProvider provider, Str
public AuthTokenResponse socialLogin(
OAuthProvider oAuthProvider, String oauthId, String email) {
Optional<Member> memberOptional =
memberRepository.findByOauthInfoOauthProviderAndOauthInfoOauthId(
oAuthProvider.getValue(), oauthId);
memberRepository.findByMemberOauthInfo(oAuthProvider.getValue(), email);

return memberOptional
.map(
Expand All @@ -67,7 +66,8 @@ public AuthTokenResponse socialLogin(
? getTemporaryLoginResponse(member)
: getLoginResponse(member);
member.updateLastLoginAt();
updateMemberStatus(member);
member.updateOauthId(oauthId);
updateMemberNormalStatus(member);
return AuthTokenResponse.of(
tokenPair, member.getRole() == MemberRole.TEMPORARY);
})
Expand Down Expand Up @@ -132,6 +132,7 @@ public void withdraw() {
validateMemberStatusDelete(member.getStatus());
member.updateMemberRole(MemberRole.TEMPORARY);
member.updateProfile(Profile.createProfile("", ""));
member.updateOauthId("");
memberRepository.flush();
withdrawMemberRelationByMemberId(member.getId());

Expand All @@ -156,7 +157,7 @@ private Member registerMember(Member member, CreateMemberRequest request) {
return member;
}

private void updateMemberStatus(Member member) {
private void updateMemberNormalStatus(Member member) {
if (member.getStatus() == MemberStatus.DELETED) {
member.updateStatus(MemberStatus.NORMAL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public SocialClientResponse authenticateFromKakao(String token) {
(request, response) -> {
if (!response.getStatusCode().is2xxSuccessful()) {
throw new CustomException(
ErrorCode.APPLE_TOKEN_CLIENT_FAILED);
ErrorCode.KAKAO_TOKEN_CLIENT_FAILED);
}
return Objects.requireNonNull(
response.bodyTo(KakaoAuthResponse.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -26,8 +27,14 @@ public class MemberController {

@Operation(summary = "내 정보 조회", description = "내 정보를 조회하는 API입니다.")
@GetMapping("/me")
public MemberInfoResponse memberInfo() {
return memberService.findMemberInfo();
public MemberInfoResponse memberMyInfo() {
return memberService.findMemberMyInfo();
}

@Operation(summary = "회원 정보 조회", description = "회원 정보를 조회하는 API입니다.")
@GetMapping("/{memberId}")
public MemberInfoResponse memberInfo(@PathVariable("memberId") Long memberId) {
return memberService.findMemberInfo(memberId);
}

@Operation(summary = "회원 프로필 변경", description = "회원 프로필을 변경합니다.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@ public class MemberService {
private final MemberUtil memberUtil;

@Transactional(readOnly = true)
public MemberInfoResponse findMemberInfo() {
public MemberInfoResponse findMemberMyInfo() {
Member currentMember = memberUtil.getCurrentMember();
return MemberInfoResponse.from(currentMember);
}

@Transactional(readOnly = true)
public MemberInfoResponse findMemberInfo(Long memberId) {
Member member = memberUtil.getMemberByMemberId(memberId);
return MemberInfoResponse.from(member);
}

@Transactional(readOnly = true)
public void checkNickname(NicknameCheckRequest request) {
memberUtil.checkNickname(request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@
import com.depromeet.stonebed.domain.member.domain.Member;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface MemberRepository extends JpaRepository<Member, Long>, MemberRepositoryCustom {
Optional<Member> findByOauthInfoOauthProviderAndOauthInfoOauthId(
String oauthProvider, String oauthId);

@Query(
"SELECT m FROM Member m WHERE m.oauthInfo.oauthProvider = :provider AND m.oauthInfo.oauthEmail = :email")
Optional<Member> findByMemberOauthInfo(
@Param("provider") String oauthProvider, @Param("email") String email);

Optional<Member> findByOauthInfoOauthProviderAndOauthInfoOauthEmail(
String oauthProvider, String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.SQLRestriction;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@SQLDelete(sql = "UPDATE member SET status = 'DELETED' WHERE member_id = ?")
@SQLRestriction("status != 'DELETED'")
public class Member extends BaseTimeEntity {

@Id
Expand Down Expand Up @@ -121,4 +119,8 @@ public void updateMemberRole(MemberRole memberRole) {
public void updateStatus(MemberStatus status) {
this.status = status;
}

public void updateOauthId(String oauthId) {
this.oauthInfo.oauthId = oauthId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class OauthInfo {

@Schema(description = "소셜 ID", example = "123487892")
private String oauthId;
public String oauthId;

@Schema(description = "소셜 제공자", example = "APPLE")
private String oauthProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ void setUp() {
// given
OAuthProvider provider = OAuthProvider.KAKAO;

when(memberRepository.findByOauthInfoOauthProviderAndOauthInfoOauthId(
provider.getValue(), oauthId))
when(memberRepository.findByMemberOauthInfo(provider.getValue(), email))
.thenReturn(Optional.of(member));
when(jwtTokenService.generateTokenPair(member.getId(), MemberRole.USER))
.thenReturn(new TokenPairResponse("accessToken", "refreshToken"));
Expand All @@ -75,8 +74,7 @@ void setUp() {
// given
OAuthProvider provider = OAuthProvider.APPLE;

when(memberRepository.findByOauthInfoOauthProviderAndOauthInfoOauthId(
provider.getValue(), oauthId))
when(memberRepository.findByMemberOauthInfo(provider.getValue(), email))
.thenReturn(Optional.of(member));
when(jwtTokenService.generateTokenPair(member.getId(), MemberRole.USER))
.thenReturn(new TokenPairResponse("accessToken", "refreshToken"));
Expand All @@ -95,8 +93,7 @@ void setUp() {
TokenPairResponse temporaryTokenPair = new TokenPairResponse("accessToken", "refreshToken");
Member newMember = Member.createOAuthMember(provider, oauthId, email);

when(memberRepository.findByOauthInfoOauthProviderAndOauthInfoOauthId(
provider.getValue(), oauthId))
when(memberRepository.findByMemberOauthInfo(provider.getValue(), email))
.thenReturn(Optional.empty());
when(memberRepository.save(any(Member.class))).thenReturn(newMember);
when(jwtTokenService.generateTemporaryTokenPair(any(Member.class)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MemberServiceTest extends FixtureMonkeySetUp {
when(memberUtil.getCurrentMember()).thenReturn(member);

// when
MemberInfoResponse result = memberService.findMemberInfo();
MemberInfoResponse result = memberService.findMemberMyInfo();

// then
assertNotNull(result);
Expand Down

0 comments on commit 78d6bb7

Please sign in to comment.