Skip to content

Commit

Permalink
Merge pull request #66 from UniBond-jijijin/feature/4-delete-post
Browse files Browse the repository at this point in the history
[feature/4-delete-post] 게시글 삭제 API
  • Loading branch information
5jisoo authored Jan 9, 2024
2 parents 4562de8 + 2ab1cb2 commit 6eac39b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ Integer getCommentCountByPost(@Param("post") Post post,
@Query("update Comment c set c.status = 'DELETED' " +
"where c.member.id = :memberId ")
void bulkDeleteByMember(@Param("memberId") Long memberId);

@Modifying
@Query("update Comment c set c.status = 'DELETED' " +
"where c.post = :post ")
void bulkDeleteByPost(@Param("post") Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

0 comments on commit 6eac39b

Please sign in to comment.