Skip to content

Commit

Permalink
Merge pull request #23 from kagong-sillok/refactor/jpa-soft-delete
Browse files Browse the repository at this point in the history
refactor: soft delete 관련 로직 jpa 어노테이션 기반으로 변경
  • Loading branch information
yxxnghwan authored Sep 28, 2023
2 parents 77de8e6 + 1b8db6e commit 5f11b05
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.prography.kagongsillok.image.application;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -43,11 +42,11 @@ public List<ImageDto> getImages(final List<Long> imageIds) {

private List<Image> toImages(List<ImageCreateCommand> commands) {
return commands.stream()
.map(command -> command.toEntity())
.map(ImageCreateCommand::toEntity)
.collect(Collectors.toList());
}

private List<Image> saveImages(List<Image> images) {
private List<Image> saveImages(List<Image> images) { // todo: 개선포인트 bulkInsert 필요
return images.stream()
.map(imageRepository::save)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;

@Getter
@Entity
@Table(name = "image")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update image set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Image extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ public MemberDto getMember(final LoginMemberDto loginMemberDto) {
final Long memberId = loginMemberDto.getMemberId();
final Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundMemberException(memberId));
if (member.getIsDeleted()) {
throw new NotFoundMemberException(memberId);
}

return MemberDto.from(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;

@Getter
@Entity
@Table(name = "member")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update member set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Member extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ public PlaceDto createPlace(final PlaceCreateCommand placeCreateCommand) {
public PlaceDto getPlace(final Long id) {
final Place place = placeRepository.findById(id)
.orElseThrow(() -> new NotFoundPlaceException(id));
if (place.getIsDeleted()) {
throw new NotFoundPlaceException(id);
}

return PlaceDto.of(place, getImages(place));
}
Expand All @@ -59,9 +56,7 @@ public PlaceDto getPlace(final Long id) {
public PlaceDto getPlaceWithTags(final Long placeId) {
final Place place = placeRepository.findById(placeId)
.orElseThrow(() -> new NotFoundPlaceException(placeId));
if (place.getIsDeleted()) {
throw new NotFoundPlaceException(placeId);
}

final List<Review> reviews = reviewRepository.findAllByPlaceId(placeId);

return PlaceDto.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;
import org.prography.kagongsillok.common.utils.CustomListUtils;
import org.prography.kagongsillok.common.utils.CustomStringUtils;

@Getter
@Entity
@Table(name = "place")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update place set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Place extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;
import org.prography.kagongsillok.common.utils.CustomListUtils;
import org.prography.kagongsillok.common.utils.CustomStringUtils;
Expand All @@ -23,6 +25,8 @@
@Getter
@Entity
@Table(name = "study_record")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update study_record set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class StudyRecord extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ public ReviewDto getReview(final Long id) {
final Long memberId = review.getMemberId();
final Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundMemberException(memberId));
if (review.getIsDeleted()) {
throw new NotFoundReviewException(id);
}

final List<Image> images = imageRepository.findByIdIn(review.getImageIds());

Expand Down Expand Up @@ -183,7 +180,7 @@ private List<Image> getMappedImage(final Review review, final Map<Long, Image> i
return review
.getImageIds()
.stream()
.filter(imageId -> images.containsKey(imageId))
.filter(images::containsKey)
.map(images::get)
.collect(Collectors.toList());
}
Expand All @@ -204,7 +201,7 @@ private List<ReviewImageDto> getReviewImageDtos(final Review review, final Map<L

private List<Long> getReviewedMemberIds(final List<Review> reviews) {
return reviews.stream()
.map(review -> review.getMemberId())
.map(Review::getMemberId)
.collect(Collectors.toList());
}

Expand All @@ -227,7 +224,7 @@ private void checkExistImage(final List<Long> imageIds) {

private List<Image> getMappedImages(final List<Long> imageIds, final Map<Long, Image> imageMap) {
return imageIds.stream()
.map(imageId -> imageMap.get(imageId))
.map(imageMap::get)
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;
import org.prography.kagongsillok.common.utils.CustomListUtils;
import org.prography.kagongsillok.common.utils.CustomStringUtils;
Expand All @@ -22,6 +24,8 @@
@Getter
@Entity
@Table(name = "review")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update review set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Review extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;
import org.prography.kagongsillok.common.entity.AbstractRootEntity;

@Getter
@Entity
@Table(name = "review_tag")
@Where(clause = "is_deleted = false")
@SQLDelete(sql = "update review_tag set is_deleted = true, updated_at = now() where id = ?")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ReviewTag extends AbstractRootEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.time.LocalTime;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.Test;
import org.prography.kagongsillok.image.domain.Image;
import org.prography.kagongsillok.image.domain.ImageRepository;
Expand Down Expand Up @@ -34,6 +35,9 @@ class PlaceServiceTest {
@Autowired
private ImageRepository imageRepository;

@Autowired
private EntityManager em;

private final List<LinkCreateCommand> linkCreateCommands = List.of(
new LinkCreateCommand(LinkType.INSTAGRAM.name(), "testInstagramUrl"),
new LinkCreateCommand(LinkType.BLOG.name(), "testBlogUrl"),
Expand Down Expand Up @@ -251,6 +255,8 @@ class PlaceServiceTest {
final Long placeId = placeService.createPlace(placeCreateCommand).getId();

placeService.deletePlace(placeId);
em.flush();
em.clear();

assertThatThrownBy(() -> placeService.getPlace(placeId))
.isInstanceOf(NotFoundPlaceException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.time.LocalTime;
import java.util.List;
import javax.persistence.EntityManager;
import org.junit.jupiter.api.Test;
import org.prography.kagongsillok.image.domain.Image;
import org.prography.kagongsillok.image.domain.ImageRepository;
Expand Down Expand Up @@ -48,6 +49,9 @@ public class ReviewServiceTest {
@Autowired
private PlaceRepository placeRepository;

@Autowired
private EntityManager em;

@Test
void 리뷰를_생성한다() {
final Long memberId = saveMemberAndGetMemberId();
Expand Down Expand Up @@ -147,6 +151,8 @@ public class ReviewServiceTest {
.build();
final Long createdReviewId = reviewService.createReview(reviewCreateCommand).getId();
reviewService.deleteReview(createdReviewId);
em.flush();
em.clear();

assertThatThrownBy(() -> reviewService.getReview(createdReviewId))
.isInstanceOf(NotFoundReviewException.class)
Expand Down

0 comments on commit 5f11b05

Please sign in to comment.