💡 학습 목표
1. depositForm.jsp 주소 확인 및 name 속성 확인
2. depositForm 확인
3. AccountController 인증검사, 유효성 검사
4. AccountService 서비스 처리
5. 거래내역 등록
<%@ 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">
<div class="form-group">
<form action="/account/deposit" method="post">
<div class="form-group">
<label for="amount">입금 금액</label>
<input type="text" id="amount" class="form-control" placeholder="입금 금액을 입력하세요" name="amount">
</div>
<div class="form-group">
<label for="dAccountNumber">입금 계좌번호</label>
<input type="text" id="dAccountNumber" class="form-control" placeholder="입금 계좌번호를 입력하세요" name="dAccountNumber">
</div>
<button type="submit" class="btn btn-primary">입금</button>
</form>
</div>
</div>
</div>
</div>
</div>
<%@ include file="/WEB-INF/view/layout/footer.jsp"%>
package com.tencoding.bank.controller;
import java.util.List;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.tencoding.bank.dto.DepositFormDto;
import com.tencoding.bank.dto.SaveFormDto;
import com.tencoding.bank.dto.WithDrawFormDto;
import com.tencoding.bank.handler.exception.CustomRestfulException;
import com.tencoding.bank.handler.exception.UnAuthorizedException;
import com.tencoding.bank.repository.model.Account;
import com.tencoding.bank.repository.model.User;
import com.tencoding.bank.service.AccountService;
import com.tencoding.bank.util.Define;
@Controller
@RequestMapping("/account")
public class AccountController {
@Autowired
private HttpSession session;
@Autowired
private AccountService accountService;
@GetMapping("/list")
public String list(Model model) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
List<Account> accountList = accountService.readAccountList(user.getId());
if(accountList.isEmpty()) {
model.addAttribute("accountList", null);
} else {
model.addAttribute("accountList", accountList);
}
return "account/list";
}
@GetMapping("/save")
public String save() {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
return "account/save";
}
@PostMapping("/save")
public String saveProc(SaveFormDto saveFormDto) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
if(saveFormDto.getNumber() == null
|| saveFormDto.getNumber().isEmpty()) {
throw new CustomRestfulException("계좌번호를 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(saveFormDto.getPassword() == null
|| saveFormDto.getPassword().isEmpty()) {
throw new CustomRestfulException("비밀번호를 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(saveFormDto.getBalance() == null
|| saveFormDto.getBalance() < 0) {
throw new CustomRestfulException("잘못된 입력입니다.", HttpStatus.BAD_REQUEST);
}
accountService.creatAccount(saveFormDto, user.getId());
return "redirect:/account/list";
}
@GetMapping("/withdraw")
public String withdraw() {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
return "account/withdraw";
}
@PostMapping("/withdraw")
public String withdrawProc(WithDrawFormDto withDrawFormDto) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
if(withDrawFormDto.getAmount() == null) {
throw new CustomRestfulException("금액을 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(withDrawFormDto.getAmount() <= 0) {
throw new CustomRestfulException("잘못된 금액입니다.", HttpStatus.BAD_REQUEST);
}
if(withDrawFormDto.getWAccountNumber() == null
|| withDrawFormDto.getWAccountNumber().isEmpty()) {
throw new CustomRestfulException("출금 계좌번호를 확인해주세요.", HttpStatus.BAD_REQUEST);
}
if(withDrawFormDto.getWAccountPassword() == null
|| withDrawFormDto.getWAccountPassword().isEmpty()) {
throw new CustomRestfulException("출금 계좌 비밀번호를 확인해주세요.", HttpStatus.BAD_REQUEST);
}
accountService.updateAccountWithdraw(withDrawFormDto, user.getId());
return "redirect:/account/list";
}
@GetMapping("/deposit")
public String deposit() {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
return "account/deposit";
}
@PostMapping("/deposit")
public String depositProc(DepositFormDto depositFormDto) {
User user = (User)session.getAttribute(Define.PRINCIPAL);;
if(user == null) {
throw new UnAuthorizedException("로그인을 먼저 해주세요.", HttpStatus.UNAUTHORIZED);
}
if(depositFormDto.getAmount() == null) {
throw new CustomRestfulException("금액을 입력해주세요.", HttpStatus.BAD_REQUEST);
}
if(depositFormDto.getAmount() <= 0) {
throw new CustomRestfulException("잘못된 금액입니다.", HttpStatus.BAD_REQUEST);
}
if(depositFormDto.getDAccountNumber() == null
|| depositFormDto.getDAccountNumber().isEmpty()) {
throw new CustomRestfulException("입금 계좌번호를 입력해주세요.", HttpStatus.BAD_REQUEST);
}
accountService.updateAccountDeposit(depositFormDto);
return "redirect:/account/list";
}
@GetMapping("/transfer")
public String transfer() {
return "account/transfer";
}
@GetMapping("/detail")
public String detail() {
return "account/detail";
}
}
package com.tencoding.bank.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tencoding.bank.dto.DepositFormDto;
import com.tencoding.bank.dto.SaveFormDto;
import com.tencoding.bank.dto.WithDrawFormDto;
import com.tencoding.bank.handler.exception.CustomRestfulException;
import com.tencoding.bank.repository.interfaces.AccountRepository;
import com.tencoding.bank.repository.interfaces.HistoryRepository;
import com.tencoding.bank.repository.model.Account;
import com.tencoding.bank.repository.model.History;
@Service
public class AccountService {
@Autowired
private AccountRepository accountRepository;
@Autowired
private HistoryRepository historyRepository;
@Transactional
public void creatAccount(SaveFormDto saveFormDto, Integer principalId) {
Account account = new Account();
account.setNumber(saveFormDto.getNumber());
account.setPassword(saveFormDto.getPassword());
account.setBalance(saveFormDto.getBalance());
account.setUserId(principalId);
int resultRowCount = accountRepository.insert(account);
if(resultRowCount != 1) {
throw new CustomRestfulException("계좌 생성 실패", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
public List<Account> readAccountList(Integer userId) {
List<Account> list = accountRepository.findByUserId(userId);
return list;
}
@Transactional
public void updateAccountWithdraw(WithDrawFormDto withDrawFormDto, Integer id) {
Account accountEntity = accountRepository.findByNumber(withDrawFormDto.getWAccountNumber());
if(accountEntity == null) {
throw new CustomRestfulException("해당 계좌가 없습니다.", HttpStatus.BAD_REQUEST);
}
if(accountEntity.getUserId() != id) {
throw new CustomRestfulException("본인 소유 계좌가 아닙니다.", HttpStatus.BAD_REQUEST);
}
if(accountEntity.getPassword().equals(withDrawFormDto.getWAccountPassword()) == false) {
throw new CustomRestfulException("출금 계좌 비밀번호가 일치하지 않습니다.", HttpStatus.BAD_REQUEST);
}
if(accountEntity.getBalance() < withDrawFormDto.getAmount()) {
throw new CustomRestfulException("계좌 잔액이 부족합니다.", HttpStatus.BAD_REQUEST);
}
accountEntity.withdraw(withDrawFormDto.getAmount());
accountRepository.updateById(accountEntity);
History history = new History();
history.setAmount(withDrawFormDto.getAmount());
history.setWBalance(accountEntity.getBalance());
history.setDBalance(null);
history.setWAccountId(accountEntity.getId());
history.setDAccountId(null);
int resultRowCount = historyRepository.insert(history);
if(resultRowCount != 1) {
throw new CustomRestfulException("정상적으로 처리되지 않았습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
public void updateAccountDeposit(DepositFormDto depositFormDto) {
Account accountEntity = accountRepository.findByNumber(depositFormDto.getDAccountNumber());
if(accountEntity == null) {
throw new CustomRestfulException("해당 계좌가 없습니다.", HttpStatus.BAD_REQUEST);
}
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 CustomRestfulException("정상적으로 처리되지 않았습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
<?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.tencoding.bank.repository.interfaces.AccountRepository">
<select id="findByUserId" resultType="com.tencoding.bank.repository.model.Account">
select * from account_tb where user_id = #{userId}
</select>
<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} where id = #{id}
</update>
<delete id="deleteById">
delete from account_tb where id = #{id}
</delete>
<select id="findById" resultType="com.tencoding.bank.repository.model.Account">
select * from account_tb where id = #{id}
</select>
<select id="findAll" resultType="com.tencoding.bank.repository.model.Account">
select * from account_tb
</select>
<select id="findByNumber" resultType="com.tencoding.bank.repository.model.Account">
select * from account_tb where number = #{number}
</select>
</mapper>