Skip to content

Commit

Permalink
refactor : Member Service 수정 (#47)
Browse files Browse the repository at this point in the history
* refatcor : @transactional(readOnly = false)는 괄호 부분 생략

* refactor : findByIdOrThrow() 함수 작성해서 중복 제거
  • Loading branch information
ezeun committed Apr 19, 2024
1 parent b3b00f2 commit 4998431
Showing 1 changed file with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ public class MemberService {

private final MemberRepository memberRepository;

//회원ID로 회원 조회 (회원이 존재하지 않으면 예외 발생)
private Member findByIdOrThrow(Long memberId){
return memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다."));
}

//회원 가입
@Transactional(readOnly = false)
@Transactional
public Long join(Member member){
if(memberRepository.existsById(member.getMemberId())){
throw new IllegalStateException("이미 존재하는 회원입니다.");
Expand All @@ -33,15 +39,13 @@ public List<Member> findMembers(){

//회원 한 명 조회
public Member findOne(Long memberId){
return memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다."));
return findByIdOrThrow(memberId);
}

// 회원 정보 변경
@Transactional(readOnly = false)
@Transactional
public void updateMember(Long memberId, Member updatedMember) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다."));
Member member = findByIdOrThrow(memberId);

// 변경할 정보를 새로운 정보로 업데이트
member.updatePassword(updatedMember.getPassword());
Expand All @@ -53,10 +57,9 @@ public void updateMember(Long memberId, Member updatedMember) {
}

// 회원 탈퇴
@Transactional(readOnly = false)
@Transactional
public void withdrawMember(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다."));
Member member = findByIdOrThrow(memberId);
memberRepository.delete(member);
}

Expand Down

0 comments on commit 4998431

Please sign in to comment.