1. BoardService 에 save() 만들기
package shop.mtcoding.blog.board; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog.user.User; @RequiredArgsConstructor @Service public class BoardService { private final BoardJPARepository boardJPARepository; @Transactional public void save(BoardRequest.SaveDTO reqDTO, User sessionUser) { boardJPARepository.save(reqDTO.toEntity(sessionUser)); } }
2. BoardController 에 save 수정하기
package shop.mtcoding.blog.board; import ch.qos.logback.core.model.Model; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.user.User; import java.lang.annotation.Native; import java.util.List; @RequiredArgsConstructor @Controller public class BoardController { private final BoardRepository boardRepository; private final HttpSession session; private final BoardService boardService; // @Transactional 트랜잭션 시간이 너무 길어져서 service에 넣어야함 @PostMapping("/board/{id}/update") public String update(@PathVariable Integer id, BoardRequest.UpdateDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); Board board = boardRepository.findById(id); if (sessionUser.getId() != board.getUser().getId()) { throw new Exception403("게시글을 수정할 권한이 없습니다"); } boardRepository.updateById(id, reqDTO.getTitle(), reqDTO.getContent()); return "redirect:/board/" + id; } @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable(name = "id") Integer id, HttpServletRequest request) { Board board = boardRepository.findById(id); if (board == null) { throw new Exception404("해당 게시글을 찾을 수 없습니다"); } request.setAttribute("board", board); return "/board/update-form"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id) { User sessionUser = (User) session.getAttribute("sessionUser"); Board board = boardRepository.findById(id); if (sessionUser.getId() != board.getUser().getId()) { throw new Exception403("게시글을 삭제할 권한이 없습니다"); } boardRepository.deleteById(id); return "redirect:/"; } @GetMapping("/") public String index(HttpServletRequest request) { List<Board> boardList = boardRepository.findAll(); request.setAttribute("boardList", boardList); return "index"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/save") public String save(BoardRequest.SaveDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); boardService.save(reqDTO, sessionUser); return "redirect:/"; } @GetMapping("/board/save-form") public String saveForm() { return "board/save-form"; } @GetMapping("/board/{id}") public String detail(@PathVariable Integer id, HttpServletRequest request) { User sessionUser = (User) session.getAttribute("sessionUser"); Board board = boardRepository.findByIdJoinUser(id); // 로그인을 하고, 게시글의 주인이면 isOwner가 true가 된다. boolean isOwner = false; if(sessionUser != null){ if(sessionUser.getId() == board.getUser().getId()){ isOwner = true; } } request.setAttribute("isOwner", isOwner); request.setAttribute("board", board); return "board/detail"; } }
Share article