- [Springboot] 19. 출금 기능 만들기2024년 08월 08일
- Song hyun
- 작성자
- 2024.08.08.:49
728x90반응형1. 출금 화면 만들기 (withdrawal.jsp)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!-- header.jsp --> <%@ include file="/WEB-INF/view/layout/header.jsp"%> <!-- start of content.jsp(xxx.jsp) --> <div class="col-sm-8"> <h2>출금 요청(인증)</h2> <h5>Bank App에 오신걸 환영합니다</h5> <!-- 예외적으로 로그인은 보안 때문에 post로 던지자 --> <!-- insert into account_tb(number, password, balance, user_id, created_at) --> <form action="/account/withdrawal" method="post"> <div class="form-group"> <label for="amount">출금 금액:</label> <input type="number" class="form-control" placeholder="Enter amount" id="amount" name="amount" value="1000" > </div> <div class="form-group"> <label for="wAccountNumber">출금 계좌:</label> <input type="text" class="form-control" placeholder="Enter wAccountNumber" id="wAccountNumber" name="wAccountNumber" value="1111"> </div> <div class="form-group"> <label for="pwd">출금 비밀번호:</label> <input type="password" class="form-control" placeholder="Enter pwd" id="pwd" name="password" value="1234" > </div> <div class="text-right"> <button type="submit" class="btn btn-primary">출금 요청</button> </div> </form> </div> <!-- end of col-sm-8 --> </div> </div> <!-- footer.jsp --> <%@ include file="/WEB-INF/view/layout/footer.jsp"%>
2. 출금 요청 페이지 이동 메서드(@GetMapping)
/** * 출금 페이지 요청 * @return */ @GetMapping("/withdrawal") public String withDrawalPage() { User principal=(User)session.getAttribute("principal"); if(principal==null) { throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED); } return "account/withdrawal"; }
3. 출금 요청 기능 메서드(@PostMapping)
/** * 계좌 출금 요청 * @param dto * @return */ @PostMapping("/withdrawal") public String withDrawalProc(WithdrawalDTO dto) { // 1. 인증 검사 User principal=(User)session.getAttribute("principal"); if(principal==null) { throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED); } // 유효성 검사 (자바 코드를 개발) --> 스프링부트 @Valid 라이브러리가 존재한다. if(dto.getAmount()==null) { throw new DataDeliveryException(Define.ENTER_YOUR_BALANCE, HttpStatus.BAD_REQUEST); } if(dto.getAmount().longValue()<=0) { throw new DataDeliveryException(Define.W_BALANCE_VALUE, HttpStatus.BAD_REQUEST); } if(dto.getWAccountNumber()==null) { throw new DataDeliveryException(Define.ENTER_YOUR_ACCOUNT_NUMBER, HttpStatus.BAD_REQUEST); } if(dto.getWAccountPassword()==null) { throw new DataDeliveryException(Define.ENTER_YOUR_PASSWORD, HttpStatus.BAD_REQUEST); } accountService.updateAccountWithdrawal(dto, principal.getId()); return "redirect:/account/list"; }
4. 출금 쿼리문 전송 메서드 (updateAccountWithdrawal())
// 한번에 모든 기능을 생각하는 건 힘들다. // 1. 계좌 존재 여부를 확인 -- select (Account) // 2. 본인 계좌 여부를 확인 -- select (1의 결과를 토대로 검증)(=객체 상태값에서 비교) // 3. 계좌 비밀번호 확인 -- 객체 상태값에서 일치 여부 확인 // 4. 잔액 여부 확인 -- 객체 상태값에서 확인 // 5. 출금 처리 -- Update 쿼리 전송 // 6. hitory에 거래 내역 등록 -- insert(history_tb) // 7. Transaction 처리 @Transactional public void updateAccountWithdrawal(WithdrawalDTO dto, Integer principalId) { // 1. 계좌 존재 여부를 확인 Account accountEntity = accountRepository.findByNumber(dto.getWAccountNumber()); if(accountEntity==null) { throw new DataDeliveryException(Define.NOT_EXIST_ACCOUNT, HttpStatus.BAD_REQUEST); } // 2. accountEntity.checkOwner(principalId); // 3. accountEntity.checkPassword(dto.getWAccountPassword()); // 4. accountEntity.checkBalance(dto.getAmount()); // 5. 출금 처리 // accountEntity 객체의 잔액을 변경하고, 업데이트 처리해야 한다. accountEntity.withdraw(dto.getAmount()); accountRepository.updateById(accountEntity); // 6. history에 거래 내역 등록 History history=new History(); history.setAmount(dto.getAmount()); history.setWBalance(accountEntity.getBalance()); history.setDBalance(null); history.setWAccountId(accountEntity.getId()); history.setDAccountId(null); int rowResultCount=historyRepository.insert(history); if(rowResultCount!=1) { throw new DataDeliveryException(Define.FAILED_PROCESSING, HttpStatus.INTERNAL_SERVER_ERROR); } System.out.println("history 확인 : "+history); // 7. Transaction 처리 }
728x90반응형'Springboot' 카테고리의 다른 글
[Springboot] 21. 계좌 상세 보기 기능 (1단계) (0) 2024.08.09 [Springboot] 20. 입금 기능 만들기 (0) 2024.08.08 [SpringBoot] 18. 중간 리팩토링 (0) 2024.08.08 [SpringBoot] 17. 계좌 목록 기능 구현 (0) 2024.08.08 [SpringBoot] **16. 중간점검 및 헷갈리는 부분들 정리(1) (0) 2024.08.07 다음글이전글이전 글이 없습니다.댓글
스킨 업데이트 안내
현재 이용하고 계신 스킨의 버전보다 더 높은 최신 버전이 감지 되었습니다. 최신버전 스킨 파일을 다운로드 받을 수 있는 페이지로 이동하시겠습니까?
("아니오" 를 선택할 시 30일 동안 최신 버전이 감지되어도 모달 창이 표시되지 않습니다.)