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
- Javascript
- 리액트프로젝트세팅
- 전자정부 서버세팅
- springboot
- HTML
- 자바스크립트
- 리액트세팅
- 코딩
- CSS
- 처음만나는자바스크립트
- react
- 기초코딩
- 자바
- spring
- mybatis
- 웹앱
- 구글캘린더api
- 리액트초기세팅
- js
- 자바스크립트기초문법
- javaspring
- 자바스크립트 기초
- java
- 구글 oauth
- 스프링부트
- 마이바티스
- 웹
- 기초 코딩
- 자바스크립트기초
- Spring Boot
Archives
- Today
- Total
인생 디벨로퍼
[Bank App] 11강 입금하기 본문
728x90
반응형
Dto
package shop.mtcoding.bankapp.dto.account;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class AccountDepositReqDto {
private Long amount;
private String dAccountNumber;
}
Controller
@PostMapping("/account/deposit")
public String deposit(AccountDepositReqDto accountDepositReqDto) {
if (accountDepositReqDto.getAmount() == null) {
throw new CustomException("amount를 입력해주세요", HttpStatus.BAD_REQUEST);
}
if (accountDepositReqDto.getAmount().longValue() <= 0) {
throw new CustomException("출금액이 0원 이하일 수 없습니다.", HttpStatus.BAD_REQUEST);
}
if (accountDepositReqDto.getDAccountNumber() == null || accountDepositReqDto.getDAccountNumber().isEmpty()) {
throw new CustomException("계좌번호를 입력해 주세요", HttpStatus.BAD_REQUEST);
}
accountService.입금하기(accountDepositReqDto);
return "redirect:/";
}
Service
Account 모델에 의미있는 메소드 만들기 (account Service 전체 리펙토링함)
package shop.mtcoding.bankapp.model.account;
import java.sql.Timestamp;
import org.springframework.http.HttpStatus;
import lombok.Getter;
import lombok.Setter;
import shop.mtcoding.bankapp.handler.ex.CustomException;
@Setter
@Getter
public class Account {
private Integer id;
private String number;
private String password;
private Long balance;
private Integer userId;
private Timestamp createdAt;
public void deposit(Long amount) {
this.balance = this.balance - amount;
}
public void withdraw(Long amount) {
this.balance = this.balance - amount;
}
public void checkPassword(String password) {
if (!this.password.equals(password)) {
throw new CustomException("출금계좌 비밀번호가 잘못되었습니다", HttpStatus.BAD_REQUEST);
}
}
}
package shop.mtcoding.bankapp.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.mtcoding.bankapp.dto.account.AccountDepositReqDto;
import shop.mtcoding.bankapp.dto.account.AccountSaveReqDto;
import shop.mtcoding.bankapp.dto.account.AccountWithdrawReqDto;
import shop.mtcoding.bankapp.handler.ex.CustomException;
import shop.mtcoding.bankapp.model.account.Account;
import shop.mtcoding.bankapp.model.account.AccountRepository;
import shop.mtcoding.bankapp.model.history.History;
import shop.mtcoding.bankapp.model.history.HistoryRepository;
@Service
public class AccountService {
@Autowired
private AccountRepository accountRepository;
@Autowired
private HistoryRepository historyRepository;
@Transactional
public void 입금하기(AccountDepositReqDto accountDepositReqDto) {
// 1. 입금계좌 존재 여부
Account accountPS = accountRepository.findByNumber(accountDepositReqDto.getDAccountNumber());
if (accountPS == null) {
throw new CustomException("계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
}
// 2. 입금하기
accountPS.deposit(accountDepositReqDto.getAmount());
accountRepository.updateById(accountPS);
// 3. 히스토리 (거래내역)
History history = new History();
history.setAmount(accountDepositReqDto.getAmount());
history.setWAccountId(null);
history.setDAccountId(accountPS.getId());
history.setWBalance(null);
history.setDBalance(accountPS.getBalance());
historyRepository.insert(history);
}
@Transactional
public void 계좌생성(AccountSaveReqDto accountSaveReqDto, int principalId) {
Account account = accountSaveReqDto.toModel(principalId);
accountRepository.insert(account);
}
@Transactional
public int 계좌출금(AccountWithdrawReqDto accountWithdrawReqDto) {
// 1. 계좌존재 여부
Account accountPS = accountRepository.findByNumber(accountWithdrawReqDto.getWAccountNumber());
if (accountPS == null) {
throw new CustomException("계좌가 존재하지 않습니다", HttpStatus.BAD_REQUEST);
}
// 2. 계좌 패스워드 확인
accountPS.checkPassword(accountWithdrawReqDto.getWAccountPassword());
// 3. 잔액 확인
if (accountPS.getBalance() < accountWithdrawReqDto.getAmount())
{
throw new CustomException("잔액이 부족합니다", HttpStatus.BAD_REQUEST);
}
// 4. 출금
accountPS.withdraw(accountWithdrawReqDto.getAmount());
accountRepository.updateById(accountPS);
// 5. 히스토리 (거래내역)
History history = new History();
history.setAmount(accountWithdrawReqDto.getAmount());
history.setWAccountId(accountPS.getId());
history.setDAccountId(null);
history.setWBalance(accountPS.getBalance());
history.setDBalance(null);
historyRepository.insert(history);
// 6. 해당 계좌 ID 리턴
return accountPS.getId();
}
}
반복되는 코드를 줄이기 위해, 메소드로 만들어 전체 코드 리펙토링.
결과
728x90
반응형
'Project > 개인 Project - Bank App' 카테고리의 다른 글
[Bank App] 13강 계좌 상세보기 (0) | 2023.06.16 |
---|---|
[Bank App] 12강 이체하기 (0) | 2023.06.16 |
[Bank App] 10강 출금하기 (0) | 2023.06.16 |
[Bank App] 9강 계좌목록보기 (1) | 2023.06.16 |
[Bank App] 8강 계좌생성하기 (0) | 2023.06.14 |