Skip to content

Commit

Permalink
Merge pull request #98 from APPS-sookmyung/feature/#86-delete-post
Browse files Browse the repository at this point in the history
[feature/#86-delete-post] 게시글 삭제 API
  • Loading branch information
5jisoo authored Jan 21, 2024
2 parents ee00db9 + aaa712a commit 9fac320
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import com.apps.pochak.comment.domain.Comment;
import com.apps.pochak.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface CommentRepository extends JpaRepository<Comment, Long> {

@Modifying
@Query("update Comment c set c.status = 'DELETED' " +
"where c.post = :post ")
void bulkDeleteByPost(@Param("post") final Post post);

@Query("select c from Comment c " +
"join fetch c.member " +
"where c.post = :post " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ public enum ErrorStatus implements BaseErrorCode {
INVALID_MEMBER_HANDLE(BAD_REQUEST, "MEMBER4001", "유효하지 않은 멤버의 handle입니다."),

// Post
INVALID_POST_ID(BAD_REQUEST, "POST4001", "유효하지 않은 POST ID 입니다."),
PRIVATE_POST(BAD_REQUEST, "POST4002", "공개되지 않은 게시물입니다. 접근 권한이 없습니다."),
INVALID_POST_ID(BAD_REQUEST, "POST4001", "유효하지 않은 게시물 아이디입니다."),
NOT_YOUR_POST(UNAUTHORIZED, "POST4002", "해당 게시물의 삭제 권한이 없습니다."),
PRIVATE_POST(UNAUTHORIZED, "POST4003", "공개되지 않은 게시물입니다. 접근 권한이 없습니다."),

// Tag

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import com.apps.pochak.post.service.PostService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -21,6 +25,12 @@ public class PostController {
private final PostService postService;
private final LikeService likeService;

@DeleteMapping("/{postId}")
public ApiResponse<Void> deletePost(@PathVariable final Long postId) {
postService.deletePost(postId);
return ApiResponse.onSuccess(null);
}

@GetMapping("/{postId}")
public ApiResponse<PostDetailResponse> getPostDetail(
@PathVariable("postId") final Long postId
Expand All @@ -36,6 +46,5 @@ public ApiResponse<Void> uploadPost(
) {
postService.savePost(postImage, request);
return ApiResponse.onSuccess(null);

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.apps.pochak.post.service;


import com.apps.pochak.comment.domain.repository.CommentRepository;
import com.apps.pochak.global.apiPayload.exception.GeneralException;
import com.apps.pochak.login.jwt.JwtService;
import com.apps.pochak.member.domain.Member;
import com.apps.pochak.post.domain.Post;
import com.apps.pochak.post.domain.repository.PostRepository;
import com.apps.pochak.alarm.domain.TagApprovalAlarm;
import com.apps.pochak.alarm.domain.repository.AlarmRepository;
import com.apps.pochak.comment.domain.Comment;
Expand Down Expand Up @@ -29,6 +34,9 @@
import static com.apps.pochak.global.apiPayload.code.status.ErrorStatus.PRIVATE_POST;
import static com.apps.pochak.global.s3.DirName.POST;

import static com.apps.pochak.global.apiPayload.code.status.ErrorStatus.INVALID_POST_ID;
import static com.apps.pochak.global.apiPayload.code.status.ErrorStatus.NOT_YOUR_POST;

@Service
@RequiredArgsConstructor
public class PostService {
Expand All @@ -43,6 +51,17 @@ public class PostService {
private final S3Service s3Service;
private final JwtService jwtService;

@Transactional
public void deletePost(final Long postId) {
final Member loginMember = jwtService.getLoginMember();
final Post post = postRepository.findById(postId).orElseThrow(() -> new GeneralException(INVALID_POST_ID));
if (!post.getOwner().getId().equals(loginMember.getId())) {
throw new GeneralException(NOT_YOUR_POST);
}
postRepository.delete(post);
commentRepository.bulkDeleteByPost(post);
}

public PostDetailResponse getPostDetail(final Long postId) {
final Member loginMember = jwtService.getLoginMember();
final Post post = postRepository.findPostById(postId);
Expand Down

0 comments on commit 9fac320

Please sign in to comment.