Skip to content

Commit

Permalink
Merge pull request #504 from codestates-seb/be/feat/adjustment/#494
Browse files Browse the repository at this point in the history
[BE] โ™ป๏ธ refactor : member account ๊ด€๊ณ„ ์žฌ์„ค์ •
  • Loading branch information
hobeen-kim authored Sep 27, 2023
2 parents aa3760a + 8fa9578 commit 10be853
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

public interface AccountRepository extends JpaRepository<Account, Long> {

@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<Account> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<DynamicTest> 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);
})
);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 10be853

Please sign in to comment.