From ca2afb6288b0d2c85d1bde7246baeb4c5e087ee4 Mon Sep 17 00:00:00 2001 From: Dae-Hee-Ryu Date: Mon, 17 Apr 2023 20:00:08 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=20=EA=B3=BC=EC=A0=9C=20=EC=BB=A4=EB=B0=8B?= =?UTF-8?q?,=20=EA=B3=BC=EC=A0=9C=20=EA=B4=80=EB=A0=A8=20=EB=82=B4?= =?UTF-8?q?=EC=9A=A9=EC=9D=B4=20=EC=9D=B4=EB=AF=B8=20=EC=A0=84=EB=B6=80=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1=EB=90=98=EC=96=B4=EC=9E=88=EC=96=B4=EC=84=9C?= =?UTF-8?q?=20=EC=97=90=EB=9F=AC=20=EC=B2=98=EB=A6=AC=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EB=82=B4=EC=9A=A9=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RealCodingServerApplication.java | 3 +-- .../controller/PostController.java | 6 +++++ .../service/PostService.java | 24 +++++++++++-------- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/cnu/real_coding_server/RealCodingServerApplication.java b/src/main/java/com/cnu/real_coding_server/RealCodingServerApplication.java index fc49dd5..6e4c715 100644 --- a/src/main/java/com/cnu/real_coding_server/RealCodingServerApplication.java +++ b/src/main/java/com/cnu/real_coding_server/RealCodingServerApplication.java @@ -6,8 +6,7 @@ @EnableJpaAuditing @SpringBootApplication -public class RealCodingServerApplication { - +public class RealCodingServerApplication public static void main(String[] args) { SpringApplication.run(RealCodingServerApplication.class, args); } diff --git a/src/main/java/com/cnu/real_coding_server/controller/PostController.java b/src/main/java/com/cnu/real_coding_server/controller/PostController.java index c31c90e..67bd2e8 100644 --- a/src/main/java/com/cnu/real_coding_server/controller/PostController.java +++ b/src/main/java/com/cnu/real_coding_server/controller/PostController.java @@ -42,4 +42,10 @@ public ResponseEntity deletePost(@PathVariable("postId") Integer postId) { return ResponseEntity.noContent().build(); } + + @ExceptionHandler(ResourceNotFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public String handleResourceNotFoundException(ResourceNotFoundException exception) { + return exception.getMessage(); + } } diff --git a/src/main/java/com/cnu/real_coding_server/service/PostService.java b/src/main/java/com/cnu/real_coding_server/service/PostService.java index 490a745..c47da9f 100644 --- a/src/main/java/com/cnu/real_coding_server/service/PostService.java +++ b/src/main/java/com/cnu/real_coding_server/service/PostService.java @@ -24,21 +24,25 @@ public List getPosts() { } public Optional getPost(Integer postId) { - return postRepository.findById(postId); + return postRepository.findById(postId) + .orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId)); } public Optional updatePost(Integer postId, PostRequest postRequest) { - return postRepository.findById(postId) - .map(post -> { - post.setTitle(postRequest.getTitle()); - post.setContents(postRequest.getContents()); - post.setTag(postRequest.getTag()); - return postRepository.save(post); - }); + Post post = postRepository.findById(postId) + .orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId)); + + post.setTitle(postRequest.getTitle()); + post.setContents(postRequest.getContents()); + post.setTag(postRequest.getTag()); + + return Optional.of(postRepository.save(post)); } public void deletePost(Integer postId) { - postRepository.findById(postId) - .ifPresent(postRepository::delete); + Post post = postRepository.findById(postId) + .orElseThrow(() -> new ResourceNotFoundException("Post not found with id: " + postId)); + + postRepository.delete(post); } }