๐ก ํ์ต ๋ชฉํ
- CSS์ ๋ถํธ์คํธ๋ฉ์ ํ์ฉํ ์ค์ ์ ๋ ฌ
ํ๋ ์ค๋ฐ์ค(d-flex)์ ๋ถํธ์คํธ๋ฉ์ ์ ํธ๋ฆฌํฐ ํด๋์ค๋ฅผ ์ฌ์ฉํด ์์๋ฅผ ์ค์์ ์ ๋ ฌํ๋ ๋ฐฉ๋ฒ. - JSP์์ ๋์ ์ฝํ
์ธ ์ฒ๋ฆฌ
JSP์์ ๋์ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๊ณ ํ๋ฉด์ ํ์ํ๋ ๋ฐฉ๋ฒ - ํ์ด์ง(Pagination) ๊ตฌํ

์ฌ์ ๊ธฐ๋ฐ ์ง์
- ๋ถํธ์คํธ๋ฉ์ ๊ทธ๋ฆฌ๋ ์์คํ
:
- ๊ฐ๋
๋ถํธ์คํธ๋ฉ์ ํ๋ฉด์ 12๊ฐ์ ์ปฌ๋ผ์ผ๋ก ๋๋์ด ๋ ์ด์์์ ๊ตฌ์ฑํ ์ ์๋๋ก ๋๋ ๊ทธ๋ฆฌ๋ ์์คํ ์ ์ ๊ณตํฉ๋๋ค. col-sm-8์ ์์ ํ๋ฉด์์ 8๊ฐ์ ์ปฌ๋ผ์ ์ฐจ์งํ๋ ๋ ์ด์์์ ์๋ฏธํฉ๋๋ค. - ์ฌ์ฉ ๋ฐฉ๋ฒ
col-sm-8, col-md-6 ๋ฑ์ ํด๋์ค๋ฅผ ์ฌ์ฉํด ๋ฐ์ํ ๋ ์ด์์์ ์ฝ๊ฒ ๊ตฌ์ฑํ ์ ์์ต๋๋ค. - ์์
col-sm-8์ 12๊ฐ์ ๊ทธ๋ฆฌ๋ ์ค 8๊ฐ๋ฅผ ์ฐจ์งํ๋ฉฐ, ์ด๋ ์ ์ฒด ํ๋ฉด์ ์ฝ 66.67%์ ๋๋ค.
- ๊ฐ๋
- ํ๋ ์ค๋ฐ์ค(Flexbox)์ ์ค์ ์ ๋ ฌ:
- ๊ฐ๋
ํ๋ ์ค๋ฐ์ค๋ CSS์ ๋ ์ด์์ ๋ชจ๋ธ๋ก, ์์๋ฅผ ์ฝ๊ฒ ์ ๋ ฌํ๊ณ ๋ฐฐ์นํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค.
๋ถํธ์คํธ๋ฉ์ d-flex์ justify-content-center๋ ํ๋ ์ค๋ฐ์ค๋ฅผ ํ์ฉํด ์์ ์์๋ฅผ ์ํ ์ค์์ ์ ๋ ฌํ๋ ๋ฐ
์ฌ์ฉ๋ฉ๋๋ค. - ์ฌ์ฉ ๋ฐฉ๋ฒ
d-flex๋ฅผ ๋ถ๋ชจ ์์์ ์ ์ฉํ๊ณ , justify-content-center๋ฅผ ์ถ๊ฐํ์ฌ ์์ ์์๋ฅผ ์ค์์ ๋ฐฐ์นํฉ๋๋ค.
- ๊ฐ๋
- ํ์ด์ง(Pagination) ๊ตฌํ:
- ๊ฐ๋
ํ์ด์ง์ ๋ง์ ์์ ๋ฐ์ดํฐ๋ฅผ ์ฌ๋ฌ ํ์ด์ง๋ก ๋๋์ด ๋ณด์ฌ์ฃผ๋ ๊ธฐ๋ฒ์ ๋๋ค.
์ฌ์ฉ์๊ฐ ํ ํ์ด์ง์ ํ์ํ ๋ฐ์ดํฐ์ ์๋ฅผ ์ง์ ํ๊ณ , ๋๋จธ์ง ๋ฐ์ดํฐ๋ ๋ค์ ํ์ด์ง๋ก ๋๊น๋๋ค. - ์ฌ์ฉ ๋ฐฉ๋ฒ
ํ์ฌ ํ์ด์ง ๋ฒํธ(currentPage)์ ์ ์ฒด ํ์ด์ง ์(totalPages)๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์ด์ง ๋งํฌ๋ฅผ ์์ฑํฉ๋๋ค.
๋ถํธ์คํธ๋ฉ์ pagination ํด๋์ค๋ฅผ ์ฌ์ฉํด ์๊ฐ์ ์ผ๋ก ๊ตฌ์ฑํฉ๋๋ค.
- ๊ฐ๋
detail.jsp
๋๋ณด๊ธฐ
detail.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>
<div class="bg-light p-md-5">
<div class="user--box">
${principal.username}๋ ๊ณ์ข<br> ๊ณ์ข๋ฒํธ : ${account.number}<br> ์์ก : ${account.formatKoreanWon(account.balance)}
</div>
<br>
<div>
<a href="/account/detail/${account.id}?type=all" class="btn btn-outline-primary" >์ ์ฒด</a>
<a href="/account/detail/${account.id}?type=deposit" class="btn btn-outline-primary" >์
๊ธ</a>
<a href="/account/detail/${account.id}?type=withdrawal" class="btn btn-outline-primary" >์ถ๊ธ</a>
</div>
<br>
<table class="table table-striped">
<thead>
<tr>
<th>๋ ์ง</th>
<th>๋ณด๋ธ์ด</th>
<th>๋ฐ์์ด</th>
<th>์
์ถ๊ธ ๊ธ์ก</th>
<th>๊ณ์ข์์ก</th>
</tr>
</thead>
<tbody>
<c:forEach var="historyAccount" items="${historyList}">
<tr>
<th>${historyAccount.timestampToString(historyAccount.createdAt)}</th>
<th>${historyAccount.sender}</th>
<th>${historyAccount.receiver}</th>
<th>${historyAccount.formatKoreanWon(historyAccount.amount)}</th>
<th>${historyAccount.formatKoreanWon(historyAccount.balance)}</th>
</tr>
</c:forEach>
</tbody>
</table>
<br>
<!-- Pagination -->
<div class="d-flex justify-content-center" >
<ul class="pagination">
<!-- Previous Page Link -->
<li class="page-item <c:if test='${currentPage == 1}'>disabled</c:if>">
<a class="page-link" href="?type=${type}&page=${currentPage - 1}&size=${size}" >Previous</a>
</li>
<!-- Page Numbers -->
<!-- [Previous] 1 2 3 4 5 6 7 8 [Next] -->
<c:forEach begin="1" end="${totalPages}" var="page" >
<li class="page-item <c:if test='${page == currentPage}'>active </c:if>">
<a class="page-link" href="?type=${type}&page=${page}&size=${size}" >${page}</a>
</li>
</c:forEach>
<!-- Next Page Link -->
<li class="page-item <c:if test='${currentPage == totalPages}'>disabled</c:if>" >
<a class="page-link" href="?type=${type}&page=${currentPage + 1}&size=${size}" >Next</a>
</li>
</ul>
</div>
</div>
</div>
<!-- end of col-sm-8 -->
</div>
</div>
<!-- end of content.jsp(xxx.jsp) -->
<!-- footer.jsp -->
<%@ include file="/WEB-INF/view/layout/footer.jsp"%>
AccountController - ์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
๋๋ณด๊ธฐ
AccountController - ์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
/**
* ๊ณ์ข ์์ธ ๋ณด๊ธฐ ํ์ด์ง
* ๋์ ์ผ๋ก ์๋ํ๋ค.
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=all
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=deposit
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=withdraw
* @return
*/
@GetMapping("/detail/{accountId}")
public String detail(@PathVariable(name = "accountId") Integer accountId,
@RequestParam (required = false, name = "type") String type,
@RequestParam (name = "page", defaultValue = "1") int page,
@RequestParam (name = "size", defaultValue = "2") int size,
Model model) {
// localhost:8080/account/detail/${4}
System.out.println("@PathVariable : " + accountId);
// localhost:8080/account/detail/${account.id}?type=all
System.out.println("@RequestParam : " + type);
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.PRINCIPAL); // ๋ค์ด ์บ์คํ
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
// ์ ํจ์ฑ ๊ฒ์ฌ
// array ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ ํ๋ ๋ฉ์๋
List<String> validTypes = Arrays.asList("all", "deposit", "withdrawal");
if(!validTypes.contains(type)) {
throw new DataDeliveryException("์ ํจํ์ง ์๋ ์ ๊ทผ ์
๋๋ค.", HttpStatus.BAD_REQUEST);
}
// ํ์ด์ง ๊ฐ์๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์ ์ด ํ์ด์ง์ ์๋ฅผ ๊ณ์ฐํด ์ฃผ์ด์ผ ํ๋ค.
int totalRecords = accountService.countHistoryByAccountIdAndType(type, accountId);
int totalPages = (int)Math.ceil( (double)totalRecords / size ); // ์ฌ๋ฆผ์ฐ์ฐ ์์์ ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด ์ฌ๋ฆผ
Account account = accountService.readAccountById(accountId);
List<HistoryAccount> historyList = accountService.readHistoryByAccountId(type, accountId, page, size);
model.addAttribute("account", account);
model.addAttribute("historyList", historyList);
model.addAttribute("currentPage", page); // 1;
model.addAttribute("totalPages", totalPages);
model.addAttribute("type", type);
model.addAttribute("size", size); // 2
return "account/detail";
}
์ ์ฒด์ฝ๋
package com.tenco.bank.controller;
import java.util.Arrays;
import java.util.List;
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.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.tenco.bank.dto.DepositDTO;
import com.tenco.bank.dto.SaveDTO;
import com.tenco.bank.dto.TransferDTO;
import com.tenco.bank.dto.WithdrawalDTO;
import com.tenco.bank.handler.exception.DataDeliveryException;
import com.tenco.bank.handler.exception.UnAuthorizedException;
import com.tenco.bank.repository.model.Account;
import com.tenco.bank.repository.model.HistoryAccount;
import com.tenco.bank.repository.model.User;
import com.tenco.bank.service.AccountService;
import com.tenco.bank.utils.Define;
import jakarta.servlet.http.HttpSession;
@Controller // IOC ๋์ (์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌ)
@RequestMapping("/account")
public class AccountController {
// ๊ณ์ข ์์ฑ ํ๋ฉด ์์ฒญ - DI ์ฒ๋ฆฌ
private final HttpSession session;
private final AccountService accountService;
@Autowired
public AccountController(HttpSession session, AccountService accountService) {
this.session = session; // ์์กด ์ฃผ์
this.accountService = accountService; // ์์กด ์ฃผ์
}
/**
* ๊ณ์ข ์์ฑ ํ์ด์ง ์์ฒญ ์ฃผ์์ค๊ณ : http://localhost:8080/account/save :
*
* @return
*/
@GetMapping("/save")
public String savePage() {
// 1. ์ธ์ฆ ๊ฒ์ฌ๊ฐ ํ์ (account ์ ์ฒด๊ฐ ํ์ํจ)
User principal = (User) session.getAttribute(Define.PRINCIPAL);
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
return "account/save";
}
/**
* ๊ณ์ข ์์ฑ ๊ธฐ๋ฅ ์์ฒญ ์ฃผ์ ์ค๊ณ : http://localhost:8080/account/save
*
* @retrun : ์ถํ ๊ณ์ข ๋ชฉ๋ก ํ์ด์ง ์ด๋ ์ฒ๋ฆฌ
*/
@PostMapping("/save")
public String saveProc(SaveDTO dto) {
// 1. form ๋ฐ์ดํฐ ์ถ์ถ (ํ์ฑ ์ ๋ต)
// 2. ์ธ์ฆ ๊ฒ์ฌ
// 3. ์ ํจ์ฑ ๊ฒ์ฌ
// 4. ์๋น์ค ํธ์ถ
User principal = (User) session.getAttribute(Define.PRINCIPAL);
// ์ธ์ฆ ๊ฒ์ฌ
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
if (dto.getNumber() == null || dto.getNumber().isEmpty()) {
throw new DataDeliveryException(Define.ENTER_YOUR_ACCOUNT_NUMBER, HttpStatus.BAD_REQUEST);
}
if (dto.getPassword() == null || dto.getPassword().isEmpty()) {
throw new DataDeliveryException(Define.ENTER_YOUR_PASSWORD, HttpStatus.BAD_REQUEST);
}
if (dto.getBalance() == null || dto.getBalance() <= 0) {
throw new DataDeliveryException(Define.ENTER_YOUR_BALANCE, HttpStatus.BAD_REQUEST);
}
// ์๋น์ค์ dto์ ์ ์ ์์ด๋๋ฅผ ๋ณด๋ธ๋ค.
accountService.createAccount(dto, principal.getId());
return "redirect:/index";
}
/**
* ๊ณ์ข ๋ชฉ๋ก ํ๋ฉด ์์ฒญ ์ฃผ์ ์ค๊ณ : http://localhost:8080/account/list, ..../
*
* @return list.jsp
*/
// ํ์ด์ง ๋ฆฌํดํด์ผ ๋์ string์ผ๋ก ์ง๋๋ค.
@GetMapping({ "/list", "/" })
public String listPage(Model model) {
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.PRINCIPAL); // ์ ์ ์ธ์
๊ฐ์ ธ์ด
if (principal == null) {
// ๋ก๊ทธ์ธ์ ์ํ ์ํ
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
// 2. ์ ํจ์ฑ ๊ฒ์ฌ
// 3. ์๋น์ค ํธ์ถ (์๋น์ค์ปจํธ๋กค๋ฌ : ํต์ฌ๊ธฐ๋ฅ )
List<Account> accountList = accountService.readAccountListByUserId(principal.getId());
if (accountList.isEmpty()) {
model.addAttribute("accountList", null);
} else {
model.addAttribute("accountList", accountList); // ๋ชจ๋ธ์์ ํค,๊ฐ์ ๋์ ธ๋ฒ๋ฆฐ๋ค.
}
// JSP ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ด ์ฃผ๋ ๋ฐฉ๋ฒ
return "account/list";
}
/**
* ์ถ๊ธ ํ์ด์ง ์์ฒญ
*
* @return withdrawal.jsp
*/
@GetMapping("/withdrawal")
public String withdrawalPage() {
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.PRINCIPAL);
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
return "account/withdrawal";
}
@PostMapping("/withdrawal")
public String withdrawalProc(WithdrawalDTO dto) {
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.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) { // 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 || dto.getWAccountPassword().isEmpty()) {
throw new DataDeliveryException(Define.ENTER_YOUR_PASSWORD, HttpStatus.BAD_REQUEST);
}
accountService.updateAccountWithdraw(dto, principal.getId());
return "redirect:/account/list";
}
// ์
๊ธ ํ์ด์ง ์์ฒญ
@GetMapping("/deposit")
public String depositPage() {
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.PRINCIPAL);
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
return "account/deposit";
}
/**
* ์
๊ธ ํ์ด์ง ์์ฒญ
*
* @param dto
* @return
*/
@PostMapping("/deposit")
public String depositProc(DepositDTO dto) {
User principal = (User) session.getAttribute(Define.PRINCIPAL);
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
if (dto.getAmount() == null) {
throw new DataDeliveryException(Define.ENTER_YOUR_BALANCE, HttpStatus.BAD_REQUEST);
}
if (dto.getAmount().longValue() <= 0) {
throw new DataDeliveryException(Define.D_BALANCE_VALUE, HttpStatus.BAD_REQUEST);
}
if (dto.getDAccountNumber() == null || dto.getDAccountNumber().trim().isEmpty()) {
throw new DataDeliveryException(Define.ENTER_YOUR_ACCOUNT_NUMBER, HttpStatus.BAD_REQUEST);
}
accountService.updateAccountDeposit(dto, principal.getId());
return "redirect:/account/list";
}
// ์ด์ฒด ํ์ด์ง ์์ฒญ
/**
* ๊ณ์ข ์ด์ฒด ํ๋ฉด ์์ฒญ
* @return
*/
@GetMapping("/transfer")
public String transfer() {
User principal = (User) session.getAttribute(Define.PRINCIPAL);
if (principal == null) {
throw new DataDeliveryException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
return "account/transfer";
}
// ์ด์ฒด ๊ธฐ๋ฅ ์ฒ๋ฆฌ ์์ฒญ
@PostMapping("/transfer")
public String transferProc(TransferDTO dto) {
// 1. ์ธ์ฆ ๊ฒ์ฌ
User principal =(User)session.getAttribute(Define.PRINCIPAL);
// 2. ์ ํจ์ฑ ๊ฒ์ฌ
if(dto.getAmount() == null) { // ์ถ๊ธํ๋ ๊ธ์ก์ด ๊ณต๋ฐฑ์ด๋ฉด ์๋๋ค.
throw new DataDeliveryException(Define.ENTER_YOUR_BALANCE, HttpStatus.BAD_REQUEST);
}
if(dto.getAmount().longValue() <= 0) { // ์ถ๊ธํ๋ ๊ธ์ก์ด 0 ์ดํ์ด๋ฉด ์๋๋ค.
throw new DataDeliveryException(Define.D_BALANCE_VALUE, HttpStatus.BAD_REQUEST);
}
if(dto.getWAccountNumber() == null || dto.getWAccountNumber().trim().isEmpty()) {
// ๊ณ์ข๋ฒํธ๊ฐ null ์ด๊ฑฐ๋ ๊ณต๋ฐฑ์ด๋ฉด ์๋๋ค.
throw new DataDeliveryException("์ถ๊ธํ์ค ๊ณ์ข๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.", HttpStatus.BAD_REQUEST);
}
if(dto.getDAccountNumber() == null || dto.getDAccountNumber().trim().isEmpty()) {
// ์
๊ธ ๊ธ์ก์ด null ์ด๊ฑฐ๋ ๊ณต๋ฐฑ์ด๋ฉด ์๋๋ค.
throw new DataDeliveryException("์ด์ฒดํ์ค ๊ณ์ข๋ฒํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์", HttpStatus.BAD_REQUEST);
}
if(dto.getPassword() == null || dto.getPassword().trim().isEmpty()) {
throw new DataDeliveryException(Define.ENTER_YOUR_PASSWORD, HttpStatus.BAD_REQUEST);
}
// ์๋น์ค ํธ์ถ
accountService.updateAccountTransfer(dto, principal.getId());
return "redirect:/account/list";
}
/**
* ๊ณ์ข ์์ธ ๋ณด๊ธฐ ํ์ด์ง
* ๋์ ์ผ๋ก ์๋ํ๋ค.
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=all
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=deposit
* ์ฃผ์ ์ค๊ณ : localhost:8080/account/detail/${account.id}?type=withdraw
* @return
*/
@GetMapping("/detail/{accountId}")
public String detail(@PathVariable(name = "accountId") Integer accountId,
@RequestParam (required = false, name = "type") String type,
@RequestParam (name = "page", defaultValue = "1") int page,
@RequestParam (name = "size", defaultValue = "2") int size,
Model model) {
// localhost:8080/account/detail/${4}
System.out.println("@PathVariable : " + accountId);
// localhost:8080/account/detail/${account.id}?type=all
System.out.println("@RequestParam : " + type);
// 1. ์ธ์ฆ๊ฒ์ฌ
User principal = (User) session.getAttribute(Define.PRINCIPAL); // ๋ค์ด ์บ์คํ
if (principal == null) {
throw new UnAuthorizedException(Define.NOT_AN_AUTHENTICATED_USER, HttpStatus.UNAUTHORIZED);
}
// ์ ํจ์ฑ ๊ฒ์ฌ
// array ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํ ํ๋ ๋ฉ์๋
List<String> validTypes = Arrays.asList("all", "deposit", "withdrawal");
if(!validTypes.contains(type)) {
throw new DataDeliveryException("์ ํจํ์ง ์๋ ์ ๊ทผ ์
๋๋ค.", HttpStatus.BAD_REQUEST);
}
// ํ์ด์ง ๊ฐ์๋ฅผ ๊ณ์ฐํ๊ธฐ ์ํด์ ์ด ํ์ด์ง์ ์๋ฅผ ๊ณ์ฐํด ์ฃผ์ด์ผ ํ๋ค.
int totalRecords = accountService.countHistoryByAccountIdAndType(type, accountId);
int totalPages = (int)Math.ceil( (double)totalRecords / size ); // ์ฌ๋ฆผ์ฐ์ฐ ์์์ ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด ์ฌ๋ฆผ
Account account = accountService.readAccountById(accountId);
List<HistoryAccount> historyList = accountService.readHistoryByAccountId(type, accountId, page, size);
model.addAttribute("account", account);
model.addAttribute("historyList", historyList);
model.addAttribute("currentPage", page); // 1;
model.addAttribute("totalPages", totalPages);
model.addAttribute("type", type);
model.addAttribute("size", size); // 2
return "account/detail";
}
}
AccountService - ์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
๋๋ณด๊ธฐ
์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
/**
* ๋จ์ผ ๊ณ์ข ๊ฑฐ๋ ๋ด์ญ ์กฐํ
* @param type = [all, deposit, withdrawal]
* @param accountId (pk)
* @return ์ ์ฒด, ์
๊ธ, ์ถ๊ธ ๊ฑฐ๋๋ด์ญ(3๊ฐ์ง ํ์
) ๋ฐํ
*/
// @Transactional
public List<HistoryAccount> readHistoryByAccountId(String type, Integer accountId, int page, int size) {
List<HistoryAccount> list = new ArrayList<>();
int limit = size;
int offset = (page - 1) * size;
list = historyRepository.findByAccountIdAndTypeOfHistory(type, accountId, limit, offset);
return list;
}
// ํด๋น ๊ณ์ข์ ๊ฑฐ๋ ์ ํ์ ๋ฐ๋ฅธ ์ ์ฒด ๋ ์ฝ๋ ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
public int countHistoryByAccountIdAndType(String type, Integer accountId) {
return historyRepository.countByAccountIdAndType(type, accountId);
}
์ ์ฒด์ฝ๋
package com.tenco.bank.service;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import org.h2.value.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tenco.bank.dto.DepositDTO;
import com.tenco.bank.dto.SaveDTO;
import com.tenco.bank.dto.TransferDTO;
import com.tenco.bank.dto.WithdrawalDTO;
import com.tenco.bank.handler.exception.DataDeliveryException;
import com.tenco.bank.handler.exception.RedirectException;
import com.tenco.bank.repository.interfaces.AccountRepository;
import com.tenco.bank.repository.interfaces.HistoryRepository;
import com.tenco.bank.repository.model.Account;
import com.tenco.bank.repository.model.History;
import com.tenco.bank.repository.model.HistoryAccount;
import com.tenco.bank.repository.model.User;
import com.tenco.bank.utils.Define;
@Service // IoC ๋์( ์ฑ๊ธํค์ผ๋ก ๊ด๋ฆฌ)
public class AccountService {
private final AccountRepository accountRepository;
private final HistoryRepository historyRepository;
@Autowired // ์๋ต ๊ฐ๋ฅ - DI ์ฒ๋ฆฌ
public AccountService(AccountRepository accountRepository, HistoryRepository historyRepository) {
this.accountRepository = accountRepository;
this.historyRepository = historyRepository;
}
/**
* ๊ณ์ข ์์ฑ ๊ธฐ๋ฅ
* @param dto
* @param integer
*/
// ํธ๋ ์ญ์
์ฒ๋ฆฌ๋ฅผ ํด์ผํ๋ค. (ํ๋ฒ์ ๋ฐ์๋๊ฑฐ๋, ์์ ๋ฐ์์๋๊ฑฐ๋)
@Transactional
public void createAccount(SaveDTO dto, Integer principalId) {
int result = 0;
try {
result = accountRepository.insert(dto.toAccount(principalId));
} catch (DataAccessException e) {
throw new DataDeliveryException("์๋ชป๋ ์์ฒญ์
๋๋ค.", HttpStatus.INTERNAL_SERVER_ERROR);
} catch(Exception e) {
throw new RedirectException("์ ์ ์๋ ์ค๋ฅ ์
๋๋ค.", HttpStatus.SERVICE_UNAVAILABLE);
}
if(result == 0) {
throw new DataDeliveryException("์ ์ ์ฒ๋ฆฌ ๋์ง ์์์ต๋๋ค.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* ๊ณ์ข ๋ช๊ฐ ์๋์ง ํ์ธ
* @param principal
*/
@Transactional
public List<Account> readAccountListByUserId(Integer userId) {
// TODO Auto-generated method stub
List<Account> accountListEntity = null;
try {
accountListEntity = accountRepository.findByUserId(userId);
} catch (DataDeliveryException e) {
// TODO: handle exception
throw new DataDeliveryException("์๋ชป๋ ์ฒ๋ฆฌ ์
๋๋ค.", HttpStatus.INTERNAL_SERVER_ERROR);
} catch (Exception e) {
throw new RedirectException("์ ์ ์๋ ์ค๋ฅ", HttpStatus.SERVICE_UNAVAILABLE);
}
return accountListEntity;
}
// ํ๋ฒ์ ๋ชจ๋ ๊ธฐ๋ฅ์ ์๊ฐํ๋ ๊ฒ์ ํ๋ฌ
// 1. ์ฌ์ฉ์๊ฐ ๋์ง ๊ณ์ข๋ฒํธ๊ฐ ์กด์ฌํ๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํด์ผ ํ๋ค. --> select
// 2. ๋ณธ์ธ ๊ณ์ข ์ฌ๋ถ๋ฅผ ํ์ธํด์ผ ํ๋ค. --> ๊ฐ์ฒด ์ํ๊ฐ์์ ๋น๊ตํ๋ค.
// 3. ๊ณ์ข ๋น๋ฒ ํ์ธ --> ๊ฐ์ฒด ์ํ๊ฐ์์ ์ผ์น ์ฌ๋ถ ํ์ธ account์์ userid๊ฐ ์๊ณ principal์์๋ ํ์ธ๊ฐ๋ฅ
// 4. ์์ก ์ฌ๋ถ ํ์ธ --> ๊ฐ์ฒด ์ํ๊ฐ์์ ํ์ธ
// 5. ์ถ๊ธ ์ฒ๋ฆฌ --> update ์ฟผ๋ฆฌ ๋ฐ์
// 6. history ํ
์ด๋ธ์ ๊ฑฐ๋๋ด์ญ ๋ฑ๋ก --> insert(history)
// 7. ํธ๋์ญ์
์ฒ๋ฆฌ ex) insert ํ๋ค๊ฐ ์ค๋ฅ๋๋ฉด ๋ค๋ก update ๊ฐ์ผ ๋๊ธฐ ๋๋ฌธ์ ํธ๋์ญ์
์ฌ์ฉ
@Transactional
public void updateAccountWithdraw(WithdrawalDTO dto, Integer principalId) {
// 1. ์ฌ์ฉ์๊ฐ ๋์ง ๊ณ์ข๋ฒํธ๊ฐ ์กด์ฌํ๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํด์ผ ํ๋ค.
// (ํผ์์คํด์ค ๊ณ์ธต์์ ๊ธ์ด์ entity ๋ถ์)
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. ์ถ๊ธ ์ฒ๋ฆฌ -- accountRepository ๊ฐ์ฒด์ ์์ก์ ๋ณ๊ฒฝํ๊ณ ์
๋ฐ์ดํธ ์ฒ๋ฆฌํด์ผ ํ๋ค.
accountEntity.withdraw(dto.getAmount());
// update ์ฒ๋ฆฌ
accountRepository.updateById(accountEntity);
// 6 - ๊ฑฐ๋ ๋ด์ญ ๋ฑ๋ก
/**
* <insert id="insert">
insert into history_tb(amount, w_Balance, d_Balance, w_Account_id, d_account_id )
values(#{amount}, #{wBalance}, #{dBalance}, #{wAccountId}, #{dAccountId} )
</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);
}
}
// 1. ๊ณ์ข ์กด์ฌ ์ฌ๋ถ๋ฅผ ํ์ธ
// 2. ๋ณธ์ธ ๊ณ์ข ์ฌ๋ถ๋ฅผ ํ์ธ -- ๊ฐ์ฒด ์ํ๊ฐ์์ ๋น๊ต
// 3. ์
๊ธ ์ฒ๋ฆฌ -- update
// 4. ๊ฑฐ๋ ๋ด์ญ ๋ฑ๋ก -- insert(history)
@Transactional
public void updateAccountDeposit(DepositDTO dto, Integer principalId) {
// 1.
Account accountEntity = accountRepository.findByNumber(dto.getDAccountNumber());
if (accountEntity == null) {
throw new DataDeliveryException(Define.NOT_EXIST_ACCOUNT, HttpStatus.BAD_REQUEST);
}
// 2.
accountEntity.checkOwner(principalId);
// 3.
accountEntity.deposit(dto.getAmount());
accountRepository.updateById(accountEntity);
// 4.
History history = History.builder()
.amount(dto.getAmount())
.dAccountId(accountEntity.getId())
.dBalance(accountEntity.getBalance())
.wAccountId(null)
.wBalance(null)
.build();
int rowResultCount = historyRepository.insert(history);
if (rowResultCount != 1) {
throw new DataDeliveryException(Define.FAILED_PROCESSING, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
// ์ด์ฒด ๊ธฐ๋ฅ ๋ง๋ค๊ธฐ
// 1. ์ถ๊ธ ๊ณ์ข ์กด์ฌ์ฌ๋ถ ํ์ธ -- select (๊ฐ์ฒด ๋ฆฌํด ๋ฐ์ ์ํ)
// 2. ์
๊ธ ๊ณ์ข ์กด์ฌ์ฌ๋ถ ํ์ธ -- select (๊ฐ์ฒด ๋ฆฌํด ๋ฐ์ ์ํ)
// 3. ์ถ๊ธ ๊ณ์ข ๋ณธ์ธ ์์ ํ์ธ -- ๊ฐ์ฒด ์ํ๊ฐ๊ณผ ์ธ์
์์ด๋(ID) ๋น๊ต
// 4. ์ถ๊ธ ๊ณ์ข ๋น๋ฐ ๋ฒํธ ํ์ธ -- ๊ฐ์ฒด ์ํ๊ฐ๊ณผ dto ๋น๋ฐ๋ฒํธ ๋น๊ต
// 5. ์ถ๊ธ ๊ณ์ข ์์ก ์ฌ๋ถ ํ์ธ -- ๊ฐ์ฒด ์ํ๊ฐ ํ์ธ, dto์ ๋น๊ต
// 6. ์
๊ธ ๊ณ์ข ๊ฐ์ฒด ์ํ๊ฐ ๋ณ๊ฒฝ ์ฒ๋ฆฌ (๊ฑฐ๋๊ธ์ก ์ฆ๊ฐ์ฒ๋ฆฌ)
// 7. ์
๊ธ ๊ณ์ข -- update ์ฒ๋ฆฌ
// 8. ์ถ๊ธ ๊ณ์ข ๊ฐ์ฒด ์ํ๊ฐ ๋ณ๊ฒฝ ์ฒ๋ฆฌ (์์ก - ๊ฑฐ๋๊ธ์ก)
// 9. ์ถ๊ธ ๊ณ์ข -- update ์ฒ๋ฆฌ
// 10. ๊ฑฐ๋ ๋ด์ญ ๋ฑ๋ก ์ฒ๋ฆฌ
// 11. ํธ๋์ญ์
์ฒ๋ฆฌ
public void updateAccountTransfer(TransferDTO dto, Integer principalId) {
// ์ถ๊ธ ๊ณ์ข ์ ๋ณด ์กฐํ
Account withdrawAccountEntity = accountRepository.findByNumber(dto.getWAccountNumber());
System.out.println("withdrawAccountEntity : " + withdrawAccountEntity);
// ์
๊ธ ๊ณ์ข ์ ๋ณด ์กฐํ
Account depositAccountEntity = accountRepository.findByNumber(dto.getDAccountNumber());
System.out.println("depositAccountEntity : " + depositAccountEntity);
if(withdrawAccountEntity == null) {
throw new DataDeliveryException(Define.NOT_EXIST_ACCOUNT, HttpStatus.INTERNAL_SERVER_ERROR);
}
if(depositAccountEntity == null) {
throw new DataDeliveryException("์๋๋ฐฉ์ ๊ณ์ข ๋ฒํธ๊ฐ ์์ต๋๋ค.", HttpStatus.INTERNAL_SERVER_ERROR);
}
withdrawAccountEntity.checkOwner(principalId); // ์ถ๊ธํ๋ ๋ด ๊ณ์ข๊ฐ ๋ด๊ป์ง ํ์ธ
withdrawAccountEntity.checkPassword(dto.getPassword()); // ์ถ๊ธํ๋ ๋ด ๊ณ์ข์ ๋น๋ฐ๋ฒํธ๊ฐ ๋ง๋์ง Transfer DTO์ password ๋ ๋น๊ต
withdrawAccountEntity.checkBalance(dto.getAmount());
withdrawAccountEntity.withdraw(dto.getAmount());
depositAccountEntity.deposit(dto.getAmount());
int resultRowCountWithdraw = accountRepository.updateById(withdrawAccountEntity); // ์๋ก ๊ฐฑ์
int resultRowCountDeposit = accountRepository.updateById(depositAccountEntity); // ์๋ก ๊ฐฑ์
if(resultRowCountWithdraw != 1 && resultRowCountDeposit != 1) {
throw new DataDeliveryException(Define.FAILED_PROCESSING, HttpStatus.INTERNAL_SERVER_ERROR);
}
// TransferDTO ์ History ๊ฐ์ฒด๋ฅผ ๋ฐํํ๋ ๋ฉ์๋ค ๋ง๋ค์ด ์ค ์ ์์ต๋๋ค.
History history = History.builder()
.amount(dto.getAmount()) // ์ด์ฒด ๊ธ์ก
.wAccountId(withdrawAccountEntity.getId()) // ์ถ๊ธ ๊ณ์ข
.dAccountId(depositAccountEntity.getId()) // ์
๊ธ ๊ณ์ข
.wBalance(withdrawAccountEntity.getBalance()) // ์ถ๊ธ ๊ณ์ข ๋จ์ ์์ก
.dBalance(depositAccountEntity.getBalance()) // ์
๊ธ ๊ณ์ข ๋จ์ ์์ก
.build();
int resultRowCountHistory = historyRepository.insert(history);
if(resultRowCountHistory != 1) {
throw new DataDeliveryException(Define.FAILED_PROCESSING, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* ๋จ์ผ ๊ณ์ข ์กฐํ ๊ธฐ๋ฅ (accountId ๊ธฐ์ค)
* @param accountId
* @return
*/
public Account readAccountById(Integer accountId) {
Account accountEntity = accountRepository.findByAccountId(accountId);
if(accountEntity == null) {
throw new DataDeliveryException(Define.NOT_EXIST_ACCOUNT, HttpStatus.INTERNAL_SERVER_ERROR);
}
return accountEntity;
}
/**
* ๋จ์ผ ๊ณ์ข๊ฑฐ๋ ๋ด์ญ ์กฐํ
* @param type : [all, deposit, withdrawal]
* @param accountId (pk)
* @return ์ ์ฒด, ์
๊ธ, ์ถ๊ธ ๊ฑฐ๋๋ด์ญ(3๊ฐ์ง ํ์
) ๋ฐํ
*/
// @Transactional
public List<HistoryAccount> readHistoryByAccountId(String type, Integer accountId, int page, int size) {
List<HistoryAccount> list = new ArrayList<HistoryAccount>();
int limit = size;
int offset = (page - 1) * size;
list = historyRepository.findByAccountIdAndTypeOfHistory(type, accountId, limit, offset);
return list;
}
// ํด๋น ๊ณ์ข์ ๊ฑฐ๋ ์ ํ์ ๋ฐ๋ฅธ ์ ์ฒด ๋ ์ฝ๋ ์๋ฅผ ๋ฐํํ๋ ๋ฉ์๋
public int countHistoryByAccountIdAndType(String type, Integer accountId) {
return historyRepository.countByAccountIdAndType(type, accountId);
}
}
AccountRepository - ์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
๋๋ณด๊ธฐ
์ฝ๋ ์ถ๊ฐ ๋ฐ ์์
// ๊ณ ๋ฏผ! - ๊ณ์ข ์กฐํ
// --> ํ ์ฌ๋์ ์ ์ ๋ ์ฌ๋ฌ๊ฐ์ ๊ณ์ข๋ฒํธ๋ฅผ ๊ฐ์ง ์ ์๋ค. (๋ฆฌ์คํธ๋ก ๋ฝ์์ผ ํ๋ค.)
// @Param ์ฌ์ฉ์ด์ : interface ํ๋ผ๋ฏธํฐ๋ช
๊ณผ xml์ ์ฌ์ฉํ ๋ณ์๋ช
์ ๋ค๋ฅด๊ฒ ์ฌ์ฉํด์ผ ๋๋ค๋ฉด
// @param ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ 2๊ฐ ์ด์์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ๋ฐ๋์ ์ฌ์ฉ.
public List<Account> findByUserId(@Param("userId") Integer principalId);
// --> account id ๊ฐ์ผ๋ก ๊ณ์ข์ ๋ณด ์กฐํ. (ํ์ํ๋ค.)
public Account findByNumber(@Param("number") String id);
// ์ฝ๋ ์ถ๊ฐ ์์
public Account findByAccountId(Integer accountId);
public int countByAccountIdAndType(@Param("type")String type,
@Param("accountId")Integer accountId);
์ ์ฒด์ฝ๋
package com.tenco.bank.repository.interfaces;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.tenco.bank.repository.model.Account;
// AccountRepository ์ธํฐํ์ด์ค์ account.xml ํ์ผ์ ๋งค์นญ ์ํจ๋ค.
@Mapper
public interface AccountRepository {
public int insert(Account account);
public int updateById(Account account);
public int deleteBtId(Integer id, String name);
// ๊ณ ๋ฏผ! - ๊ณ์ข ์กฐํ
// --> ํ ์ฌ๋์ ์ ์ ๋ ์ฌ๋ฌ๊ฐ์ ๊ณ์ข๋ฒํธ๋ฅผ ๊ฐ์ง ์ ์๋ค. (๋ฆฌ์คํธ๋ก ๋ฝ์์ผ ํ๋ค.)
// @Param ์ฌ์ฉ์ด์ : interface ํ๋ผ๋ฏธํฐ๋ช
๊ณผ xml์ ์ฌ์ฉํ ๋ณ์๋ช
์ ๋ค๋ฅด๊ฒ ์ฌ์ฉํด์ผ ๋๋ค๋ฉด
// @param ์ ๋
ธํ
์ด์
์ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ 2๊ฐ ์ด์์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ๋ฐ๋์ ์ฌ์ฉ.
public List<Account> findByUserId(@Param("userId") Integer principalId);
// --> account id ๊ฐ์ผ๋ก ๊ณ์ข์ ๋ณด ์กฐํ. (ํ์ํ๋ค.)
public Account findByNumber(@Param("number") String id);
// ์ฝ๋ ์ถ๊ฐ ์์
public Account findByAccountId(Integer accountId);
public int countByAccountIdAndType(@Param("type")String type,
@Param("accountId")Integer accountId);
}
history.xml - ์ฟผ๋ฆฌ ์์ ๋ฐ ์ถ๊ฐ
๋๋ณด๊ธฐ
์ฟผ๋ฆฌ ์์ ๋ฐ ์ถ๊ฐ
<select id="findByAccountIdAndTypeOfHistory" resultType="com.tenco.bank.repository.model.HistoryAccount">
<if test="type == 'all'">
select h.id, h.amount,
case
when h.w_account_id = #{accountId} then (h.w_balance)
when h.d_account_id = #{accountId} then (h.d_balance)
end as balance,
coalesce(cast(wa.number as char(10)), 'ATM') as sender,
coalesce(cast(da.number as char(10)), 'ATM') as receiver,
h.created_at
from history_tb as h
left join account_tb as wa on h.w_account_id = wa.id
left join account_tb as da on h.d_account_id = da.id
where h.w_account_id = #{accountId} OR h.d_account_id = #{accountId}
limit #{limit} offset #{offset}
</if>
<if test="type == 'deposit'">
select h.id, h.amount, h.d_balance as balance, h.created_at,
coalesce(CAST(wa.number as CHAR(10)) , 'ATM') as sender,
da.number as receiver
from history_tb as h
left join account_tb as wa on wa.id = h.w_account_id
left join account_tb as da on da.id = h.d_account_id
where h.d_account_id = #{accountId}
limit #{limit} offset #{offset}
</if>
<if test="type == 'withdrawal'">
select h.id, h.amount, h.w_balance AS balance, h.created_at,
coalesce(cast(da.number as CHAR(10)), 'ATM') as receiver,
wa.number as sender
from history_tb as h
left join account_tb as wa on wa.id = h.w_account_id
left join account_tb as da on da.id = h.d_account_id
where h.w_account_id = #{accountId}
limit #{limit} offset #{offset}
</if>
</select>
<select id="countByAccountIdAndType" resultType="int">
<if test="type == 'all'">
select count(*)
from history_tb as h
where h.w_account_id = #{accountId} OR h.d_account_id = #{accountId}
</if>
<if test="type == 'deposit'">
select count(*)
from history_tb as h
where h.d_account_id = #{accountId}
</if>
<if test="type == 'withdrawal'">
select count(*)
from history_tb as h
where h.w_account_id = #{accountId}
</if>
</select>


