Notice
Recent Posts
Recent Comments
Link
반응형
250x250
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 자바스크립트기초
- react
- CSS
- js
- Spring Boot
- springboot
- 자바스크립트기초문법
- 리액트프로젝트세팅
- 리액트세팅
- java
- 마이바티스
- 기초 코딩
- 처음만나는자바스크립트
- HTML
- 웹앱
- spring
- 자바스크립트
- 구글캘린더api
- mybatis
- 전자정부 서버세팅
- Javascript
- 코딩
- 스프링부트
- 자바스크립트 기초
- javaspring
- 웹
- 리액트초기세팅
- 구글 oauth
- 기초코딩
- 자바
Archives
- Today
- Total
인생 디벨로퍼
[Bank App] 12강 이체하기 본문
728x90
반응형
Dto 만들기
package shop.mtcoding.bankapp.dto.account;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class AccountTransferReqDto {
private Long amount;
private String wAccountNumber;
private String dAccountNumver;
private String wAccountPassword;
}
Controller
@PostMapping("/account/transfer")
public String transfer(AccountTransferReqDto accountTransferReqDto) {
User principal = (User) session.getAttribute("principal");
if (principal == null) {
throw new CustomException("로그인이 필요한 서비스입니다.", HttpStatus.UNAUTHORIZED);
}
if (accountTransferReqDto.getWAccountNumber().equals(accountTransferReqDto.getDAccountNumber())) {
throw new CustomException("출금계좌와 입금계좌가 동일할 수 없습니다", HttpStatus.BAD_REQUEST);
}
if (accountTransferReqDto.getAmount() == null) {
throw new CustomException("amount를 입력해주세요", HttpStatus.BAD_REQUEST);
}
if (accountTransferReqDto.getAmount().longValue() <= 0) {
throw new CustomException("출금액이 0원 이하일 수 없습니다.", HttpStatus.BAD_REQUEST);
}
if (accountTransferReqDto.getWAccountNumber() == null || accountTransferReqDto.getWAccountNumber().isEmpty()) {
throw new CustomException("출금 계좌번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
}
if (accountTransferReqDto.getDAccountNumber() == null || accountTransferReqDto.getDAccountNumber().isEmpty()) {
throw new CustomException("입금 계좌번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
}
if (accountTransferReqDto.getWAccountPassword() == null
|| accountTransferReqDto.getWAccountPassword().isEmpty()) {
throw new CustomException("계좌 비밀번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
}
int accountId = accountService.이체하기(accountTransferReqDto, principal.getId());
return "redirect:/account/" + accountId;
}
서비스 로직
- 출금 계좌존재 여부 확인
- 입금 계좌존재 여부 확인
- 출금 계좌패스워드 확인
- 출금 잔액확인
- 출금계좌 소유주 확인
- 출금
- 입금
- 거래내역 남기기
Service
@Transactional
public int 이체하기(AccountTransferReqDto accountTransferReqDto, int principalId) {
// 1. 출금 계좌존재 여부 확인
Account wAccountPS = accountRepository.findByNumber(accountTransferReqDto.getWAccountNumber());
if (wAccountPS == null) {
throw new CustomException("출금 계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
}
// 2. 입금 계좌존재 여부 확인
Account dAccountPS = accountRepository.findByNumber(accountTransferReqDto.getDAccountNumber());
if (dAccountPS == null) {
throw new CustomException("입금 계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
}
// 3. 출금 계좌패스워드 확인
wAccountPS.checkPassword(accountTransferReqDto.getWAccountPassword());
// 4. 출금 잔액확인
dAccountPS.checkPassword(accountTransferReqDto.getWAccountPassword());
// 5. 출금계좌 소유주 확인
wAccountPS.checkOwner(principalId);
// 6. 출금
wAccountPS.withdraw(accountTransferReqDto.getAmount());
accountRepository.updateById(wAccountPS);
// 7. 입금
dAccountPS.deposit(accountTransferReqDto.getAmount());
accountRepository.updateById(dAccountPS);
// 8. 거래내역 남기기
History history = new History();
history.setAmount(accountTransferReqDto.getAmount());
history.setWAccountId(wAccountPS.getId());
history.setDAccountId(dAccountPS.getId());
history.setWBalance(wAccountPS.getBalance());
history.setDBalance(dAccountPS.getBalance());
historyRepository.insert(history);
// 9. ID 리던
return wAccountPS.getId();
}
결과
728x90
반응형
'Project > 개인 Project - Bank App' 카테고리의 다른 글
[Bank App] 뱅크 프로젝트 정리 (0) | 2023.06.16 |
---|---|
[Bank App] 13강 계좌 상세보기 (0) | 2023.06.16 |
[Bank App] 11강 입금하기 (0) | 2023.06.16 |
[Bank App] 10강 출금하기 (0) | 2023.06.16 |
[Bank App] 9강 계좌목록보기 (1) | 2023.06.16 |