6. 게시판 글수정하기v1

송민경's avatar
Mar 12, 2024
6. 게시판 글수정하기v1

1. View 확인하기

notion image
notion image
 

2. BoardController 에 updateForm() 만들기

  • get 요청 : 수정 페이지 요청
  • post 요청 : 수정 액션 요청
  • get 요청 : 수정한 페이지로 이동
이 페이지를 기억하기 위해 session에 저장해서 이동할 수 있음
package shop.mtcoding.blog.board; import ch.qos.logback.core.model.Model; import jakarta.servlet.http.HttpServletRequest; 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 java.util.List; @RequiredArgsConstructor @Controller public class BoardController { private final BoardNativeRepository boardNativeRepository; @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable (name="id") Integer id, HttpServletRequest request) { return "/board/update-form"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id) { // DTO 없이 구현 boardNativeRepository.deleteById(id); return "redirect:/"; } @GetMapping("/") public String index(HttpServletRequest request) { // 조회하기 List<Board> boardList = boardNativeRepository.findAll(); // 가방에 담기 request.setAttribute("boardList", boardList); return "index"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/save") public String save(String title, String content, String username) { // DTO 없이 구현 boardNativeRepository.save(title, content, username); return "redirect:/"; } @GetMapping("/board/save-form") public String saveForm() { return "board/save-form"; } @GetMapping("/board/{id}") public String detail(@PathVariable Integer id, HttpServletRequest request) { // Integer : 없으면 null, int : 0 Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "board/detail"; } }
 

3. update-form 화면 만들기

{{> /layout/header}} <div class="container p-5"> <!-- 요청을 하면 localhost:8080/board/save POST로 요청됨 title=사용자입력값&content=사용자값 --> <div class="card"> <div class="card-header"><b>글수정하기 화면입니다</b></div> <div class="card-body"> <form action="/board/1/update" method="post"> <div class="mb-3"><!--username 추가--> <input type="text" class="form-control" placeholder="Enter username" name="username" value="ssar"> </div> <div class="mb-3"> <input type="text" class="form-control" placeholder="Enter title" name="title" value="제목1"> </div> <div class="mb-3"> <textarea class="form-control" rows="5" name="content" value="내용1"></textarea> </div> <button type="submit" class="btn btn-primary form-control">글수정하기완료</button> </form> </div> </div> </div> {{> /layout/footer}}
notion image
 

4. BoardController 에 updateForm() 수정하기

package shop.mtcoding.blog.board; import ch.qos.logback.core.model.Model; import jakarta.servlet.http.HttpServletRequest; 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 java.util.List; @RequiredArgsConstructor @Controller public class BoardController { private final BoardNativeRepository boardNativeRepository; @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable (name="id") Integer id, HttpServletRequest request) { Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "/board/update-form"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id) { // DTO 없이 구현 boardNativeRepository.deleteById(id); return "redirect:/"; } @GetMapping("/") public String index(HttpServletRequest request) { // 조회하기 List<Board> boardList = boardNativeRepository.findAll(); // 가방에 담기 request.setAttribute("boardList", boardList); return "index"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/save") public String save(String title, String content, String username) { // DTO 없이 구현 boardNativeRepository.save(title, content, username); return "redirect:/"; } @GetMapping("/board/save-form") public String saveForm() { return "board/save-form"; } @GetMapping("/board/{id}") public String detail(@PathVariable Integer id, HttpServletRequest request) { // Integer : 없으면 null, int : 0 Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "board/detail"; } }
 

5. update-form 수정하기

{{> /layout/header}} <div class="container p-5"> <!-- 요청을 하면 localhost:8080/board/save POST로 요청됨 title=사용자입력값&content=사용자값 --> <div class="card"> <div class="card-header"><b>글수정하기 화면입니다</b></div> <div class="card-body"> <form action="/board/{{board.id}}/update" method="post"> <div class="mb-3"><!--username 추가--> <input type="text" class="form-control" placeholder="Enter username" name="username" value="{{board.username}}"> </div> <div class="mb-3"> <input type="text" class="form-control" placeholder="Enter title" name="title" value="{{board.title}}"> </div> <div class="mb-3"> <textarea class="form-control" rows="5" name="content">{{board.content}}</textarea> </div> <button type="submit" class="btn btn-primary form-control">글수정하기완료</button> </form> </div> </div> </div> {{> /layout/footer}}
notion image
 

6.

notion image
 

7. BoardController 에 update() 만들기

package shop.mtcoding.blog.board; import ch.qos.logback.core.model.Model; import jakarta.servlet.http.HttpServletRequest; 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 java.util.List; @RequiredArgsConstructor @Controller public class BoardController { private final BoardNativeRepository boardNativeRepository; @PostMapping("/board/{id}/update") public String update(@PathVariable Integer id, String title, String content, String username) { // DTO 없이 구현 System.out.println("id : " + id); System.out.println("title : " + title); System.out.println("content : " + content); System.out.println("username : " + username); return "redirect:/board/" + id; } @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable (name="id") Integer id, HttpServletRequest request) { Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "/board/update-form"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id) { // DTO 없이 구현 boardNativeRepository.deleteById(id); return "redirect:/"; } @GetMapping("/") public String index(HttpServletRequest request) { // 조회하기 List<Board> boardList = boardNativeRepository.findAll(); // 가방에 담기 request.setAttribute("boardList", boardList); return "index"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/save") public String save(String title, String content, String username) { // DTO 없이 구현 boardNativeRepository.save(title, content, username); return "redirect:/"; } @GetMapping("/board/save-form") public String saveForm() { return "board/save-form"; } @GetMapping("/board/{id}") public String detail(@PathVariable Integer id, HttpServletRequest request) { // Integer : 없으면 null, int : 0 Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "board/detail"; } }
notion image
  • 단위 테스트 하기
package shop.mtcoding.blog.Board; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; import shop.mtcoding.blog.board.Board; import shop.mtcoding.blog.board.BoardNativeRepository; import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @Import(BoardNativeRepository.class) @DataJpaTest public class BoardNativeRepositoryTest { @Autowired // DI private BoardNativeRepository boardNativeRepository; @Test public void updateById_test(){ // given int id = 1; String title = "제목수정1"; String content = "내용수정1"; String username = "이름수정1"; // when boardNativeRepository.updateById(id, title, content, username); // then Board board = boardNativeRepository.findById(id); System.out.println("updateById_test/board : "+board); assertThat(board.getTitle()).isEqualTo("제목수정2"); assertThat(board.getContent()).isEqualTo("내용수정1"); assertThat(board.getUsername()).isEqualTo("이름수정1"); } @Test public void findById_test() { //given - 지금은 넣을게 없음 int id = 1; //when Board board = boardNativeRepository.findById(id); System.out.println("findById_test : " + board); //then //org.assertj.core.api Assertions.assertThat(board.getTitle()).isEqualTo("제목1"); Assertions.assertThat(board.getContent()).isEqualTo("내용1"); } @Test public void findAll_test() { //given - 지금은 넣을게 없음 //when List<Board> boardList = boardNativeRepository.findAll(); //then System.out.println("findAll_test/size : " + boardList.size()); System.out.println("findAll_test/username : " + boardList.get(2).getUsername()); //org.assertj.core.api Assertions.assertThat(boardList.size()).isEqualTo(4); Assertions.assertThat(boardList.get(2).getUsername()).isEqualTo("ssar"); } @Test public void deleteById_test(){ // given int id = 1; // when boardNativeRepository.deleteById(id); // then List<Board> boardList = boardNativeRepository.findAll(); assertThat(boardList.size()).isEqualTo(2); } }
notion image
notion image

8. BoardNativeRepository 에 update() 수정하기

package shop.mtcoding.blog.board; import ch.qos.logback.core.model.Model; import jakarta.servlet.http.HttpServletRequest; 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 java.util.List; @RequiredArgsConstructor @Controller public class BoardController { private final BoardNativeRepository boardNativeRepository; @PostMapping("/board/{id}/update") public String update(@PathVariable Integer id, String title, String content, String username){ boardNativeRepository.updateById(id, title, content, username); return "redirect:/board/"+id; } @GetMapping("/board/{id}/update-form") public String updateForm(@PathVariable (name="id") Integer id, HttpServletRequest request) { Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "/board/update-form"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/{id}/delete") public String delete(@PathVariable Integer id) { // DTO 없이 구현 boardNativeRepository.deleteById(id); return "redirect:/"; } @GetMapping("/") public String index(HttpServletRequest request) { // 조회하기 List<Board> boardList = boardNativeRepository.findAll(); // 가방에 담기 request.setAttribute("boardList", boardList); return "index"; // 서버가 내부적으로 index를 요청 - 외부에서는 다이렉트 접근이 안됨 } @PostMapping("/board/save") public String save(String title, String content, String username) { // DTO 없이 구현 boardNativeRepository.save(title, content, username); return "redirect:/"; } @GetMapping("/board/save-form") public String saveForm() { return "board/save-form"; } @GetMapping("/board/{id}") public String detail(@PathVariable Integer id, HttpServletRequest request) { // Integer : 없으면 null, int : 0 Board board = boardNativeRepository.findById(id); request.setAttribute("board", board); return "board/detail"; } }
notion image
Share article

vosw1