5. 게시판 : 삭제하기v1

송민경's avatar
Mar 12, 2024
5. 게시판 : 삭제하기v1

1. View 확인하기

notion image
notion image
 

2. BoardNativeRepository에서 deleteById() 만들기

package shop.mtcoding.blog.board; import jakarta.persistence.EntityManager; import jakarta.persistence.Query; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Repository; import java.util.List; // 나중에 안쓰고 새로운 레파지토리를 쓰고 이건 버릴거임 @RequiredArgsConstructor @Repository public class BoardNativeRepository { private final EntityManager em; public Board findById(int id) { Query query = em.createNativeQuery("select * from board_tb where id = ?", Board.class); query.setParameter(1, id); return (Board) query.getSingleResult(); } public List<Board> findAll() { Query query = em.createNativeQuery("select * from board_tb order by id desc", Board.class); // join은 DTO로 받아야 return (List<Board>) query.getResultList(); // 캐스팅 해주기 } @Transactional public void save(String title, String content, String username) { Query query = em.createNativeQuery("insert into board_tb (title, content, username, created_at) values (?,?,?,now())"); query.setParameter(1, title); query.setParameter(2, content); query.setParameter(3, username); query.executeUpdate(); } @Transactional public void deleteById(int id) { Query query = em.createNativeQuery("delete * from board_tb where id=?"); query.setParameter(1, id); query.executeUpdate(); } }
  • 단위 테스트하기
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 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

3. BoardController 에 delete() 만들기

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}/delete") public String delete(@PathVariable int 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