Joshua게시판(SSR)-18-게시글 삭제하기

jaegeunsong97·2023년 8월 16일
0

SSR 기반 JoshuaBlog

목록 보기
18/23

깃허브링크

노션링크

  • detail.jsp

song 로그인

park 수정 삭제 없음

song 존재

  • boardController
@PostMapping("/s/board/{id}/delete")
     public String delete(@PathVariable Long id, @AuthenticationPrincipal MyUserDetails myUserDetails) {
          boardService.게시글삭제(id, myUserDetails.getUser().getId());
          return "redirect:/";
     }
  • boardService
@Transactional
     public void 게시글삭제(Long id, Long userId) {
          Board boardPS = boardRepository.findByIdFetchUser(id).orElseThrow(
                    () -> new Exception400("id", "게시글 아이디를 찾을 수 없습니다"));

          if (boardPS.getUser().getId() != userId) {
               throw new Exception403("권한이 없습니다");
          }

          try {
               boardRepository.delete(boardPS);
          } catch (Exception e) {
               throw new Exception500("게시글 삭제 실패 : " + e.getMessage());
          }
     }

delete. update, insert -> 검증 후에 처리(앞에서 DB 조회 여부 확인해야함)

  • boardRepository
@Query("select b from Board b join fetch b.user where b.id = :id")
     Optional<Board> findByIdFetchUser(@Param("id") Long id);
  • 테스트

10번 삭제 후

  • 포스트맨 테스트

ssar 로그인 -> 1번 삭제

profile
현재 블로그 : https://jasonsong97.tistory.com/

0개의 댓글