From e5bb184dd3e1ea74cabee4bfdea1584458f11afe Mon Sep 17 00:00:00 2001 From: ezeun Date: Thu, 18 Apr 2024 20:07:13 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat=20:=20Member=20Service=20=EC=9E=91?= =?UTF-8?q?=EC=84=B1=20(#47)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 회원 가입 * 전체 회원 조회 * 회원 한 명 조회 * 회원 정보 변경 * 회원 탈퇴 --- .../domain/member/service/MemberService.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java diff --git a/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java new file mode 100644 index 0000000..b0a87f8 --- /dev/null +++ b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java @@ -0,0 +1,62 @@ +package LeejuButU.BidCycle.domain.member.service; + +import LeejuButU.BidCycle.domain.member.domain.Member; +import LeejuButU.BidCycle.domain.member.repository.MemberRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Optional; + +@Service +@Transactional +@RequiredArgsConstructor +public class MemberService { + + private final MemberRepository memberRepository; + + //회원 가입 + public Long join(Member member){ + validateDuplicateMember(member); + memberRepository.save(member); + return member.getMemberId(); + + } + private void validateDuplicateMember(Member member) { + memberRepository.findByNickname(member.getNickname()) + .ifPresent(m -> { + throw new IllegalStateException("이미 존재하는 회원입니다."); + }); + } + + //전체 회원 조회 + public List findMembers(){ + return memberRepository.findAll(); + } + + //회원 한 명 조회 + public Optional findOne(Long memberId){ + return memberRepository.findById(memberId); + } + + // 회원 정보 변경 + public void updateMember(Long memberId, Member updatedMember) { + Member member = memberRepository.findById(memberId).orElseThrow(); + + // 변경할 정보를 새로운 정보로 업데이트 + member.updatePassword(updatedMember.getPassword()); + member.updateNickname(updatedMember.getNickname()); + member.updateTown(updatedMember.getTown()); + + // 변경된 정보를 저장 + memberRepository.save(member); + } + + // 회원 탈퇴 + public void withdrawMember(Long memberId) { + Member member = memberRepository.findById(memberId).orElseThrow(); + memberRepository.delete(member); + } + +} From b3b00f2eafedbef5fbf5c99b0a9425f3c89687c8 Mon Sep 17 00:00:00 2001 From: ezeun Date: Sat, 20 Apr 2024 00:57:29 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor=20:=20Member=20Service=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#49)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor : @Transactional(readOnly = true)를 기본값으로 변경 * refactor : 회원가입 중복회원 체크 방법 변경 * refactor : orElseThrow에 에러와 메세지 명시 --- .../domain/member/service/MemberService.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java index b0a87f8..5a0d536 100644 --- a/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java +++ b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java @@ -10,24 +10,20 @@ import java.util.Optional; @Service -@Transactional +@Transactional(readOnly = true) @RequiredArgsConstructor public class MemberService { private final MemberRepository memberRepository; //회원 가입 + @Transactional(readOnly = false) public Long join(Member member){ - validateDuplicateMember(member); + if(memberRepository.existsById(member.getMemberId())){ + throw new IllegalStateException("이미 존재하는 회원입니다."); + } memberRepository.save(member); return member.getMemberId(); - - } - private void validateDuplicateMember(Member member) { - memberRepository.findByNickname(member.getNickname()) - .ifPresent(m -> { - throw new IllegalStateException("이미 존재하는 회원입니다."); - }); } //전체 회원 조회 @@ -36,13 +32,16 @@ public List findMembers(){ } //회원 한 명 조회 - public Optional findOne(Long memberId){ - return memberRepository.findById(memberId); + public Member findOne(Long memberId){ + return memberRepository.findById(memberId) + .orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다.")); } // 회원 정보 변경 + @Transactional(readOnly = false) public void updateMember(Long memberId, Member updatedMember) { - Member member = memberRepository.findById(memberId).orElseThrow(); + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다.")); // 변경할 정보를 새로운 정보로 업데이트 member.updatePassword(updatedMember.getPassword()); @@ -54,8 +53,10 @@ public void updateMember(Long memberId, Member updatedMember) { } // 회원 탈퇴 + @Transactional(readOnly = false) public void withdrawMember(Long memberId) { - Member member = memberRepository.findById(memberId).orElseThrow(); + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new IllegalArgumentException("해당 회원을 찾을 수 없습니다.")); memberRepository.delete(member); } From 49984316d41b79afd64781913f35d9cc656a4420 Mon Sep 17 00:00:00 2001 From: ezeun Date: Sat, 20 Apr 2024 01:23:06 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor=20:=20Member=20Service=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#47)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refatcor : @Transactional(readOnly = false)는 괄호 부분 생략 * refactor : findByIdOrThrow() 함수 작성해서 중복 제거 --- .../domain/member/service/MemberService.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java index 5a0d536..c7bbe35 100644 --- a/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java +++ b/src/main/java/LeejuButU/BidCycle/domain/member/service/MemberService.java @@ -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("이미 존재하는 회원입니다."); @@ -33,15 +39,13 @@ public List 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()); @@ -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); }