Skip to content

Commit

Permalink
feat: 게시물 삭제 기능
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeonkyujin committed Nov 21, 2024
1 parent 1c40372 commit f8b161a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public static <T> DataResponse<T> from(T data) {
public static <T> DataResponse<Void> ok() {
return new DataResponse<>(HttpStatus.OK, null);
}

public static <T> DataResponse<String> success(String message) {
return new DataResponse<>(HttpStatus.OK, message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
public class GlobalExceptionHandler {

@ExceptionHandler(ApiException.class)
public ResponseEntity<Object> handleApiException(ApiException e) {
public ResponseEntity<String> 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<Object> makeErrorResponseEntity(HttpStatus httpStatus, String message, String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,5 +68,12 @@ public ResponseEntity<Map<String, Object>> readByView(@RequestParam(defaultValue
return ResponseEntity.ok(response);
}

@DeleteMapping("/delete/{id}")
public ResponseEntity<DataResponse<String>> deletePostBySingle(@PathVariable Long id) {
postService.deletePostBySingle(id);

return ResponseEntity.ok(DataResponse.success("게시글이 성공적으로 삭제 되었습니다."));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,4 +65,12 @@ public Page<Post> 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);

}

}

0 comments on commit f8b161a

Please sign in to comment.