게시물 수정하기는 삭제하기와 다르게 수정 버튼을 누르면 내용을 수정한 후 수정 내용을 서버로 보내는 과정이 추가된다.
PostsController의 editForm 메서드 (GET /posts/{id}/edit) 호출editForm 메서드 동작
postId와 로그인된 사용자(SessionUser.id)를 Service 계층으로 전달findByIdWithUser(postId) 호출 → Post + User를 fetch join으로 조회PostNotFoundException 발생PostAccessDeniedException 발생PostEditFormResponseDto(postId, title, content, category) 반환model에 담아 write.html 반환
write.html은 원래 글 작성 템플릿이지만 mode(create/edit)를 함께 전달하여 동적으로 렌더링PostsController의 update 메서드 (PATCH /posts/{id}) 호출update 메서드 동작
postId와 수정 데이터(PostUpdateRequestDto)를 Service 계층으로 전달findByIdWithUser(postId) 호출 → Post + User 조회PostNotFoundException 발생PostAccessDeniedException 발생setTitle, setContent, setCategory)<!--수정 버튼 (작성자만 보이게)-->
<div th:if="${session.user !=null and session.user.id == post.userId}">
<a th:href="@{/posts/{id}/edit(id=${post.postId})}" class="write-btn">수정하기</a>
</div>
/posts/{id}로 GET요청@GetMapping("/{id}/edit")
public String editForm(@PathVariable Long id, @SessionAttribute("user") SessionUser sessionUser, Model model){
PostEditFormResponseDto post = postsService.findForEdit(id, sessionUser.getId());
model.addAttribute("mode","edit");
model.addAttribute("post",post);
return "write";
}
mode를 넘긴다.