Spring boot

Bank App 만들기 - 계좌 상세보기 페이징처리

햄발자 2024. 9. 27. 15:38

 

 

학습 목표

  • 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>&nbsp;
			<a href="/account/detail/${account.id}?type=deposit" class="btn btn-outline-primary" >입금</a>&nbsp;
			<a href="/account/detail/${account.id}?type=withdrawal" class="btn btn-outline-primary" >출금</a>&nbsp;
		</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>