-
Notifications
You must be signed in to change notification settings - Fork 1
#18 송금 요청 기능 #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#18 송금 요청 기능 #25
Changes from 15 commits
2f7ba51
8646f67
cc0a7dd
ee58548
6c9284d
fb32e3e
b63ede1
235f43d
f7327ed
043d5ee
24b9b55
f85a008
86efbeb
4d8c037
dd3f346
0e1c691
1786e55
43d7744
51c9278
f9159ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,12 +9,33 @@ public interface AccountRepository { | |
| * @param account 생성할 계좌 | ||
| * @return 생성된 계좌의 ID | ||
| */ | ||
| Long create(Account account); | ||
| Long create(final Account account); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
|
||
| /** | ||
| * 회원 ID로 계좌를 조회합니다. | ||
| * @param memberId 회원 ID | ||
| * @return 조회된 계좌 | ||
| */ | ||
| Optional<Account> findByMemberId(Long memberId); | ||
| Optional<Account> findByMemberId(final Long memberId); | ||
|
|
||
| /** | ||
| * 회원 ID로 계좌를 조회하고, 조회된 계좌에 배타적 잠금(Exclusive Lock)을 겁니다. | ||
| * @param memberId 회원 ID | ||
| * @return 잠금이 설정된 계좌(Optional) | ||
| */ | ||
| Optional<Account> findByIdForUpdate(final Long memberId); | ||
|
|
||
| /** | ||
| * 계좌의 대기 중인 금액을 업데이트합니다. | ||
| * @param id 계좌 ID | ||
| * @param pendingAmount 대기 중인 금액 | ||
| */ | ||
| void updatePendingAmount(final Long id, final Long pendingAmount); | ||
|
|
||
| /** | ||
| * 계좌의 잔액을 업데이트합니다. | ||
| * @param id 계좌 ID | ||
| * @param balance 잔액 | ||
| */ | ||
| void updateBalance(final Long id, final Long balance); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package com.donggi.sendzy.account.domain; | ||
|
|
||
| import com.donggi.sendzy.member.exception.MemberNotFoundException; | ||
| import com.donggi.sendzy.account.exception.AccountNotFoundException; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.stereotype.Service; | ||
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
@@ -14,6 +14,24 @@ public class AccountService { | |
| @Transactional(readOnly = true) | ||
| public Account getByMemberId(final Long memberId) { | ||
| return accountRepository.findByMemberId(memberId) | ||
| .orElseThrow(() -> new MemberNotFoundException("회원을 찾을 수 없습니다. :" + memberId)); | ||
| .orElseThrow(() -> new AccountNotFoundException(memberId)); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void withdraw(final Account account, final Long amount) { | ||
| account.withdraw(amount); | ||
| accountRepository.updatePendingAmount(account.getId(), account.getPendingAmount()); | ||
| } | ||
|
|
||
| @Transactional | ||
| public void deposit(final Account account, final Long amount) { | ||
| account.deposit(amount); | ||
| accountRepository.updateBalance(account.getId(), account.getBalance()); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public Account findByIdForUpdate(final Long memberId) { | ||
| return accountRepository.findByIdForUpdate(memberId) | ||
| .orElseThrow(() -> new AccountNotFoundException(memberId)); | ||
|
Comment on lines
+32
to
+35
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반드시 대상을 조회해야 하는 경우에는 또한, 불필요하게 메서드 파라미터로 Wrapper Type을 받고 있습니다.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 데이터가 조회되지 않을 경우 예외를 던지는 현재 흐름에서는 말씀 주신 것처럼 평소에 관성적으로 그리고 현재 메서드에서 파라미터에 Wrapper 타입을 사용할 필요는 없는데, 이런 부분도 수정하고 이후에는 기본형을 우선적으로 고려하는 습관을 들이도록 하겠습니다! |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package com.donggi.sendzy.account.exception; | ||
|
|
||
| import com.donggi.sendzy.common.exception.NotFoundException; | ||
|
|
||
| public class AccountNotFoundException extends NotFoundException { | ||
| public AccountNotFoundException(final Long memberId) { | ||
| super("계좌를 찾을 수 없습니다. :" + memberId); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| package com.donggi.sendzy.account.exception; | ||
|
|
||
| public class InvalidWithdrawalException extends RuntimeException { | ||
| public InvalidWithdrawalException() { | ||
| super("잔액이 부족합니다."); | ||
| } | ||
|
|
||
| public InvalidWithdrawalException(final Long amount) { | ||
| super("송금액은 0원 이상이어야 합니다. 사용자 요청 금액: " + amount); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.donggi.sendzy.common.exception; | ||
|
|
||
| public class BadRequestException extends RuntimeException { | ||
| public BadRequestException(final String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.donggi.sendzy.common.exception; | ||
|
|
||
| public class NotFoundException extends RuntimeException { | ||
| public NotFoundException(final String message) { | ||
| super(message); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
amount <= 0/this.balance < amount/this.balance < this.pendingAmount + amount은 어떤 조건을 검증할것인지에 대한 명시성이 부족합니다. 단순히 어떤 값이 어떤 형태여야한다는 조건이 아닌, validation이라는 비즈니스 검증 로직이 들어갔으므로 어떤 형태의 검증 요구사항을 수행하는 것인지 명확하게 표현해야 검증에 대한 코드를 읽기 수월해집니다.예를 들어, 이런식으로 표현할 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
출금 검증 로직을 메서드로 추출해서 의미를 명확하게 표현하도록 변경했습니다!