From f8b161a149fc44fbca3a9f2aa5c546ec78d3c646 Mon Sep 17 00:00:00 2001 From: Jeonkyujin <954qew@naver.com> Date: Thu, 21 Nov 2024 18:39:32 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cotato/backend/common/dto/DataResponse.java | 4 ++++ .../backend/common/exception/GlobalExceptionHandler.java | 4 ++-- .../java/cotato/backend/domains/post/PostController.java | 8 ++++++++ .../java/cotato/backend/domains/post/PostService.java | 9 +++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/backend/src/main/java/cotato/backend/common/dto/DataResponse.java b/backend/src/main/java/cotato/backend/common/dto/DataResponse.java index 4802e59..142478e 100644 --- a/backend/src/main/java/cotato/backend/common/dto/DataResponse.java +++ b/backend/src/main/java/cotato/backend/common/dto/DataResponse.java @@ -24,4 +24,8 @@ public static DataResponse from(T data) { public static DataResponse ok() { return new DataResponse<>(HttpStatus.OK, null); } + + public static DataResponse success(String message) { + return new DataResponse<>(HttpStatus.OK, message); + } } diff --git a/backend/src/main/java/cotato/backend/common/exception/GlobalExceptionHandler.java b/backend/src/main/java/cotato/backend/common/exception/GlobalExceptionHandler.java index f052f14..5959602 100644 --- a/backend/src/main/java/cotato/backend/common/exception/GlobalExceptionHandler.java +++ b/backend/src/main/java/cotato/backend/common/exception/GlobalExceptionHandler.java @@ -13,10 +13,10 @@ public class GlobalExceptionHandler { @ExceptionHandler(ApiException.class) - public ResponseEntity handleApiException(ApiException e) { + public ResponseEntity handleApiException(ApiException e) { log.warn("handleApiException", e); - return makeErrorResponseEntity(e.getHttpStatus(), e.getMessage(), e.getCode()); + return ResponseEntity.status(e.getHttpStatus()).body(e.getMessage()); } private ResponseEntity makeErrorResponseEntity(HttpStatus httpStatus, String message, String code) { diff --git a/backend/src/main/java/cotato/backend/domains/post/PostController.java b/backend/src/main/java/cotato/backend/domains/post/PostController.java index 5c41cf6..390a324 100644 --- a/backend/src/main/java/cotato/backend/domains/post/PostController.java +++ b/backend/src/main/java/cotato/backend/domains/post/PostController.java @@ -7,6 +7,7 @@ import org.springframework.data.domain.Page; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -67,5 +68,12 @@ public ResponseEntity> readByView(@RequestParam(defaultValue return ResponseEntity.ok(response); } + @DeleteMapping("/delete/{id}") + public ResponseEntity> deletePostBySingle(@PathVariable Long id) { + postService.deletePostBySingle(id); + + return ResponseEntity.ok(DataResponse.success("게시글이 성공적으로 삭제 되었습니다.")); + } + } diff --git a/backend/src/main/java/cotato/backend/domains/post/PostService.java b/backend/src/main/java/cotato/backend/domains/post/PostService.java index da5d9f2..c635799 100644 --- a/backend/src/main/java/cotato/backend/domains/post/PostService.java +++ b/backend/src/main/java/cotato/backend/domains/post/PostService.java @@ -9,6 +9,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -64,4 +65,12 @@ public Page getPosts(int page, int size) { Pageable pageable = PageRequest.of(page, size); return postRepository.findAllByOrderByViewsDesc(pageable); } + + public void deletePostBySingle(Long id) { + Post post = postRepository.findById(id) + .orElseThrow(() -> ApiException.of(HttpStatus.NOT_FOUND,"해당 ID의 게시글을 찾을 수 없습니다", "")); + postRepository.delete(post); + + } + }