Spring boot

Bank App 만들기 - 입금 기능

ryeonng 2024. 8. 13. 17:42

 

생성 될 파일 확인

 

결과 화면 미리 보기

 

deposit.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>
	<form action="/account/deposit" 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="dAccountNumber">입금 계좌 번호:</label> <input type="text" class="form-control" placeholder="Enter account number" id="dAccountNumber" name="dAccountNumber" value="1111">
        </div>
        <div class="text-right">
            <button type="submit" class="btn btn-primary">입금</button>
        </div>
    </form>
</div>
<!-- end of col-sm-8  -->
</div>
</div>
<!-- end of content.jsp(xxx.jsp)   -->

<!-- footer.jsp  -->
<%@ include file="/WEB-INF/view/layout/footer.jsp"%>

 

DepositDTO
package com.tenco.bank.dto;

import lombok.Data;

@Data
public class DepositDTO {
	
	private Long amount; 
	private String dAccountNumber; 
}

 

AccountService - 입금 기능
@Transactional
	// 입금 기능 만들기
	public void updateAccountDeposit(DepositDTO dto, Integer principalId) {
		// 계좌 존재 여부 확인
		Account accountEntity = accountRepository.findByNumber(dto.getDAccountNumber());
		if(accountEntity == null ) {
			throw new DataDeliveryException(Define.NOT_EXIST_ACCOUNT, HttpStatus.BAD_REQUEST);
		}
		
		// 본인 계좌 여부 확인 - 입금 기능에도 필요 (입금자명)
		accountEntity.checkOwner(principalId);
		
		// 입금 처리 -- update
		accountEntity.deposit(dto.getAmount());
		accountRepository.updateById(accountEntity);
		
		// 6. 거래 내역 등록 -- insert
		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);
		}
	}

 

account.xml

	<update id="updateById">
		update account_tb set number = #{number}, password = #{password},
			balance = #{balance}, user_id = #{userId} where id = #{id}
	</update>