From 8fa957871ecb2213a9db755d4f49d5205193d3d7 Mon Sep 17 00:00:00 2001 From: kimhobeen Date: Wed, 27 Sep 2023 15:50:02 +0900 Subject: [PATCH] =?UTF-8?q?:recycle:=20refactor=20:=20member=20account=20?= =?UTF-8?q?=EA=B4=80=EA=B3=84=20=EC=9E=AC=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/domain/account/domain/Account.java | 15 ++-- .../account/repository/AccountRepository.java | 2 +- .../adjustment/service/AdjustmentService.java | 5 +- .../server/domain/member/entity/Member.java | 1 + .../service/AdjustmentServiceTest.java | 71 +++++++++++++++++++ .../server/global/testhelper/ServiceTest.java | 2 + 6 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 Server/src/test/java/com/server/domain/adjustment/service/AdjustmentServiceTest.java diff --git a/Server/src/main/java/com/server/domain/account/domain/Account.java b/Server/src/main/java/com/server/domain/account/domain/Account.java index 3c5b198a..0798bf71 100644 --- a/Server/src/main/java/com/server/domain/account/domain/Account.java +++ b/Server/src/main/java/com/server/domain/account/domain/Account.java @@ -23,22 +23,23 @@ public class Account extends BaseEntity { private String bank; - @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "member_id") - private Member member; - - private Account(String name, String account, String bank, Member member) { + private Account(String name, String account, String bank) { this.name = name; this.account = account; this.bank = bank; - this.member = member; } public static Account createAccount(String name, String accountNumber, String bank, Member member) { - Account account = new Account(name, accountNumber, bank, member); + Account account = new Account(name, accountNumber, bank); member.updateAccount(account); return account; } + + public void updateAccount(String name, String account, String bank) { + this.name = name; + this.account = account; + this.bank = bank; + } } diff --git a/Server/src/main/java/com/server/domain/account/repository/AccountRepository.java b/Server/src/main/java/com/server/domain/account/repository/AccountRepository.java index 238a680f..a9a75dc1 100644 --- a/Server/src/main/java/com/server/domain/account/repository/AccountRepository.java +++ b/Server/src/main/java/com/server/domain/account/repository/AccountRepository.java @@ -8,6 +8,6 @@ public interface AccountRepository extends JpaRepository { - @Query("select a from Account a where a.member.memberId = :memberId") + @Query("select a from Account a join Member m on m.account = a where m.memberId = :memberId") Optional findByMemberId(Long memberId); } \ No newline at end of file diff --git a/Server/src/main/java/com/server/domain/adjustment/service/AdjustmentService.java b/Server/src/main/java/com/server/domain/adjustment/service/AdjustmentService.java index fa7e1b90..718896f4 100644 --- a/Server/src/main/java/com/server/domain/adjustment/service/AdjustmentService.java +++ b/Server/src/main/java/com/server/domain/adjustment/service/AdjustmentService.java @@ -157,7 +157,10 @@ public void updateAccount(Long loginMemberId, AccountUpdateServiceRequest reques if(account == null) { Member member = verifiedMember(loginMemberId); - Account.createAccount(request.getName(), request.getAccount(), request.getBank(), member); + Account createdAccount = Account.createAccount(request.getName(), request.getAccount(), request.getBank(), member); + accountRepository.save(createdAccount); + }else { + account.updateAccount(request.getName(), request.getAccount(), request.getBank()); } } diff --git a/Server/src/main/java/com/server/domain/member/entity/Member.java b/Server/src/main/java/com/server/domain/member/entity/Member.java index da5bab95..7c742b18 100644 --- a/Server/src/main/java/com/server/domain/member/entity/Member.java +++ b/Server/src/main/java/com/server/domain/member/entity/Member.java @@ -67,6 +67,7 @@ public class Member extends BaseEntity { private Channel channel; @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JoinColumn(name = "account_id") private Account account; @OneToMany(mappedBy = "member", cascade = CascadeType.ALL) diff --git a/Server/src/test/java/com/server/domain/adjustment/service/AdjustmentServiceTest.java b/Server/src/test/java/com/server/domain/adjustment/service/AdjustmentServiceTest.java new file mode 100644 index 00000000..0b275f6b --- /dev/null +++ b/Server/src/test/java/com/server/domain/adjustment/service/AdjustmentServiceTest.java @@ -0,0 +1,71 @@ +package com.server.domain.adjustment.service; + +import com.server.domain.account.domain.Account; +import com.server.domain.adjustment.service.dto.request.AccountUpdateServiceRequest; +import com.server.domain.member.entity.Member; +import com.server.global.testhelper.ServiceTest; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Collection; +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.DynamicTest.*; + +class AdjustmentServiceTest extends ServiceTest { + + @Autowired AdjustmentService adjustmentService; + + @TestFactory + @DisplayName("계좌 정보를 수정한다.") + Collection updateAccount() { + //given + Member member = createMemberWithChannel(); + + em.flush(); + em.clear(); + + return List.of( + dynamicTest("계좌정보가 없다면 생성한다.", ()-> { + //given + String accountNumber = "123456789"; + String bank = "신한은행"; + String name = "홍길동"; + + AccountUpdateServiceRequest request = new AccountUpdateServiceRequest(name, accountNumber, bank); + + //when + adjustmentService.updateAccount(member.getMemberId(), request); + + //then + Account account = accountRepository.findByMemberId(member.getMemberId()).orElseThrow(); + assertThat(account.getAccount()).isEqualTo(accountNumber); + }), + dynamicTest("계좌정보가 있다면 수정한다.", ()-> { + //given + String accountNumber = "1234567892"; + String bank = "신한은행2"; + String name = "홍길동2"; + + AccountUpdateServiceRequest request = new AccountUpdateServiceRequest(name, accountNumber, bank); + + //when + adjustmentService.updateAccount(member.getMemberId(), request); + + //then + Account account = accountRepository.findByMemberId(member.getMemberId()).orElseThrow(); + assertThat(account.getAccount()).isEqualTo(accountNumber); + assertThat(accountRepository.findAll().size()).isEqualTo(1); + }) + ); + + + } +} \ No newline at end of file diff --git a/Server/src/test/java/com/server/global/testhelper/ServiceTest.java b/Server/src/test/java/com/server/global/testhelper/ServiceTest.java index 588a5f61..3c999982 100644 --- a/Server/src/test/java/com/server/global/testhelper/ServiceTest.java +++ b/Server/src/test/java/com/server/global/testhelper/ServiceTest.java @@ -2,6 +2,7 @@ import com.server.auth.jwt.service.CustomUserDetails; import com.server.auth.jwt.service.JwtProvider; +import com.server.domain.account.repository.AccountRepository; import com.server.domain.announcement.repository.AnnouncementRepository; import com.server.domain.answer.entity.Answer; import com.server.domain.answer.repository.AnswerRepository; @@ -78,6 +79,7 @@ public abstract class ServiceTest { @Autowired protected CartRepository cartRepository; @Autowired protected RewardRepository rewardRepository; @Autowired protected ReportRepository reportRepository; + @Autowired protected AccountRepository accountRepository; @Autowired protected EntityManager em; @MockBean protected RedisService redisService;