Skip to content

Commit 387c0e0

Browse files
committed
Feat: 게시글 삭제 API 구현
1 parent cecca43 commit 387c0e0

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/main/java/com/back/domain/board/controller/PostController.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,19 @@ public ResponseEntity<RsData<PostResponse>> updatePost(
8181
response
8282
));
8383
}
84+
85+
// 게시글 삭제
86+
@DeleteMapping("/{postId}")
87+
public ResponseEntity<RsData<Void>> deletePost(
88+
@PathVariable Long postId,
89+
@AuthenticationPrincipal CustomUserDetails user
90+
) {
91+
postService.deletePost(postId, user.getUserId());
92+
return ResponseEntity
93+
.status(HttpStatus.OK)
94+
.body(RsData.success(
95+
"게시글이 삭제되었습니다.",
96+
null
97+
));
98+
}
8499
}

src/main/java/com/back/domain/board/service/PostService.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,24 @@ public PostResponse updatePost(Long postId, PostRequest request, Long userId) {
111111
// 응답 반환
112112
return PostResponse.from(post);
113113
}
114+
115+
/**
116+
* 게시글 삭제 서비스
117+
* 1. Post 조회
118+
* 2. 작성자 검증
119+
* 3. Post 삭제
120+
*/
121+
public void deletePost(Long postId, Long userId) {
122+
// Post 조회
123+
Post post = postRepository.findById(postId)
124+
.orElseThrow(() -> new CustomException(ErrorCode.POST_NOT_FOUND));
125+
126+
// 작성자 검증
127+
if (!post.getUser().getId().equals(userId)) {
128+
throw new CustomException(ErrorCode.POST_NO_PERMISSION);
129+
}
130+
131+
// Post 삭제
132+
postRepository.delete(post);
133+
}
114134
}

0 commit comments

Comments
 (0)