-
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 1 commit
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,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(); | ||
@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,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); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,7 +52,6 @@ public class Post extends TemporalRecord { | |
@JoinColumn(nullable = false) | ||
private Member member; | ||
|
||
// TODO: cascade | ||
private PostImageInfos postImageInfos; | ||
|
||
@ColumnDefault("0") | ||
|
@@ -125,6 +124,7 @@ private void validateMember(final Member member) { | |
} | ||
} | ||
|
||
// todo: 이 부분 docs test에서만 쓰고 있어요 | ||
public void addPostImageInfo(final PostImageInfo postImageInfo) { | ||
this.postImageInfos.add(postImageInfo); | ||
} | ||
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. 게시글 생성할때도, 수정할때도 새로 추가되는 이미지는 DB에만 잘 저장해두면되지 Post 객체에는 저장해줄 필요가 없네요. 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. 생성자 주입으로 넣어주도록 수정하고, 위 메서드는 삭제했습니다 !! 먼가 postInfo의 post가 db에서 참조로 저장되기 위해서만 존재하는 늬낌,,, |
||
|
@@ -156,18 +156,16 @@ private void updatePrice(final Long price) { | |
|
||
/** | ||
* 게시글 수정시 사용, 새로 추가되는 이미지가 없고 기존 이미지에 대한 수정만 일어나는 경우 | ||
* -> imageNamesToMaintain을 제외하고 삭제한다. | ||
*/ | ||
public void updateImages(final List<String> remainedImageNames) { | ||
postImageInfos.update(remainedImageNames, Collections.emptyList()); | ||
public List<Long> getImageIdsToDeleteBy(final List<String> remainedImageNames) { | ||
return this.postImageInfos.getImageIdsToDeleteBy(remainedImageNames, Collections.emptyList()); | ||
} | ||
|
||
/** | ||
* 게시글 수정시 사용, 새로 추가되는 이미지도 있는 경우 | ||
* -> imageNamesToMaintain을 제외하고 삭제 후, imagesToAdd를 추가한다. | ||
*/ | ||
public void updateImages(final List<String> remainedImageNames, final PostImageInfos imagesToAdd) { | ||
this.postImageInfos.update(remainedImageNames, imagesToAdd.getPostImageInfos()); | ||
public List<Long> getImageIdsToDeleteBy(final List<String> remainedImageNames, final PostImageInfos imagesToAdd) { | ||
return this.postImageInfos.getImageIdsToDeleteBy(remainedImageNames, imagesToAdd.getPostImageInfos()); | ||
} | ||
|
||
public boolean isSameMember(final Member member) { | ||
|
@@ -179,7 +177,7 @@ public boolean isSameMember(final Long memberId) { | |
} | ||
|
||
public Member getMember() { | ||
return member; | ||
return this.member; | ||
} | ||
|
||
public Long getWriterId() { | ||
|
@@ -212,8 +210,6 @@ public void updateView(final Member member) { | |
} | ||
|
||
public void delete() { | ||
//lazyLoading 문제로 repository를 통해 직접 postImageInfos를 제거해주는 것이 필요하다. | ||
this.postImageInfos.deleteAll(); | ||
this.deleted = true; | ||
} | ||
|
||
|
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.
제가 못지운 부분을 지워주셨군요!! 감사드립니당
CommentInfoRepository, ProfileImageInfoRepository에 있는 부분도 부탁드려도 될까요(굽신굽신)
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.
수정했슴돠 !!