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

#41 [feat] 글 삭제 구현 #42

Merged
merged 2 commits into from
Jan 10, 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 @@ -9,6 +9,7 @@
import com.mile.post.service.dto.PostPutRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -23,6 +24,7 @@
@RestController
@RequestMapping("/api/post")
@RequiredArgsConstructor
@Slf4j
public class PostController implements PostControllerSwagger {

private final PostService postService;
Expand Down Expand Up @@ -101,4 +103,14 @@ public SuccessResponse putPost(
postService.updatePost(postId, Long.valueOf(principal.getName()), putRequest);
return SuccessResponse.of(SuccessMessage.POST_PUT_SUCCESS);
}

@DeleteMapping("/{postId}")
@Override
public SuccessResponse deletePost(
@PathVariable final Long postId,
final Principal principal
) {
postService.deletePost(postId, Long.valueOf(principal.getName()));
return SuccessResponse.of(SuccessMessage.POST_DELETE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,21 @@ SuccessResponse putPost(
@RequestBody final PostPutRequest putRequest,
final Principal principal
);

@Operation(summary = "글 삭제")
@ApiResponses(
value = {
@ApiResponse(responseCode = "200", description = "글 삭제가 완료되었습니다."),
@ApiResponse(responseCode = "404", description = "해당 글은 존재하지 않습니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "403", description = "해당 사용자는 글 수정/삭제 권한이 없습니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "서버 내부 오류입니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
}
)
SuccessResponse deletePost(
@PathVariable final Long postId,
final Principal principal
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum SuccessMessage {
CURIOUS_DELETE_SUCCESS(HttpStatus.OK.value(), "궁금해요 삭제가 완료되었습니다."),
WRITER_AUTHENTIACTE_SUCCESS(HttpStatus.OK.value(), "게시글 권한이 확인되었습니다."),
POST_PUT_SUCCESS(HttpStatus.OK.value(), "글 수정이 완료되었습니다."),
POST_DELETE_SUCCESS(HttpStatus.OK.value(), "글 삭제가 완료되었습니다."),
/*
201 CREATED
*/
Expand Down
1 change: 1 addition & 0 deletions module-domain/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ jar { enabled = true }
dependencies {
implementation project(':module-auth')
implementation project(':module-common')
implementation project(':module-external')

//Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.1.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mile.comment.repository;

import com.mile.comment.domain.Comment;
import com.mile.post.domain.Post;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -13,4 +14,6 @@ public interface CommentRepository extends JpaRepository<Comment, Long> {
Long findUserIdByComment(@Param(value = "comment") final Comment comment);

List<Comment> findByPostId(final Long postId);

void deleteAllByPost(final Post post);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ private boolean isCommentListNull(
) {
return commentList.isEmpty();
}

public void deleteAllByPost(
final Post post
) {
commentRepository.deleteAllByPost(post);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface CuriousRepository extends JpaRepository<Curious, Long> {
boolean existsByPostAndUser(Post post, User user);
Curious findByPostAndUser(Post post, User user);

void deleteAllByPost(final Post post);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.mile.curious.serivce;

import com.mile.curious.domain.Curious;
import com.mile.curious.repository.CuriousRepository;
import com.mile.curious.serivce.dto.CuriousInfoResponse;
import com.mile.exception.message.ErrorMessage;
import com.mile.exception.model.NotFoundException;
import com.mile.curious.domain.Curious;
import com.mile.exception.model.ConflictException;
import com.mile.exception.model.NotFoundException;
import com.mile.post.domain.Post;
import com.mile.user.domain.User;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -44,4 +45,10 @@ public void checkCuriousExists(final Post post, final User user) {
public CuriousInfoResponse getCuriousInfoResponse(final Post post, final User user) {
return CuriousInfoResponse.of(curiousRepository.existsByPostAndUser(post, user), post.getCuriousCount());
}

public void deleteAllByPost(
final Post post
) {
curiousRepository.deleteAllByPost(post);
}
}
2 changes: 1 addition & 1 deletion module-domain/src/main/java/com/mile/post/domain/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class Post extends BaseTimeEntity {
private String imageUrl;
@ManyToOne
private WriterName writerName;

private int curiousCount;
private boolean containPhoto;
private boolean anonymous;
private boolean isTemporary;

Expand Down
34 changes: 34 additions & 0 deletions module-domain/src/main/java/com/mile/post/service/PostService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mile.post.service;

import com.mile.aws.utils.S3Service;
import com.mile.comment.service.CommentService;
import com.mile.curious.serivce.CuriousService;
import com.mile.curious.serivce.dto.CuriousInfoResponse;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class PostService {
private final CuriousService curiousService;
private final UserService userService;
private final TopicService topicService;
private final S3Service s3Service;

@Transactional
public void createCommentOnPost(
Expand Down Expand Up @@ -119,4 +121,36 @@ public WriterAuthenticateResponse getAuthenticateWriter(
) {
return WriterAuthenticateResponse.of(postAuthenticateService.authenticateWriterWithPost(postId, userId));
}

@Transactional
public void deletePost(
final Long postId,
final Long userId
) {
postAuthenticateService.authenticateWriterWithPost(postId, userId);
delete(postId);
}

private void delete(
final Long postId
) {
Post post = findById(postId);
deleteRelatedData(post);
postRepository.delete(post);
}

private void deleteRelatedData(
final Post post
) {
if(post.isContainPhoto()) {
deleteS3File(post.getImageUrl());
}
curiousService.deleteAllByPost(post);
commentService.deleteAllByPost(post);
}
private void deleteS3File(
final String key
) {
s3Service.deleteImage(key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public PreSignedUrlResponse getUploadPreSignedUrl(final S3BucketDirectory prefix
}

// S3 버킷에 업로드된 이미지 삭제
public void deleteImage(final String key) throws IOException {
public void deleteImage(final String key) {
try {
final S3Client s3Client = awsConfig.getS3Client();

Expand Down