Skip to content

Commit

Permalink
fix: member 조회, 업데이트 메소드 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
unanchoi committed Jul 22, 2024
1 parent f053b65 commit 115e1be
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.pophory.pophoryapi.member.application

import com.pophory.pophorydomain.member.Member
import com.pophory.pophorydomain.member.infrastructure.MemberJpaRepository
import jakarta.persistence.EntityNotFoundException
import org.springframework.stereotype.Component

@Component
class MemberFinder(
private val memberJpaRepository: MemberJpaRepository
) {

fun findById(id: Long) : Member {
return memberJpaRepository.findById(id)
.orElseThrow { EntityNotFoundException("Member not found") }
}

fun findByIdForUpdate(id: Long) : Member {
return memberJpaRepository.findByIdForUpdate(id)
.orElseThrow { EntityNotFoundException("Member not found") }
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class MemberService {
private final AlbumJpaRepository albumJpaRepository;
private final AlbumDesignJpaRepository albumDesignJpaRepository;
private final FcmJpaRepository fcmJpaRepository;
private final MemberFinder memberFinder;

private final SlackService slackService;

Expand All @@ -64,13 +65,13 @@ public MemberCreateResponseDto updateV2(MemberCreateV2RequestDto request, Long m
@Transactional(readOnly = true)
public MemberGetResponseDto getMember(Long id)
{
return MemberGetResponseDto.of(findMemberById(id));
return MemberGetResponseDto.of(memberFinder.findById(id));
}

@Transactional(readOnly = true)
public MemberMyPageGetResponseDto getMypageMember(Long memberId) {
return MemberMyPageGetResponseDto.of(
findMemberById(memberId), getPhotoCount(memberId), getPhotos(memberId)
memberFinder.findById(memberId), getPhotoCount(memberId), getPhotos(memberId)
);
}

Expand All @@ -82,25 +83,25 @@ public MemberNicknameDuplicateResponseDto checkDuplicateMemberNickname(String ni
@Transactional(readOnly = true)
public MemberMyPageGetV2ResponseDto getMypageMemberV2(Long memberId) {
return MemberMyPageGetV2ResponseDto.of(
findMemberById(memberId), getPhotoCount(memberId)
memberFinder.findById(memberId), getPhotoCount(memberId)
);
}

@Transactional
public void logout(Long memberId) {
findMemberById(memberId).updateRefreshToken(null);
memberFinder.findById(memberId).updateRefreshToken(null);
}

private void updateMemberInfo(MemberCreateRequestDto request, Long memberId) {
Member member = findMemberById(memberId);
Member member = memberFinder.findByIdForUpdate(memberId);
member.updateRealName(request.getRealName());
member.updateNickname(request.getNickname());
member.generatePophoryId(RandomUtil.generateRandomString(8));
addAlbum(member, request.getAlbumCover());
}

private Long updateMemberInfoV2(MemberCreateV2RequestDto request, Long memberId) {
Member member = findMemberById(memberId);
Member member = memberFinder.findByIdForUpdate(memberId);
member.updateRealName(request.getRealName());
member.updateNickname(request.getNickname());
member.generatePophoryId(RandomUtil.generateRandomString(8));
Expand All @@ -115,12 +116,6 @@ private void checkNicknameDuplicate(String nickName) {
}
}

private Member findMemberById(Long id) {
return Objects.requireNonNull(memberJpaRepository.findByIdForUpdate(id)).orElseThrow(
() -> new EntityNotFoundException("Member가 존재하지 않습니다. id: " + id)
);
}

private void addAlbum(Member member, Long cover) {
Album album = new Album();
album.updateAlbumDesign(getAlbumDesignById(cover));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ interface MemberJpaRepository : JpaRepository<Member, Long> {

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select m from Member m where m.id = :id")
fun findByIdForUpdate(@Param("id") id: Long) : Optional<Member?>?
fun findByIdForUpdate(@Param("id") id: Long) : Optional<Member>

fun existsMemberByNickname(nickname: String?): Boolean
fun existsMemberBySocialIdAndSocialType(socialId: String?, socialType: SocialType?): Boolean

Expand Down

0 comments on commit 115e1be

Please sign in to comment.