Skip to content

Commit

Permalink
[feat] #69 Post 삭제 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
OJOJIN committed Mar 22, 2024
1 parent ba98181 commit 1f93a37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import org.gachon.checkmate.domain.post.dto.request.PostCreateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.request.PostStateUpdateRequestDto;
import org.gachon.checkmate.domain.post.dto.response.PostDetailResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostSearchResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostStateUpdateResponseDto;
import org.gachon.checkmate.domain.post.dto.response.PostUpdateResponseDto;
import org.gachon.checkmate.domain.post.dto.response.*;
import org.gachon.checkmate.domain.post.service.PostService;
import org.gachon.checkmate.global.common.SuccessResponse;
import org.gachon.checkmate.global.config.auth.UserId;
Expand Down Expand Up @@ -55,6 +52,13 @@ public ResponseEntity<SuccessResponse<?>> getMyPosts(@UserId final Long userId,
return SuccessResponse.ok(responseDto);
}

@DeleteMapping("{id}")
public ResponseEntity<SuccessResponse<?>> deleteMyPost(@UserId final Long userId,
@PathVariable("id") final Long postId) {
PostDeleteResponseDto responseDto = postService.deleteMyPost(userId, postId);
return SuccessResponse.ok(responseDto);
}

@PatchMapping("/{id}")
public ResponseEntity<SuccessResponse<?>> updateMyPost(@UserId final Long userId,
@PathVariable("id") final Long postId,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.gachon.checkmate.domain.post.dto.response;

import lombok.Builder;

@Builder
public record PostDeleteResponseDto(
Boolean isSuccess
) {
public static PostDeleteResponseDto of(Boolean isSuccess) {
return PostDeleteResponseDto.builder()
.isSuccess(isSuccess)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ public PostStateUpdateResponseDto updateMyPostState(Long userId, Long postId, Po
return PostStateUpdateResponseDto.of(post);
}

public PostDeleteResponseDto deleteMyPost(Long userId, Long postId) {
User user = getUserOrThrow(userId);
Post post = getPostOrThrow(postId);
validatePostWriter(user, post);
deletePost(post);
return PostDeleteResponseDto.of(true);
}

private List<PostSearchElementResponseDto> createPostSearchResponseDto(Page<PostSearchDto> postSearchDtoList, CheckList checkList) {
return postSearchDtoList.stream()
.map(postSearchDto ->
Expand Down Expand Up @@ -182,4 +190,8 @@ private Post getPostOrThrow(Long postId) {
return postRepository.findById(postId)
.orElseThrow(() -> new EntityNotFoundException(POST_NOT_FOUND));
}

private void deletePost(Post post) {
postRepository.delete(post);
}
}

0 comments on commit 1f93a37

Please sign in to comment.