방명록
- [Spring Boot][FastCampus][Final Project] 스프링 부트를 이용한 게시판 프로그램 ③ - 컨트롤러 클래스 설계와 구현2022년 08월 30일 00시 52분 59초에 업로드 된 글입니다.작성자: DandyNow728x90반응형
과제 상세 7
컨트롤러 클래스는 게시판 관련 컨트롤러 하나로 모든 요청을 처리하도록 작성합니다.
src/main/java/com/fastcampus/board/BoardController.java
package com.fastcampus.board; import java.util.List; import javax.validation.Valid; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.fastcampus.board.dto.BoardDto; import com.fastcampus.board.service.BoardService; @Controller public class BoardController { private BoardService boardService; public BoardController(BoardService boardService) { this.boardService = boardService; } @RequestMapping(value="/save",method = RequestMethod.POST) public String createPost(@Valid @ModelAttribute("command") BoardDto postDto){ System.out.println("save " + postDto); boardService.savePost(postDto); return "redirect:/"; // 추가 후 홈 화면으로 } @RequestMapping("/") public String ReadAllPost(Model model){ List<BoardDto> postList = boardService.getBoardList(); model.addAttribute("postList", postList); return "index"; } @RequestMapping(value="/update",method = RequestMethod.POST) public String updatePost(@Valid @ModelAttribute("command") BoardDto postDto){ System.out.println("update " + postDto); boardService.updatePost(postDto); return "redirect:/"; } @RequestMapping(value="/delete/{id}",method = RequestMethod.GET) public String deletePost(@PathVariable Long id){ System.out.println("삭제 " + id); boardService.deletePost(id); return "redirect:/"; } @RequestMapping(value="/createView") public String ViewCreate(Model model){ model.addAttribute("command", new BoardDto()); return "create"; } @RequestMapping(value="/updateView/{id}") public String ViewUpdate(@PathVariable Long id, Model model){ BoardDto boardDto = boardService.getPost(id); // 게시물 조회(수정전 기존 값 불러오기) BoardDto postDto = new BoardDto(); postDto.setSeq(id); postDto.setWriter(boardDto.getWriter()); postDto.setContent(boardDto.getContent()); postDto.setTitle(boardDto.getTitle()); postDto.setCnt(boardDto.getCnt()); model.addAttribute("command",postDto); return "update"; } }
과제 상세 8
비즈니스 컴포넌트는 인터페이스를 제공하지 않도록 설계했기 때문에 컨트롤러 클래스가 비즈니스 컴포넌트를 사용할 때 비즈니스 클래스를 직접 사용하도록 구현합니다.
src/main/java/com/fastcampus/board/service/BoardService.java
package com.fastcampus.board.service; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.fastcampus.board.dto.BoardDto; import com.fastcampus.board.model.entity.Board; import com.fastcampus.board.repository.BoardRepository; @Service public class BoardService { private BoardRepository boardRepository; public BoardService(BoardRepository boardRepository) { this.boardRepository = boardRepository; } // 게시물 생성 @Transactional public Long savePost(BoardDto boardDto) { return boardRepository.save(boardDto.toEntity(0L)).getSeq(); } // 게시물 수정 @Transactional public Long updatePost(BoardDto boardDto) { Board board = boardRepository.findById(boardDto.getSeq()).get(); System.out.println("board : "+board); return boardRepository.save(boardDto.toEntity(board.getCnt())).getSeq(); } // 게시물 전체 가져오기 @Transactional public List<BoardDto> getBoardList() { Iterable<Board> boardList = boardRepository.findAll(); List<BoardDto> boardDtoList = new ArrayList<>(); for(Board board : boardList) { BoardDto boardDto = BoardDto.builder() .seq(board.getSeq()) .writer(board.getWriter()) .title(board.getTitle()) .content(board.getContent()) .regDate(board.getRegDate()) .cnt(board.getCnt()) .build(); boardDtoList.add(boardDto); } return boardDtoList; } // 게시물 삭제 @Transactional public void deletePost(Long id) { boardRepository.deleteById(id); } // 게시물 상세 조회 @Transactional public BoardDto getPost(Long id) { Board board = boardRepository.findById(id).get(); // 조회수 +1 if(board.getCnt() == null) { board.setCnt(1L); } else { board.setCnt(board.getCnt()+1); } BoardDto boardDto = BoardDto.builder() .seq(board.getSeq()) .writer(board.getWriter()) .title(board.getTitle()) .content(board.getContent()) .regDate(board.getRegDate()) .build(); return boardDto; } }
728x90반응형'언어·프레임워크 > Spring Boot' 카테고리의 다른 글
다음글이 없습니다.이전글이 없습니다.댓글