Skip to content

Commit

Permalink
[NAYB-159] feat: 점원과 라이더는 배달 상세를 조회할 수 있다.
Browse files Browse the repository at this point in the history
[NAYB-159] feat: 점원과 라이더는 배달 상세를 조회할 수 있다.
  • Loading branch information
hseong3243 authored Sep 21, 2023
2 parents 3c2e596 + 7dc2ebe commit c539360
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import com.prgrms.nabmart.domain.delivery.service.DeliveryService;
import com.prgrms.nabmart.domain.delivery.service.request.AcceptDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.CompleteDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryByOrderCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryDetailCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindRiderDeliveriesCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindWaitingDeliveriesCommand;
import com.prgrms.nabmart.domain.delivery.service.request.RegisterDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.StartDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryDetailResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryByOrderResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindRiderDeliveriesResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindWaitingDeliveriesResponse;
import com.prgrms.nabmart.global.auth.LoginUser;
Expand Down Expand Up @@ -58,12 +59,23 @@ public ResponseEntity<Void> registerDelivery(
}

@GetMapping("/orders/{orderId}/deliveries")
public ResponseEntity<FindDeliveryDetailResponse> findDelivery(
public ResponseEntity<FindDeliveryByOrderResponse> findDeliveryByOrder(
@PathVariable final Long orderId,
@LoginUser final Long userId) {
FindDeliveryCommand findDeliveryCommand = FindDeliveryCommand.of(userId, orderId);
FindDeliveryDetailResponse findDeliveryDetailResponse
= deliveryService.findDelivery(findDeliveryCommand);
FindDeliveryByOrderCommand findDeliveryByOrderCommand
= FindDeliveryByOrderCommand.of(userId, orderId);
FindDeliveryByOrderResponse findDeliveryByOrderResponse
= deliveryService.findDeliveryByOrder(findDeliveryByOrderCommand);
return ResponseEntity.ok(findDeliveryByOrderResponse);
}

@GetMapping("/deliveries/{deliveryId}")
public ResponseEntity<FindDeliveryDetailResponse> findDelivery(
@PathVariable final Long deliveryId) {
FindDeliveryDetailCommand findDeliveryDetailCommand
= FindDeliveryDetailCommand.from(deliveryId);
FindDeliveryDetailResponse findDeliveryDetailResponse = deliveryService.findDelivery(
findDeliveryDetailCommand);
return ResponseEntity.ok(findDeliveryDetailResponse);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.prgrms.nabmart.domain.delivery.controller;

import com.prgrms.nabmart.domain.delivery.Delivery;
import com.prgrms.nabmart.domain.delivery.DeliveryStatus;
import com.prgrms.nabmart.domain.order.OrderItem;
import java.time.LocalDateTime;
import java.util.List;

public record FindDeliveryDetailResponse(
Long deliveryId,
DeliveryStatus deliveryStatus,
LocalDateTime arrivedAt,
String address,
String orderName,
int orderPrice,
String riderRequest,
int deliveryFee,
List<OrderItemResponse> items) {


public static FindDeliveryDetailResponse from(final Delivery delivery) {
List<OrderItemResponse> items = delivery.getOrder().getOrderItems().stream()
.map(OrderItemResponse::from)
.toList();
return new FindDeliveryDetailResponse(
delivery.getDeliveryId(),
delivery.getDeliveryStatus(),
delivery.getArrivedAt(),
delivery.getAddress(),
delivery.getOrder().getName(),
delivery.getOrderPrice(),
delivery.getRiderRequest(),
delivery.getDeliveryFee(),
items);
}

public record OrderItemResponse(String name, int quantity, int price) {

public static OrderItemResponse from(final OrderItem orderItem) {
return new OrderItemResponse(
orderItem.getItem().getName(),
orderItem.getQuantity(),
orderItem.getItem().getPrice());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,11 @@ Page<Delivery> findRiderDeliveries(
List<Delivery> findAllByUser(@Param("user") User user);

boolean existsByOrder(Order order);

@Query("select d from Delivery d"
+ " join fetch d.order o"
+ " join fetch o.orderItems oi"
+ " join fetch oi.item i"
+ " where d.deliveryId = :deliveryId")
Optional<Delivery> findByIdWithOrderAndItems(@Param("deliveryId") Long deliveryId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.prgrms.nabmart.domain.delivery.Delivery;
import com.prgrms.nabmart.domain.delivery.Rider;
import com.prgrms.nabmart.domain.delivery.controller.FindDeliveryDetailResponse;
import com.prgrms.nabmart.domain.delivery.exception.AlreadyRegisteredDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.NotFoundDeliveryException;
import com.prgrms.nabmart.domain.delivery.exception.NotFoundRiderException;
Expand All @@ -14,12 +15,13 @@
import com.prgrms.nabmart.domain.delivery.repository.RiderRepository;
import com.prgrms.nabmart.domain.delivery.service.request.AcceptDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.CompleteDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryByOrderCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindDeliveryDetailCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindRiderDeliveriesCommand;
import com.prgrms.nabmart.domain.delivery.service.request.FindWaitingDeliveriesCommand;
import com.prgrms.nabmart.domain.delivery.service.request.RegisterDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.request.StartDeliveryCommand;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryDetailResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryByOrderResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindRiderDeliveriesResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindWaitingDeliveriesResponse;
import com.prgrms.nabmart.domain.notification.NotificationType;
Expand Down Expand Up @@ -92,11 +94,12 @@ private void checkAlreadyRegisteredDelivery(final Order order) {
}

@Transactional(readOnly = true)
public FindDeliveryDetailResponse findDelivery(FindDeliveryCommand findDeliveryCommand) {
User user = findUserByUserId(findDeliveryCommand.userId());
Delivery delivery = findDeliveryByOrderWithOrder(findDeliveryCommand.orderId());
public FindDeliveryByOrderResponse findDeliveryByOrder(
FindDeliveryByOrderCommand findDeliveryByOrderCommand) {
User user = findUserByUserId(findDeliveryByOrderCommand.userId());
Delivery delivery = findDeliveryByOrderWithOrder(findDeliveryByOrderCommand.orderId());
checkAuthority(delivery, user);
return FindDeliveryDetailResponse.from(delivery);
return FindDeliveryByOrderResponse.from(delivery);
}

private void checkAuthority(final Delivery delivery, final User user) {
Expand All @@ -105,6 +108,21 @@ private void checkAuthority(final Delivery delivery, final User user) {
}
}

@Transactional(readOnly = true)
public FindDeliveryDetailResponse findDelivery(
FindDeliveryDetailCommand findDeliveryDetailCommand) {
Delivery delivery = findDeliveryByDeliveryIdWithOrderAndOrderItems(
findDeliveryDetailCommand);
return FindDeliveryDetailResponse.from(delivery);
}

private Delivery findDeliveryByDeliveryIdWithOrderAndOrderItems(
FindDeliveryDetailCommand findDeliveryDetailCommand) {
return deliveryRepository.findByIdWithOrderAndItems(
findDeliveryDetailCommand.deliveryId())
.orElseThrow(() -> new NotFoundDeliveryException("존재하지 않는 배달입니다."));
}

@Transactional
public void acceptDelivery(AcceptDeliveryCommand acceptDeliveryCommand) {
Rider rider = findRiderByRiderId(acceptDeliveryCommand.riderId());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.prgrms.nabmart.domain.delivery.service.request;

public record FindDeliveryByOrderCommand(Long userId, Long orderId) {

public static FindDeliveryByOrderCommand of(final Long userId,final Long orderId) {
return new FindDeliveryByOrderCommand(userId, orderId);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.prgrms.nabmart.domain.delivery.service.request;

public record FindDeliveryDetailCommand(Long deliveryId) {

public static FindDeliveryDetailCommand from(final Long deliveryId) {
return new FindDeliveryDetailCommand(deliveryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.prgrms.nabmart.domain.delivery.DeliveryStatus;
import java.time.LocalDateTime;

public record FindDeliveryDetailResponse(
public record FindDeliveryByOrderResponse(
Long deliveryId,
DeliveryStatus deliveryStatus,
LocalDateTime createdAt,
Expand All @@ -14,8 +14,8 @@ public record FindDeliveryDetailResponse(
int orderPrice,
String riderRequest) {

public static FindDeliveryDetailResponse from(final Delivery delivery) {
return new FindDeliveryDetailResponse(
public static FindDeliveryByOrderResponse from(final Delivery delivery) {
return new FindDeliveryByOrderResponse(
delivery.getDeliveryId(),
delivery.getDeliveryStatus(),
delivery.getCreatedAt(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ private RequestMatcher[] requestHasRoleRider() {
private RequestMatcher[] requestHasRoleEmployee() {
List<RequestMatcher> requestMatchers = List.of(
antMatcher(POST, "/api/v1/orders/*/deliveries"),
antMatcher(GET, "/api/v1/orders/payed")
antMatcher(GET, "/api/v1/orders/payed"),
antMatcher(GET, "/api/v1/deliveries/[0-9]+")
);
return requestMatchers.toArray(RequestMatcher[]::new);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@

import com.prgrms.nabmart.base.BaseControllerTest;
import com.prgrms.nabmart.domain.delivery.DeliveryStatus;
import com.prgrms.nabmart.domain.delivery.controller.FindDeliveryDetailResponse.OrderItemResponse;
import com.prgrms.nabmart.domain.delivery.controller.request.RegisterDeliveryRequest;
import com.prgrms.nabmart.domain.delivery.controller.request.StartDeliveryRequest;
import com.prgrms.nabmart.domain.delivery.exception.AlreadyAssignedDeliveryException;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryDetailResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindDeliveryByOrderResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindRiderDeliveriesResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindRiderDeliveriesResponse.FindRiderDeliveryResponse;
import com.prgrms.nabmart.domain.delivery.service.response.FindWaitingDeliveriesResponse;
Expand Down Expand Up @@ -81,17 +82,17 @@ void registerDelivery() throws Exception {

@Nested
@DisplayName("배달 현황 조회 API 호출 시")
class FindDeliveryTest {
class FindDeliveryByOrderTest {

@Test
@DisplayName("성공")
void findDelivery() throws Exception {
void findDeliveryByOrder() throws Exception {
//given
Long orderId = 1L;
FindDeliveryDetailResponse findDeliveryDetailResponse
FindDeliveryByOrderResponse findDeliveryByOrderResponse
= DeliveryFixture.findDeliveryDetailResponse();

given(deliveryService.findDelivery(any())).willReturn(findDeliveryDetailResponse);
given(deliveryService.findDeliveryByOrder(any())).willReturn(findDeliveryByOrderResponse);

//when
ResultActions resultActions = mockMvc
Expand Down Expand Up @@ -348,4 +349,59 @@ void findRiderDeliveries() throws Exception {
));
}
}

@Nested
@DisplayName("배달 상세 조회(라이더, 직원) API 조회")
class FindDeliveryTest {

@Test
@DisplayName("성공")
void findDelivery() throws Exception {
//given
Long deliveryId = 1L;
OrderItemResponse orderItemResponse = new OrderItemResponse("비비고 왕교자", 2, 5990);
FindDeliveryDetailResponse findDeliveryDetailResponse = new FindDeliveryDetailResponse(
1L,
DeliveryStatus.ACCEPTING_ORDER,
LocalDateTime.now().plusMinutes(30),
"address",
"비비고 왕교자 외 2개",
30000,
"배달 요청 사항",
3000,
List.of(orderItemResponse));

given(deliveryService.findDelivery(any())).willReturn(findDeliveryDetailResponse);

//when
ResultActions resultActions = mockMvc.perform(
get("/api/v1/deliveries/{deliveryId}", deliveryId)
.header(AUTHORIZATION, accessToken));

//then
resultActions.andExpect(status().isOk())
.andDo(restDocs.document(
requestHeaders(
headerWithName(AUTHORIZATION).description("액세스 토큰")
),
pathParameters(
parameterWithName("deliveryId").description("배달 ID")
),
responseFields(
fieldWithPath("deliveryId").type(NUMBER).description("배달 ID"),
fieldWithPath("deliveryStatus").type(STRING).description("배달 상태"),
fieldWithPath("arrivedAt").type(STRING).description("배달 예상 도착 시간"),
fieldWithPath("address").type(STRING).description("배달지 주소"),
fieldWithPath("orderName").type(STRING).description("주문 이름"),
fieldWithPath("orderPrice").type(NUMBER).description("주문 가격"),
fieldWithPath("riderRequest").type(STRING).description("배달 요청 사항"),
fieldWithPath("deliveryFee").type(NUMBER).description("배달비"),
fieldWithPath("items").type(ARRAY).description("상품 목록"),
fieldWithPath("items[].name").type(STRING).description("상품 이름"),
fieldWithPath("items[].quantity").type(NUMBER).description("상품 갯수"),
fieldWithPath("items[].price").type(NUMBER).description("상품 가격")
)
));
}
}
}
Loading

0 comments on commit c539360

Please sign in to comment.