728x90
반응형
[SpringBoot] 17. 계좌 목록 기능 구현
(1) list.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>
<c:choose>
<c:when test="${accountList!=null}">
<%-- 계좌 존재 : html 주석 -- 오류 --%>
<table class="table">
<thead>
<tr>
<th>계좌 번호</th>
<th>잔액</th>
</tr>
</thead>
<tbody>
<c:forEach var="account" items="${accountList}">
<tr>
<td>${account.number}</td>
<td>${account.balance}</td>
</tr>
</c:forEach>
</tbody>
</table>
</c:when>
<c:otherwise>
<div class="jumbotron display-4">
<h5>아직 생성된 계좌가 없습니다.</h5>
</div>
</c:otherwise>
</c:choose>
<%-- 계좌가 없는 경우와 있는 경우를 분리하자. --%>
<%-- 계좌가 있는 사용자일 경우, 반복문을 활용할 예정 --%>
</div>
<%-- end of col-sm-8 --%>
</div>
</div>
<%-- footer.jsp --%>
<%@ include file="/WEB-INF/view/layout/footer.jsp"%>
(2) accountController.java
package com.tenco.bank.controller;
import java.util.List;
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.tenco.bank.dto.SaveDTO;
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.User;
import com.tenco.bank.service.AccountService;
import jakarta.servlet.http.HttpSession;
@Controller // IoC 대상(싱글톤으로 관리)
@RequestMapping("/account")
public class AccountController {
// 계좌 생성 화면 요청 DI 처리
private final HttpSession session;
private final AccountService accountService;
public AccountController(HttpSession session,AccountService accountService) {
this.session=session;
this.accountService=accountService;
if(session==null) {
throw new UnAuthorizedException("인증된 사용자가 아닙니다.", HttpStatus.UNAUTHORIZED);
}
}
/**
* 계좌 생성 페이지 요청
* 주소 설계 : http://localhost:8080/account/save
* @return save.jsp
*/
@GetMapping("/save")
public String savePage() {
// 1. 인증 검사가 필요(account 전체 필요함)
User principal = (User)session.getAttribute("principal");
if(principal==null) {
throw new UnAuthorizedException("인증된 사용자가 아닙니다.", HttpStatus.UNAUTHORIZED);
}
return "account/save";
}
/**
* 계좌 생성 기능 요청
* 주소 설계 : http://localhost:8080/account/save
* @param dto
* @return
*/
@PostMapping("/save")
public String saveProc(SaveDTO dto) {
// 1. form 데이터 추출(파싱 전략)
// 2. 인증 검사
// 3. 유효성 검사
// 4. 서비스 호출
User principal = (User) session.getAttribute("principal");
if(principal==null) {
throw new UnAuthorizedException("인증된 사용자가 아닙니다.", HttpStatus.UNAUTHORIZED);
}
if(dto.getNumber()==null||dto.getNumber().isEmpty()) {
throw new DataDeliveryException("계좌 번호를 입력하세요.", HttpStatus.BAD_REQUEST);
}
if(dto.getPassword()==null||dto.getPassword().isEmpty()) {
throw new DataDeliveryException("계좌 비밀 번호를 입력하세요.", HttpStatus.BAD_REQUEST);
}
if(dto.getBalance()==null||dto.getBalance()<=0) {
throw new DataDeliveryException("계좌 잔액을 입력하세요.", HttpStatus.BAD_REQUEST);
}
accountService.createAccount(dto,principal.getId());
return "redirect:/index";
}
/**
* 계좌 목록 화면 요청
* 주소 설계: http://localhost:8080/account/list ...
* @return list.jsp
*/
@GetMapping({"/list","/"})
public String listPage(Model model) {
// 1. 인증 검사
User principal=(User)session.getAttribute("principal");
if(principal==null) {
throw new UnAuthorizedException("인증된 사용자가 아닙니다.", 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";
}
}
(3) accountService.java
package com.tenco.bank.service;
import java.util.ArrayList;
import java.util.List;
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.SaveDTO;
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.model.Account;
import com.tenco.bank.repository.model.User;
@Service
public class AccountService {
private final AccountRepository accountRepository;
@Autowired
public AccountService(AccountRepository accountRepository) {
this.accountRepository=accountRepository;
}
/**
* 계좌 생성 기능
* @param dto
* @param id
*/
// 트랜잭션
@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 DataDeliveryException("알 수 없는 오류입니다.", HttpStatus.SERVICE_UNAVAILABLE);
}
if(result==0) {
throw new DataDeliveryException("정상 처리되지 않았습니다.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
// 원자성을 위한 트랜잭션 처리
// @Transactional
public List<Account> readAccountListByUserId(Integer userId) {
List<Account> accountListEntity=new ArrayList<>();
try {
accountListEntity=accountRepository.findByUserId(userId);
} catch (DataAccessException e) {
throw new DataDeliveryException("잘못된 처리입니다.", HttpStatus.INTERNAL_SERVER_ERROR);
} catch (Exception e) {
throw new RedirectException("알 수 없는 오류", HttpStatus.SERVICE_UNAVAILABLE);
}
return accountListEntity;
}
}
728x90
반응형
'Springboot' 카테고리의 다른 글
[Springboot] 19. 출금 기능 만들기 (0) | 2024.08.08 |
---|---|
[SpringBoot] 18. 중간 리팩토링 (0) | 2024.08.08 |
[SpringBoot] **16. 중간점검 및 헷갈리는 부분들 정리(1) (0) | 2024.08.07 |
[SpringBoot] 15. 계좌 생성 및 유효성 검사 (0) | 2024.08.07 |
[SpringBoot] 14. 헤더 링크 설정 및 JSTL 태그 활용하기 (0) | 2024.08.07 |