1. 글 조회하기
- 글 수정 화면시 뿌려지는 데이터
package shop.mtcoding.blog.board; import lombok.AllArgsConstructor; import lombok.Data; import shop.mtcoding.blog.reply.Reply; import shop.mtcoding.blog.user.User; import java.util.ArrayList; import java.util.List; public class BoardResponse { // 게시글 조회하기 화면 @Data public static class LookDTO { private Integer id; private String title; private String content; public LookDTO(Board board) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); } } }
package shop.mtcoding.blog.board; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @RestController public class BoardController { private final HttpSession session; private final BoardService boardService; @GetMapping("/api/boards/{id}") public ResponseEntity<?> lookUp(@PathVariable Integer id) { BoardResponse.LookDTO respDTO = boardService.lookUp(id); return ResponseEntity.ok(new ApiUtil<>(respDTO)); } }
package shop.mtcoding.blog.board; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @Service public class BoardService { private final BoardJPARepository boardJPARepository; public BoardResponse.LookDTO lookUp(int boardId){ // 글조회 Board board = boardJPARepository.findById(boardId) .orElseThrow(() -> new Exception404("게시글을 찾을 수 없습니다")); return new BoardResponse.LookDTO(board); } }
2. 글 쓰기
package shop.mtcoding.blog.board; import lombok.AllArgsConstructor; import lombok.Data; import shop.mtcoding.blog.reply.Reply; import shop.mtcoding.blog.user.User; import java.util.ArrayList; import java.util.List; public class BoardResponse { // 글 쓰기 @Data public static class SaveDTO { private Integer id; private String title; private String content; public SaveDTO(Board board) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); } } }
package shop.mtcoding.blog.board; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @RestController public class BoardController { private final HttpSession session; private final BoardService boardService; @GetMapping("/api/boards/{id}") public ResponseEntity<?> lookUp(@PathVariable Integer id) { BoardResponse.LookDTO respDTO = boardService.lookUp(id); return ResponseEntity.ok(new ApiUtil<>(respDTO)); } }
package shop.mtcoding.blog.board; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @Service public class BoardService { private final BoardJPARepository boardJPARepository; @Transactional public BoardResponse.SaveDTO save(BoardRequest.SaveDTO reqDTO, User sessionUser) { Board board = boardJPARepository.save(reqDTO.toEntity(sessionUser)); return new BoardResponse.SaveDTO(board); // sessionUser 정보는 사용되지 않으므로 제거 } }
3. 글 수정하기
package shop.mtcoding.blog.board; import lombok.AllArgsConstructor; import lombok.Data; import shop.mtcoding.blog.reply.Reply; import shop.mtcoding.blog.user.User; import java.util.ArrayList; import java.util.List; public class BoardResponse { // 게시글 조회하기 화면 @Data public static class UpdateDTO { private Integer id; private String title; private String content; public UpdateDTO(Board board) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); } } }
package shop.mtcoding.blog.board; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @RestController public class BoardController { private final HttpSession session; private final BoardService boardService; @PutMapping("/api/boards/{id}") public ResponseEntity<?> update(@PathVariable Integer id, @RequestBody BoardRequest.UpdateDTO reqDTO) { User sessionUser = (User) session.getAttribute("sessionUser"); BoardResponse.UpdateDTO respDTOd = boardService.update(id, sessionUser.getId(), reqDTO); return ResponseEntity.ok(new ApiUtil<>(respDTOd)); } }
package shop.mtcoding.blog.board; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @Service public class BoardService { private final BoardJPARepository boardJPARepository; public BoardResponse.UpdateDTO update(int boardId, int sessionUserId, BoardRequest.UpdateDTO reqDTO) { // 1. 조회 및 예외처리 Board board = boardJPARepository.findById(boardId) .orElseThrow(() -> new Exception404("게시글을 찾을 수 없습니다")); board.setTitle(reqDTO.getTitle()); board.setContent(reqDTO.getContent()); return new BoardResponse.UpdateDTO(board); }
4. 글 삭제하기
package shop.mtcoding.blog.board; import lombok.AllArgsConstructor; import lombok.Data; import shop.mtcoding.blog.reply.Reply; import shop.mtcoding.blog.user.User; import java.util.ArrayList; import java.util.List; public class BoardResponse { // 게시글 조회하기 화면 @Data public static class DeleteDTO { private Integer id; private String title; private String content; public DeleteDTO(Board board) { this.id = board.getId(); this.title = board.getTitle(); this.content = board.getContent(); } } }
package shop.mtcoding.blog.board; import jakarta.servlet.http.HttpSession; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import shop.mtcoding.blog._core.utils.ApiUtil; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @RestController public class BoardController { private final HttpSession session; private final BoardService boardService; @DeleteMapping("/api/boards/{id}") public ResponseEntity<?> delete(@PathVariable Integer id) { User sessionUser = (User) session.getAttribute("sessionUser"); BoardResponse.DeleteDTO respDTO = boardService.delete(id, sessionUser.getId()); return ResponseEntity.ok(new ApiUtil<>(null)); } }
package shop.mtcoding.blog.board; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Sort; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import shop.mtcoding.blog._core.errors.exception.Exception403; import shop.mtcoding.blog._core.errors.exception.Exception404; import shop.mtcoding.blog.user.User; import java.util.List; @RequiredArgsConstructor @Service public class BoardService { private final BoardJPARepository boardJPARepository; @Transactional public BoardResponse.DeleteDTO delete(int boardId, Integer sessionUserId) { Board board = boardJPARepository.findById(boardId) .orElseThrow(() -> new Exception404("게시글을 찾을 수 없습니다")); if (sessionUserId != board.getUser().getId()) { throw new Exception403("게시글을 삭제할 권한이 없습니다"); } boardJPARepository.deleteById(boardId); return new BoardResponse.DeleteDTO(board); }
Share article