-
Notifications
You must be signed in to change notification settings - Fork 0
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
refactor/#415 글 삭제, 수정시 게시글 이미지의 soft delete를 위해 update문이 중복해서 나지 않도록 수정함 #526
The head ref may contain hidden characters: "refactor/#415_\uAC8C\uC2DC\uAE00_\uC0AD\uC81C_\uC218\uC815\uC2DC_\uAC8C\uC2DC\uAE00_\uC774\uBBF8\uC9C0_\uC0AD\uC81C_\uCFFC\uB9AC_\uAC1C\uC120"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
package edonymyeon.backend.image.commentimage.repository; | ||
|
||
import edonymyeon.backend.image.commentimage.domain.CommentImageInfo; | ||
import java.util.List; | ||
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.List; | ||
|
||
public interface CommentImageInfoRepository extends JpaRepository<CommentImageInfo, Long> { | ||
|
||
@Modifying //todo: 옵션.. 이대로 괜찮은가? | ||
@Query("update CommentImageInfo c set c.deleted = true where c.id in :ids") | ||
void deleteAllById(@Param("ids") List<Long> ids); | ||
|
||
@Query(value = "select * from comment_image_info", nativeQuery = true) | ||
List<CommentImageInfo> findAllImages(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
package edonymyeon.backend.image.postimage.repository; | ||
|
||
import edonymyeon.backend.image.postimage.domain.PostImageInfo; | ||
import java.util.List; | ||
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.List; | ||
|
||
public interface PostImageInfoRepository extends JpaRepository<PostImageInfo, Long> { | ||
|
||
List<PostImageInfo> findAllByPostId(final Long postId); | ||
|
||
@Modifying | ||
@Query("delete from PostImageInfo pi where pi.post.id=:postId") | ||
@Query("update PostImageInfo pi set pi.deleted = true where pi.post.id=:postId") | ||
void deleteAllByPostId(@Param("postId") final Long postId); | ||
|
||
@Query(value = "select * from post_image_info", nativeQuery = true) | ||
List<PostImageInfo> findAllImages(); | ||
Comment on lines
-18
to
-19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 제가 못지운 부분을 지워주셨군요!! 감사드립니당 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정했슴돠 !! |
||
@Modifying | ||
@Query("update PostImageInfo pi set pi.deleted = true where pi.id in (:imageIds)") | ||
void deleteAllByIds(@Param("imageIds") final List<Long> imageIds); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
package edonymyeon.backend.image.profileimage.repository; | ||
|
||
import edonymyeon.backend.image.profileimage.domain.ProfileImageInfo; | ||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
public interface ProfileImageInfoRepository extends JpaRepository<ProfileImageInfo, Long> { | ||
|
||
@Query(value = "select * from profile_image_info", nativeQuery = true) | ||
List<ProfileImageInfo> findAllImages(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
package edonymyeon.backend.post.application; | ||
|
||
import static edonymyeon.backend.global.exception.ExceptionInformation.MEMBER_ID_NOT_FOUND; | ||
import static edonymyeon.backend.global.exception.ExceptionInformation.POST_ID_NOT_FOUND; | ||
import static edonymyeon.backend.global.exception.ExceptionInformation.POST_MEMBER_NOT_SAME; | ||
|
||
import edonymyeon.backend.global.exception.EdonymyeonException; | ||
import edonymyeon.backend.image.application.ImageService; | ||
import edonymyeon.backend.image.domain.ImageType; | ||
|
@@ -18,14 +14,17 @@ | |
import edonymyeon.backend.post.application.event.PostDeletionEvent; | ||
import edonymyeon.backend.post.domain.Post; | ||
import edonymyeon.backend.post.repository.PostRepository; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static edonymyeon.backend.global.exception.ExceptionInformation.*; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class PostService { | ||
|
@@ -90,13 +89,12 @@ public void deletePost(final MemberId memberId, final Long postId) { | |
final Post post = findPostById(postId); | ||
checkWriter(member, post); | ||
|
||
// soft delete 시킬 때, 실제 이미지는 보관된다. | ||
// todo: 이미지 삭제.. 한번에.. | ||
// todo: 소비내역 삭제할 때, 이벤트 대신 인터페이스로 변경 | ||
applicationEventPublisher.publishEvent(new PostDeletionEvent(post.getId())); | ||
thumbsService.deleteAllThumbsInPost(postId); | ||
commentService.deleteAllCommentsInPost(postId); | ||
post.delete(); | ||
postImageInfoRepository.deleteAllByPostId(postId); | ||
} | ||
|
||
private Post findPostById(final Long postId) { | ||
|
@@ -126,13 +124,18 @@ public PostIdResponse updatePost( | |
final List<String> remainedImageNames = imageService.convertToStoreName(request.originalImages(), ImageType.POST); | ||
|
||
if(isImagesEmpty(imageFilesToAdd)) { | ||
post.updateImages(remainedImageNames); | ||
final List<Long> imageIdsToDelete = post.getImageIdsToDeleteBy(remainedImageNames); | ||
postImageInfoRepository.deleteAllByIds(imageIdsToDelete); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분 분기처리 안해도 될 거 같은데 어떻게 생각하나여? 케로상 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 저도 빼보려고 지워보긴 했는데,,, |
||
return new PostIdResponse(postId); | ||
} | ||
|
||
final PostImageInfos imagesToAdd = PostImageInfos.of(post, imageService.saveAll(imageFilesToAdd, ImageType.POST)); | ||
post.updateImages(remainedImageNames, imagesToAdd); //이때 기존 이미지중 삭제되는 것들은 softDelete | ||
|
||
final List<Long> imageIdsToDelete = post.getImageIdsToDeleteBy(remainedImageNames, imagesToAdd); | ||
postImageInfoRepository.deleteAllByIds(imageIdsToDelete); //이때 기존 이미지중 삭제되는 것들은 softDelete | ||
|
||
postImageInfoRepository.saveAll(imagesToAdd.getPostImageInfos()); // //새로 추가된 이미지들을 DB에 저장 | ||
|
||
return new PostIdResponse(postId); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이전 기수 선배님께서 같은 고민을 하고 남기신 글이 있어 공유드려요!
Modifying을 이용한 작업을 하면 1차 캐시와 데이터베이스 사이의 정보 불일치가 발생하기 때문에,
해당 메서드를 호출한 다음에는 1차 캐시를 비워주는 작업을 반드시 해야 한다고 합니다 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이 부분은 저도 고민을 했었는데여!! 👍🏻
이 분도 이렇게 말씀하셧네용
현재 게시글의 수정과 삭제 이후에 같은 트랜잭션에서 게시글의 상세정보, 이미지나 댓글의 상세정보를 사용하고 있지 않아여!
return이 Void거나 id 정도만 사용되고 있습니당!! 따라서 1차 캐시를 비워주는 작업을 추가하지 않았습니당~ 😘
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
합리적이네요!