diff --git a/unibond/src/main/java/com/unibond/unibond/common/BaseResponseStatus.java b/unibond/src/main/java/com/unibond/unibond/common/BaseResponseStatus.java index b748e37..4708eba 100644 --- a/unibond/src/main/java/com/unibond/unibond/common/BaseResponseStatus.java +++ b/unibond/src/main/java/com/unibond/unibond/common/BaseResponseStatus.java @@ -87,6 +87,8 @@ public enum BaseResponseStatus { // post (3600 ~ 3699) INVALID_POST_STATUS(false, 3600, "접근 불가능한 게시물 입니다."), BLOCKED_POST(false, 3601, "차단한 게시글입니다."), + DELETED_POST(false, 3602, "삭제된 게시글입니다."), + NOT_YOUR_POST(false, 3603, "게시글 수정 및 삭제 권한이 없습니다."), /** * 4000: DB Error diff --git a/unibond/src/main/java/com/unibond/unibond/post/controller/PostController.java b/unibond/src/main/java/com/unibond/unibond/post/controller/PostController.java index 2622eef..73869c9 100644 --- a/unibond/src/main/java/com/unibond/unibond/post/controller/PostController.java +++ b/unibond/src/main/java/com/unibond/unibond/post/controller/PostController.java @@ -26,4 +26,14 @@ public BaseResponse getDetailCommunityPosts(@RequestHeader("Authorization") L return new BaseResponse<>(e.getStatus()); } } + + @DeleteMapping("/{postId}") + public BaseResponse deleteCommunityPosts(@RequestHeader("Authorization") Long loginId, + @PathVariable("postId") Long postId) { + try { + return new BaseResponse<>(postService.deletePost(postId)); + } catch (BaseException e) { + return new BaseResponse<>(e.getStatus()); + } + } } diff --git a/unibond/src/main/java/com/unibond/unibond/post/service/PostService.java b/unibond/src/main/java/com/unibond/unibond/post/service/PostService.java index 62075b4..3d0525d 100644 --- a/unibond/src/main/java/com/unibond/unibond/post/service/PostService.java +++ b/unibond/src/main/java/com/unibond/unibond/post/service/PostService.java @@ -5,6 +5,7 @@ import com.unibond.unibond.comment.domain.Comment; import com.unibond.unibond.comment.repository.CommentRepository; import com.unibond.unibond.common.BaseException; +import com.unibond.unibond.common.BaseResponseStatus; import com.unibond.unibond.common.service.LoginInfoService; import com.unibond.unibond.common.service.S3Uploader; import com.unibond.unibond.member.domain.Member; @@ -21,6 +22,7 @@ import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; +import static com.unibond.unibond.common.BaseEntityStatus.DELETED; import static com.unibond.unibond.common.BaseResponseStatus.*; import static com.unibond.unibond.post.domain.BoardType.EXPERIENCE; @@ -102,6 +104,24 @@ public GetCommunityContentDetailResDto getDetailCommunityContent(Long postId, Pa } } + @Transactional + public BaseResponseStatus deletePost(Long postId) throws BaseException { + try { + Long loginMemberId = loginInfoService.getLoginMemberId(); + Post post = findPost(postId); + if (!post.getOwner().getId().equals(loginMemberId)) { + throw new BaseException(NOT_YOUR_POST); + } + post.setStatus(DELETED); + deleteComments(post); + return SUCCESS; + } catch (BaseException e) { + throw e; + } catch (Exception e) { + throw new BaseException(DATABASE_ERROR); + } + } + private void checkBlockedMember(Long reporterId, Long respondentId) throws BaseException { Boolean isBlocked = memberBlockRepository.existsByReporterIdAndRespondentId(reporterId, respondentId); if (isBlocked) { @@ -115,4 +135,16 @@ private void checkBlockedPost(Long reporterId, Long reportedPostId) throws BaseE throw new BaseException(BLOCKED_POST); } } + + private Post findPost(Long postId) throws BaseException { + Post post = postRepository.findById(postId).orElseThrow(() -> new BaseException(INVALID_POST_ID)); + if (post.getStatus().equals(DELETED)) { + throw new BaseException(DELETED_POST); + } + return post; + } + + private void deleteComments(Post post) throws BaseException { + commentRepository.bulkDeleteByPost(post); + } }