Skip to content

Commit

Permalink
[NAYB-81] feat: 사용자와 연관된 데이터를 제거
Browse files Browse the repository at this point in the history
[NAYB-81] feat: 사용자와 연관된 데이터를 제거
  • Loading branch information
hseong3243 authored Sep 15, 2023
2 parents feb506e + 45eca4c commit 5783271
Show file tree
Hide file tree
Showing 19 changed files with 350 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

import com.prgrms.nabmart.domain.cart.Cart;
import com.prgrms.nabmart.domain.cart.CartItem;
import com.prgrms.nabmart.domain.user.User;
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;

public interface CartItemRepository extends JpaRepository<CartItem, Long> {

List<CartItem> findAllByCartItemIdOrderByCreatedAt(Long cartItemId);

List<CartItem> findAllByCartOrderByCreatedAt(Cart cart);

@Modifying
@Query("delete from CartItem ci"
+ " where ci.cart ="
+ " (select c from Cart c where c.user = :user)")
void deleteByUser(@Param("user") User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@
public interface CartRepository extends JpaRepository<Cart, Long> {

Optional<Cart> findByUser(final User user);

void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,7 @@ List<UserCoupon> findByUserAndIsUsedAndCouponEndAtAfter(
@Param("user") User user,
@Param("isUsed") boolean isUsed,
@Param("currentDate") LocalDate currentDate);

void deleteByUser(User findUser);
}

24 changes: 23 additions & 1 deletion src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ public class Delivery extends BaseTimeEntity {

private static final int ADDRESS_LENGTH = 500;
private static final int ZERO = 0;
public static final String DELETED = "삭제됨";

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long deliveryId;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "orderId", nullable = false)
@JoinColumn(name = "orderId")
private Order order;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -50,10 +51,26 @@ public class Delivery extends BaseTimeEntity {
@Version
private Long version;

@Column
private String address;

@Column
private Integer orderPrice;

@Column
private String riderRequest;

@Column
private Integer deliveryFee;

@Builder
public Delivery(final Order order) {
this.order = order;
this.deliveryStatus = DeliveryStatus.ACCEPTING_ORDER;
this.address = order.getAddress();
this.orderPrice = order.getPrice();
this.riderRequest = order.getRiderRequest();
this.deliveryFee = order.getDeliveryFee();
}

public boolean isOwnByUser(final User user) {
Expand Down Expand Up @@ -86,4 +103,9 @@ public void completeDelivery() {
this.arrivedAt = LocalDateTime.now();
this.deliveryStatus = DeliveryStatus.DELIVERED;
}

public void deleteAboutUser() {
this.order = null;
this.address = DELETED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.prgrms.nabmart.domain.delivery.Delivery;
import com.prgrms.nabmart.domain.delivery.DeliveryStatus;
import com.prgrms.nabmart.domain.delivery.Rider;
import com.prgrms.nabmart.domain.user.User;
import jakarta.persistence.LockModeType;
import java.util.List;
import java.util.Optional;
Expand All @@ -19,18 +20,11 @@ public interface DeliveryRepository extends JpaRepository<Delivery, Long> {
Optional<Delivery> findByOrderIdWithOrder(@Param("orderId") Long orderId);

@Query(value = "select d from Delivery d"
+ " join fetch d.order"
+ " where d.deliveryStatus"
+ " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER",
countQuery = "select d.deliveryId from Delivery d"
+ " where d.deliveryStatus"
+ " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER")
Page<Delivery> findWaitingDeliveries(Pageable pageable);

@Query(value = "select d from Delivery d"
+ " join fetch d.order"
+ " where d.rider = :rider and d.deliveryStatus in :statuses",
countQuery = "select d.deliveryId from Delivery d"
+ " where d.rider = :rider and d.deliveryStatus in :statuses")
Page<Delivery> findRiderDeliveries(
@Param("rider") Rider rider,
Expand All @@ -40,4 +34,7 @@ Page<Delivery> findRiderDeliveries(
@Lock(LockModeType.OPTIMISTIC)
@Query("select d from Delivery d where d.deliveryId = :deliveryId")
Optional<Delivery> findByIdOptimistic(@Param("deliveryId") Long deliveryId);

@Query("select d from Delivery d join d.order o where o.user = :user")
List<Delivery> findAllByUser(@Param("user") User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static FindDeliveryDetailResponse from(final Delivery delivery) {
delivery.getArrivedAt(),
delivery.getOrder().getOrderId(),
delivery.getOrder().getName(),
delivery.getOrder().getPrice(),
delivery.getOrder().getRiderRequest());
delivery.getOrderPrice(),
delivery.getRiderRequest());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public static FindRiderDeliveryResponse from(final Delivery delivery) {
delivery.getDeliveryStatus(),
delivery.getArrivedAt(),
delivery.getCreatedAt(),
delivery.getOrder().getAddress(),
delivery.getOrder().getPrice(),
delivery.getOrder().getRiderRequest(),
delivery.getOrder().getDeliveryFee());
delivery.getAddress(),
delivery.getOrderPrice(),
delivery.getRiderRequest(),
delivery.getDeliveryFee());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public static FindWaitingDeliveryResponse from(final Delivery delivery) {
delivery.getDeliveryId(),
delivery.getArrivedAt(),
delivery.getCreatedAt(),
delivery.getOrder().getAddress(),
delivery.getOrder().getDeliveryFee());
delivery.getAddress(),
delivery.getDeliveryFee());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface LikeItemRepository extends JpaRepository<LikeItem, Long> {

@Query("select li from LikeItem li join fetch li.item where li.user = :user")
Page<LikeItem> findByUserWithItem(@Param("user") User user, Pageable pageable);

void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.prgrms.nabmart.domain.order.Order;
import com.prgrms.nabmart.domain.order.OrderStatus;
import com.prgrms.nabmart.domain.user.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand All @@ -21,4 +22,6 @@ public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("SELECT o FROM Order o WHERE o.createdAt <= :expiredTime AND o.status IN :statusList")
List<Order> findByStatusInBeforeExpiredTime(LocalDateTime expiredTime,
List<OrderStatus> statusList);

void deleteByUser(User findUser);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.prgrms.nabmart.domain.payment.repository;

import com.prgrms.nabmart.domain.payment.Payment;
import com.prgrms.nabmart.domain.user.User;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PaymentRepository extends JpaRepository<Payment, Long> {

Optional<Payment> findByOrder_UuidAndUser_UserId(String uuid, Long userId);

void deleteByUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public interface ReviewRepository extends JpaRepository<Review, Long> {

Long countByItem_ItemId(Long itemId);

void deleteByUser(User findUser);

@Query("select avg(r.rate) from Review r where r.item.itemId = :itemId")
Double findAverageRatingByItemId(@Param("itemId") Long itemId);

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package com.prgrms.nabmart.domain.user.service;

import com.prgrms.nabmart.domain.cart.repository.CartItemRepository;
import com.prgrms.nabmart.domain.cart.repository.CartRepository;
import com.prgrms.nabmart.domain.coupon.repository.UserCouponRepository;
import com.prgrms.nabmart.domain.delivery.Delivery;
import com.prgrms.nabmart.domain.delivery.repository.DeliveryRepository;
import com.prgrms.nabmart.domain.item.repository.LikeItemRepository;
import com.prgrms.nabmart.domain.order.repository.OrderRepository;
import com.prgrms.nabmart.domain.payment.repository.PaymentRepository;
import com.prgrms.nabmart.domain.review.repository.ReviewRepository;
import com.prgrms.nabmart.domain.user.User;
import com.prgrms.nabmart.domain.user.exception.NotFoundUserException;
import com.prgrms.nabmart.domain.user.repository.UserRepository;
import com.prgrms.nabmart.domain.user.service.request.FindUserCommand;
import com.prgrms.nabmart.domain.user.service.request.RegisterUserCommand;
import com.prgrms.nabmart.domain.user.service.response.FindUserDetailResponse;
import com.prgrms.nabmart.domain.user.service.response.RegisterUserResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -16,6 +26,14 @@
public class UserService {

private final UserRepository userRepository;
private final ReviewRepository reviewRepository;
private final LikeItemRepository likeItemRepository;
private final CartRepository cartRepository;
private final CartItemRepository cartItemRepository;
private final UserCouponRepository userCouponRepository;
private final OrderRepository orderRepository;
private final DeliveryRepository deliveryRepository;
private final PaymentRepository paymentRepository;

@Transactional
public RegisterUserResponse getOrRegisterUser(final RegisterUserCommand registerUserCommand) {
Expand Down Expand Up @@ -47,7 +65,21 @@ public FindUserDetailResponse findUser(FindUserCommand findUserCommand) {

@Transactional
public void deleteUser(Long userId) {
userRepository.deleteById(userId);
User findUser = findUserByUserId(userId);
releaseDeliveryAboutUserInfo(findUser);
reviewRepository.deleteByUser(findUser);
likeItemRepository.deleteByUser(findUser);
cartItemRepository.deleteByUser(findUser);
cartRepository.deleteByUser(findUser);
paymentRepository.deleteByUser(findUser);
orderRepository.deleteByUser(findUser);
userCouponRepository.deleteByUser(findUser);
userRepository.delete(findUser);
}

private void releaseDeliveryAboutUserInfo(User findUser) {
List<Delivery> userDeliveries = deliveryRepository.findAllByUser(findUser);
userDeliveries.forEach(Delivery::deleteAboutUser);
}

private User findUserByUserId(Long userId) {
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/com/prgrms/nabmart/base/IntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ static void beforeAll() {
properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey");
properties.setProperty("REDIS_HOST", "localhost");
properties.setProperty("REDIS_PORT", "6379");
properties.setProperty("spring.data.redis.host", "localhost");
properties.setProperty("spring.data.redis.port", "6379");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,92 @@

import static org.assertj.core.api.Assertions.assertThat;

import com.prgrms.nabmart.base.IntegrationTest;
import com.prgrms.nabmart.domain.category.MainCategory;
import com.prgrms.nabmart.domain.category.SubCategory;
import com.prgrms.nabmart.domain.category.fixture.CategoryFixture;
import com.prgrms.nabmart.domain.category.repository.MainCategoryRepository;
import com.prgrms.nabmart.domain.category.repository.SubCategoryRepository;
import com.prgrms.nabmart.domain.delivery.repository.DeliveryRepository;
import com.prgrms.nabmart.domain.delivery.repository.RiderRepository;
import com.prgrms.nabmart.domain.delivery.service.DeliveryService;
import com.prgrms.nabmart.domain.delivery.service.request.AcceptDeliveryCommand;
import com.prgrms.nabmart.domain.item.Item;
import com.prgrms.nabmart.domain.item.repository.ItemRepository;
import com.prgrms.nabmart.domain.item.support.ItemFixture;
import com.prgrms.nabmart.domain.order.Order;
import com.prgrms.nabmart.domain.order.OrderItem;
import com.prgrms.nabmart.domain.order.repository.OrderItemRepository;
import com.prgrms.nabmart.domain.order.repository.OrderRepository;
import com.prgrms.nabmart.domain.user.User;
import com.prgrms.nabmart.domain.user.repository.UserRepository;
import com.prgrms.nabmart.domain.user.support.UserFixture;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.IntStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

public class DeliveryIntegrationTest extends IntegrationTest {
@SpringBootTest
public class DeliveryIntegrationTest {

@Autowired
UserRepository userRepository;

@Autowired
MainCategoryRepository mainCategoryRepository;

@Autowired
SubCategoryRepository subCategoryRepository;

@Autowired
ItemRepository itemRepository;

@Autowired
OrderItemRepository orderItemRepository;

@Autowired
OrderRepository orderRepository;

@Autowired
DeliveryRepository deliveryRepository;

@Autowired
DeliveryService deliveryService;

@Autowired
RiderRepository riderRepository;

@BeforeAll
static void beforeAll() {
Properties properties = System.getProperties();
properties.setProperty("ISSUER", "issuer");
properties.setProperty("CLIENT_SECRET", "clientSecret");
properties.setProperty("NAVER_CLIENT_ID", "naverClientId");
properties.setProperty("NAVER_CLIENT_SECRET", "naverClientSecret");
properties.setProperty("KAKAO_CLIENT_ID", "kakaoClientId");
properties.setProperty("KAKAO_CLIENT_SECRET", "kakaoClientSecret");
properties.setProperty("REDIRECT_URI",
"http://localhost:8080/login/oauth2/code/{registrationId}");
properties.setProperty("EXPIRY_SECONDS", "60");
properties.setProperty("TOSS_SUCCESS_URL", "tossSuccessUrl");
properties.setProperty("TOSS_FAIL_URL", "tossFailUrl");
properties.setProperty("TOSS_SECRET_KEY", "tossSecretKey");
properties.setProperty("REDIS_HOST", "localhost");
properties.setProperty("REDIS_PORT", "6379");
properties.setProperty("spring.data.redis.host", "localhost");
properties.setProperty("spring.data.redis.port", "6379");
}


@Nested
@DisplayName("acceptDelivery 메서드 실행 시")
class AcceptDeliveryTest {
Expand Down Expand Up @@ -59,7 +117,6 @@ void setUpData() {
orderRepository.save(order);
}


List<Rider> createAndSaveRiders(int end) {
List<Rider> riders = IntStream.range(0, end)
.mapToObj(i -> Rider.builder()
Expand All @@ -78,6 +135,16 @@ private Delivery createAndSaveDelivery() {
return delivery;
}

@AfterEach
void tearDown() {
deliveryRepository.deleteAll();
riderRepository.deleteAll();
orderRepository.deleteAll();
itemRepository.deleteAll();
subCategoryRepository.deleteAll();
mainCategoryRepository.deleteAll();
}

@Test
@DisplayName("성공: 여러 명의 라이더 중 한 명만 성공")
void success() throws InterruptedException {
Expand Down
Loading

0 comments on commit 5783271

Please sign in to comment.