Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,4 +70,17 @@ public RsData<PostResponseDto> updatePost(
) {
return RsData.successOf(postService.updatePost(postId, reqBody)); // code=200, message="success"
}

/**
* 게시글 삭제 API
* @param postId 삭제할 게시글 ID
* @return 삭제 성공 메시지
*/
@DeleteMapping("/{postId}")
public RsData<Void> deletePost(
@PathVariable Long postId
) {
postService.deletePost(postId);
return RsData.successOf(null); // code=200, message="success"
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/back/domain/post/post/service/PostService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> tagNames, Post post) {
for (String tagName : tagNames) {
Expand Down