Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 형 변환
- OPP개념
- multi-threading
- continue문
- Thread
- 자바 멀티스레딩
- 컴파일
- 접근제어지시자
- 집합관계
- 시스템 환경 변수 편집
- 인텔리제이 한글 깨짐 해결법
- this예약어
- JAVA객체지향
- JAVA기초
- 포함관계
- break문
- 메서드 오버로딩
- Java
- IntelliJ IDEA
- 상수
- 반복문
- 연관관계
- function
- While
- 인텔리제이 기초 설정
- 메서드
- for문
- java변수
- 생성자
- Java데이터 타입
Archives
- Today
- Total
최원종의 개발 블로그
V16-4 유료 게시글 구매하기 본문

BoardController 코드 추가⬇️
// /board/{{board.id}}/purchase
@PostMapping("/board/{id}/purchase")
public String purchase(@PathVariable(name = "id") Integer boardId, HttpSession session) {
User sessionUser = (User) session.getAttribute(Define.SESSION_USER);
// 포인트 차감
// 구매 이력 저장
User updatedUser = purchaseService.구매하기(sessionUser.getId(), boardId);
// 세션 동기화 처리
session.setAttribute(Define.SESSION_USER, updatedUser);
return "redirect:/board/" + boardId;
}
더보기
@Slf4j
@Controller // IoC
@RequiredArgsConstructor // DI
public class BoardController {
private final BoardService boardService;
// 댓글 목록 조회시 필요
private final ReplyService replyService;
private final PurchaseService purchaseService;
// /board/{{board.id}}/purchase
@PostMapping("/board/{id}/purchase")
public String purchase(@PathVariable(name = "id") Integer boardId, HttpSession session) {
User sessionUser = (User) session.getAttribute(Define.SESSION_USER);
// 포인트 차감
// 구매 이력 저장
User updatedUser = purchaseService.구매하기(sessionUser.getId(), boardId);
// 세션 동기화 처리
session.setAttribute(Define.SESSION_USER, updatedUser);
return "redirect:/board/" + boardId;
}
// ... 생략
}



사용자 예외 클래스 추가
package com.tenco.blog._core.errors;
// 401 NotEnoughException
public class NotEnoughException extends RuntimeException {
public NotEnoughException(String msg) {
super(msg);
}
}
GlobalExceptionHandler
@ExceptionHandler(NotEnoughException.class)
@ResponseBody
public String handleNotEnoughException(NotEnoughException e, HttpServletRequest request) {
log.warn("=== 포인트 부족 예외 처리 발생 ===");
log.warn("요청 URL: {}", request.getRequestURL());
log.warn("에러메시지: {}", e.getMessage());
String message = e.getMessage() != null ? e.getMessage() : "포인트가 부족합니다";
String escapeMessage = message.replace("'", "\\'" );
return """
<script>
alert('%s');
location.href='/user/detail'
</script>
""".formatted(escapeMessage);
}
User
// 포인트 관련 편의 메서드 추가
public void deductPoint(Integer amount) {
if(amount == null || amount <= 0 ) {
throw new Exception400("차감할 포인트는 0보다 커야 합니다");
}
if(this.point < amount) {
throw new NotEnoughException("포인트가 부족합니다. 현재 포인트 : " + this.point);
}
this.point -= amount;
}'Spring boot 입문 > Spring boot 입문 -1' 카테고리의 다른 글
| V17 PG사 연결 - 포트원 연동하기 (0) | 2026.06.25 |
|---|---|
| V16-3 Board 상세보기 유료/무료 구분 (0) | 2026.06.25 |
| V16-2 포인트 시스템의 개념과 유료 게시글 (0) | 2026.06.25 |
| V16-1 포인트 시스템(유료 게시글 기능 추가) (0) | 2026.06.23 |
| V15-4 이메일 인증 후 회원가입 로직 변경 (0) | 2026.06.23 |
