diff --git a/src/main/java/com/back/domain/post/post/controller/PostController.java b/src/main/java/com/back/domain/post/post/controller/PostController.java index 270f5066..a7a44479 100644 --- a/src/main/java/com/back/domain/post/post/controller/PostController.java +++ b/src/main/java/com/back/domain/post/post/controller/PostController.java @@ -8,6 +8,7 @@ import jakarta.validation.Valid; import java.util.List; import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -69,4 +70,17 @@ public RsData updatePost( ) { return RsData.successOf(postService.updatePost(postId, reqBody)); // code=200, message="success" } + + /** + * 게시글 삭제 API + * @param postId 삭제할 게시글 ID + * @return 삭제 성공 메시지 + */ + @DeleteMapping("/{postId}") + public RsData deletePost( + @PathVariable Long postId + ) { + postService.deletePost(postId); + return RsData.successOf(null); // code=200, message="success" + } } diff --git a/src/main/java/com/back/domain/post/post/service/PostService.java b/src/main/java/com/back/domain/post/post/service/PostService.java index 6eedac05..7ec975d0 100644 --- a/src/main/java/com/back/domain/post/post/service/PostService.java +++ b/src/main/java/com/back/domain/post/post/service/PostService.java @@ -7,6 +7,7 @@ import com.back.domain.post.post.dto.response.PostResponseDto; import com.back.domain.post.post.entity.Post; import com.back.domain.post.post.entity.Tag; +import com.back.domain.post.post.enums.PostStatus; import com.back.domain.post.post.repository.PostRepository; import com.back.domain.post.post.repository.TagRepository; import com.back.domain.user.entity.User; @@ -107,6 +108,18 @@ public PostResponseDto updatePost(Long postId, PostUpdateRequestDto reqBody) { return new PostResponseDto(post); } + // 게시글 삭제 로직 + @Transactional + public void deletePost(Long postId) { + Post post = postRepository.findById(postId) + .orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId)); + + post.updateStatus(PostStatus.DELETED); + + // soft delete를 사용하기 위해 레포지토리 삭제 작업은 진행하지 않음. +// postRepository.delete(post); + } + // 태그 추가 메서드 private void addTag(List tagNames, Post post) { for (String tagName : tagNames) {