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

refactor: soft delete 관련 로직 jpa 어노테이션 기반으로 변경 #23

Merged
merged 1 commit into from
Sep 28, 2023
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
@@ -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
Loading