07. 댓글 삭제 만들기
본인이 작성한 댓글을 삭제하는 기능을 구현합니다.
삭제 흐름
삭제 버튼 클릭 → 확인 대화상자 → Ajax DELETE → 목록 갱신
JavaScript 구현
function deleteComment(commentId) { if (!confirm('댓글을 삭제하시겠습니까?')) return; fetch('/comments/' + commentId, { method: 'DELETE' }) .then(response => response.json()) .then(data => { if (data.success) { loadComments(currentPostId); } else { alert(data.message); } });}
Controller 구현
@DeleteMapping("/{id}")public Map<String, Object> delete(@PathVariable Long id, HttpSession session) { if (!isOwner(id, session)) { return Map.of("success", false, "message", "삭제 권한이 없습니다."); } commentService.delete(id); return Map.of("success", true);}
Service 구현
@Transactionalpublic void delete(Long id) { commentMapper.delete(id);}
학습 내용
- DELETE 메서드: RESTful 삭제 요청
- 확인 대화상자: 실수 방지를 위한 확인
- 권한 검증: 본인 댓글만 삭제 가능