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); + + } + }