1. depositForm.jsp 주소 확인 및 name 속성 확인 
2. depositForm 코드 확인 
3. AccountController 입금 페이지 인증 검사 추가 
4. AccountController 입금 기능 데이터 파싱 및 인증,유효성 검사 처리  
5. AccountService 입금 기능 처리 구현 
6. 거래 내역 등록 처리

depositForm.jsp 코드 수정 및 name 속성 확인

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/view/layout/header.jsp"%>

<div class="col-sm-8">
	<h2>입금페이지(인증)</h2>
	<h5>어서오세요 환영합니다</h5>
		<div class="bg-light p-md-5 h-75">
		<form action="/account/deposit-proc" method="post">
			<div class="form-group">
				<label for="amount">입금 금액:</label>
				<input type="text" class="form-control" placeholder="입금금액을 입력하시오" id="amount" name="amount">
			</div>
			<div class="form-group">
				<label for="dAccountNumber">입금계좌번호:</label>
				<input type="text" class="form-control" placeholder="입금계좌번호" id="dAccountNumber" name="dAccountNumber">
			</div>
			<button type="submit" class="btn btn-primary">입금</button>
		</form>
	</div>
	<br>
</div>

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

입금 페이지 인증 검사 추가

AccountController

// 입금 페이지
@GetMapping("/deposit")
public String deposit() {
	
	if((User)session.getAttribute(Define.PRINCIPAL) == null) {
		throw new UnAuthorizedException("로그인 먼저 해주세요", HttpStatus.UNAUTHORIZED);
	}
	return "/account/depositForm";
}

DepositFormDto 코드 확인

package com.tenco.bank.dto;

import lombok.Data;

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

AccountController 입금 기능 추가

@PostMapping("/deposit-proc")
public String depositProc(DepositFormDto depositFormDto) {
	
	User principal = (User)session.getAttribute(Define.PRINCIPAL);
	if(principal == null) {
		throw new UnAuthorizedException("로그인 먼저 해주세요", HttpStatus.UNAUTHORIZED);
	}
	
	if(depositFormDto.getAmount() == null) {
		throw new CustomRestfullException("금액을 입력해 주세요", HttpStatus.BAD_REQUEST);
	}
	
	if(depositFormDto.getAmount().longValue() <= 0) {
		throw new CustomRestfullException("입금 금액이 0원 이하일 수 없습니다.", HttpStatus.BAD_REQUEST);
	}
	
	if(depositFormDto.getDAccountNumber() == null || 
			depositFormDto.getDAccountNumber().isEmpty()) {
		throw new CustomRestfullException("계좌 번호를 입력하세요", HttpStatus.BAD_REQUEST);
	}
	
	accountService.updateAccountDeposit(depositFormDto); 
	return "redirect:/account/list";
}

Account 모델 클래스 확인

package com.tenco.bank.repository.model;

import java.sql.Timestamp;
import lombok.Data;

/**
 * 모델 클래스 (Value Object 역할만 하는것은 아니다) 
 */
@Data
public class Account {
	
	private Integer id;
	private String number; 
	private String password; 
	private Long balance; 
	private Integer userId; 
	private Timestamp createdAt; 
	
	public void withdraw(Long amount) {
		this.balance -= amount;
	}
	
	public void deposit(Long amount) {
		this.balance += amount;
	}
	
}

AccountService 입금 기능 추가

// 입금 처리 기능 
// 트랜잭션 처리 
// 1 계좌 존재 여부 확인 -> select 
// 2 입금 처리 -> update 
// 3 거래 내역 등록 처리 -> insert 
@Transactional
public void updateAccountDeposit(DepositFormDto depositFormDto) {
	
	Account accountEntity = accountRepository.findByNumber(depositFormDto.getDAccountNumber());
	if(accountEntity == null) {
		throw new CustomRestfullException("해당 계좌가 존재하지 않습니다", HttpStatus.INTERNAL_SERVER_ERROR);
	}
	
	// 객체 상태값 변경 
	accountEntity.deposit(depositFormDto.getAmount());
	accountRepository.updateById(accountEntity);
	
	History history = new History();
	history.setAmount(depositFormDto.getAmount());
	history.setWBalance(null);
	history.setDBalance(accountEntity.getBalance());
	history.setWAccountId(null);
	history.setDAccountId(accountEntity.getId());
	
	int resultRowCount = historyRepository.insert(history);
	if(resultRowCount != 1) {
		throw new CustomRestfullException("정상 처리가 되지 않았습니다", HttpStatus.INTERNAL_SERVER_ERROR);
	}
}

MyBatis 확인

package com.tenco.bank.repository.interfaces;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;

import com.tenco.bank.repository.model.Account;

@Mapper // mybatis 연결 처리 
public interface AccountRepository {
	
	public int insert(Account account);
	public int updateById(Account account); 
	public int deleteById(int id); 
	public List<Account> findAll();   
	public Account findById(int id);
	public List<Account> findByUserId(Integer userId);
	public Account findByNumber(String number); 
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.tenco.bank.repository.interfaces.AccountRepository"> 
	
	<insert id="insert">
		insert into account_tb(number, password, balance, user_id, created_at)
		values(#{number}, #{password}, #{balance}, #{userId}, now())
	</insert>	
	
	<update id="updateById">
		update account_tb set number = #{number}, password = #{password},
			balance = #{balance}, user_id = #{userId} where id = #{id}
	</update>
	
	<delete id="deleteById">
		delete from account_tb where id = #{id}
	</delete>
	
	<select id="findById" resultType="com.tenco.bank.repository.model.Account">
		select * from account_tb where id = #{id}
	</select>
	
	<select id="findAll" resultType="com.tenco.bank.repository.model.Account">
		select * from account_tb 
	</select>
	
	<select id="findByUserId" resultType="com.tenco.bank.repository.model.Account">
		select * from account_tb where user_id = #{userId} 
	</select>
	
	<select id="findByNumber"  resultType="com.tenco.bank.repository.model.Account">
		select * from account_tb where number = #{number}
	</select>
	
</mapper>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
	namespace="com.tenco.bank.repository.interfaces.HistoryRepository">

	<insert id="insert"
		parameterType="com.tenco.bank.repository.model.History">
		insert into
		history_tb(
		amount, w_balance, d_balance,
		w_account_id, d_account_id
		)
		values(
		#{amount}, #{wBalance}, #{dBalance},
		#{wAccountId}, #{dAccountId}
		)
	</insert>

	<update id="updateById"
		parameterType="com.tenco.bank.repository.model.History">
		update history_tb
		set amount = #{amount},
		w_balance =	#{wBalance},
		d_balace = #{dBalance},
		w_account_id = #{wAccountId},
		d_account_id = #{dAccountId}
		where id = #{id}
		
	</update>

	<delete id="deleteById" parameterType="int">
		delete from history_tb
		where id = #{id}
	</delete>
	
	<select id="findById" resultType="com.tenco.bank.repository.model.History">
		select * from history_tb where id = #{id}
	</select>

	<select id="findAll" resultType="com.tenco.bank.repository.model.History">
		select * from history_tb
	</select>


</mapper>

+ Recent posts