diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java index 365a12c7..5e86c14e 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/Delivery.java @@ -1,5 +1,6 @@ package com.prgrms.nabmart.domain.delivery; +import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.UnauthorizedDeliveryException; import com.prgrms.nabmart.domain.order.Order; @@ -7,6 +8,8 @@ import com.prgrms.nabmart.global.BaseTimeEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; @@ -16,6 +19,7 @@ import jakarta.persistence.OneToOne; import jakarta.persistence.Version; import java.time.LocalDateTime; +import java.util.Objects; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -42,6 +46,7 @@ public class Delivery extends BaseTimeEntity { @JoinColumn(name = "riderId") private Rider rider; + @Enumerated(EnumType.STRING) @Column(nullable = false) private DeliveryStatus deliveryStatus; @@ -96,9 +101,16 @@ public void checkAuthority(final Rider rider) { } public void assignRider(Rider rider) { + checkAlreadyAssignedToRider(); this.rider = rider; } + private void checkAlreadyAssignedToRider() { + if (Objects.nonNull(this.rider)) { + throw new AlreadyAssignedDeliveryException("이미 배차 완료된 배달입니다."); + } + } + public void completeDelivery() { this.arrivedAt = LocalDateTime.now(); this.deliveryStatus = DeliveryStatus.DELIVERED; diff --git a/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java b/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java index 306063b9..3b4cc19a 100644 --- a/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java +++ b/src/main/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepository.java @@ -21,7 +21,8 @@ public interface DeliveryRepository extends JpaRepository { @Query(value = "select d from Delivery d" + " where d.deliveryStatus" - + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER") + + " = com.prgrms.nabmart.domain.delivery.DeliveryStatus.ACCEPTING_ORDER" + + " and d.rider is null") Page findWaitingDeliveries(Pageable pageable); @Query(value = "select d from Delivery d" diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java b/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java index 7f39f8e9..f7539150 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/repository/DeliveryRepositoryTest.java @@ -88,6 +88,10 @@ void success() { int totalElements = 3; List orders = createAndSaveOrders(totalElements); List deliveries = createAndSaveDeliveries(orders); + List alreadyAssignedOrders = createAndSaveOrders(3); + List alreadyAssignedDeliveries + = createAndSaveDeliveries(alreadyAssignedOrders); + alreadyAssignedDeliveries.forEach(delivery -> delivery.assignRider(rider)); PageRequest pageRequest = PageRequest.of(0, 10); //when diff --git a/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java b/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java index e46005d9..241c41ff 100644 --- a/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java +++ b/src/test/java/com/prgrms/nabmart/domain/delivery/service/DeliveryServiceTest.java @@ -10,6 +10,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.delivery.exception.AlreadyAssignedDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.InvalidDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.NotFoundDeliveryException; import com.prgrms.nabmart.domain.delivery.exception.NotFoundRiderException; @@ -401,6 +402,21 @@ void throwExceptionWhenNotFoundDelivery() { assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand)) .isInstanceOf(NotFoundDeliveryException.class); } + + @Test + @DisplayName("예외: 이미 배차 완료된 배달") + void throwExceptionWhenAlreadyAssignedDelivery() { + //given + given(riderRepository.findById(any())).willReturn(Optional.ofNullable(rider)); + given(deliveryRepository.findByIdOptimistic(any())).willReturn( + Optional.ofNullable(delivery)); + delivery.assignRider(rider); + + //when + //then + assertThatThrownBy(() -> deliveryService.acceptDelivery(acceptDeliveryCommand)) + .isInstanceOf(AlreadyAssignedDeliveryException.class); + } } @Nested