Skip to content

Commit 80ad213

Browse files
authored
[feat] 게시글 삭제 구현#46
[feat] 게시글 삭제 구현#46
2 parents 275ac49 + 2ef920e commit 80ad213

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import jakarta.validation.Valid;
99
import java.util.List;
1010
import lombok.RequiredArgsConstructor;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
1112
import org.springframework.web.bind.annotation.GetMapping;
1213
import org.springframework.web.bind.annotation.PatchMapping;
1314
import org.springframework.web.bind.annotation.PathVariable;
@@ -69,4 +70,17 @@ public RsData<PostResponseDto> updatePost(
6970
) {
7071
return RsData.successOf(postService.updatePost(postId, reqBody)); // code=200, message="success"
7172
}
73+
74+
/**
75+
* 게시글 삭제 API
76+
* @param postId 삭제할 게시글 ID
77+
* @return 삭제 성공 메시지
78+
*/
79+
@DeleteMapping("/{postId}")
80+
public RsData<Void> deletePost(
81+
@PathVariable Long postId
82+
) {
83+
postService.deletePost(postId);
84+
return RsData.successOf(null); // code=200, message="success"
85+
}
7286
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.back.domain.post.post.dto.response.PostResponseDto;
88
import com.back.domain.post.post.entity.Post;
99
import com.back.domain.post.post.entity.Tag;
10+
import com.back.domain.post.post.enums.PostStatus;
1011
import com.back.domain.post.post.repository.PostRepository;
1112
import com.back.domain.post.post.repository.TagRepository;
1213
import com.back.domain.user.entity.User;
@@ -107,6 +108,18 @@ public PostResponseDto updatePost(Long postId, PostUpdateRequestDto reqBody) {
107108
return new PostResponseDto(post);
108109
}
109110

111+
// 게시글 삭제 로직
112+
@Transactional
113+
public void deletePost(Long postId) {
114+
Post post = postRepository.findById(postId)
115+
.orElseThrow(() -> new NoSuchElementException("해당 게시글을 찾을 수 없습니다. ID: " + postId));
116+
117+
post.updateStatus(PostStatus.DELETED);
118+
119+
// soft delete를 사용하기 위해 레포지토리 삭제 작업은 진행하지 않음.
120+
// postRepository.delete(post);
121+
}
122+
110123
// 태그 추가 메서드
111124
private void addTag(List<String> tagNames, Post post) {
112125
for (String tagName : tagNames) {

0 commit comments

Comments
 (0)