Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] #69 게시물 삭제 API 추가 #70

Merged
merged 1 commit into from
Mar 22, 2024
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 @@ -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);
}
}
Loading