Skip to content

Commit

Permalink
LIB-217 주문 내역 조회 API - 주문 상세정보 조회 (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
hseungho authored May 12, 2023
1 parent ba86d6d commit 9095d4e
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
public enum FeignErrorCode implements ErrorCode {

UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "다시 로그인 후 시도해주세요."),
UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "FEIGN_ERROR - 다시 로그인 후 시도해주세요."),
ERROR_4XX(HttpStatus.INTERNAL_SERVER_ERROR),
ERROR_5XX(HttpStatus.INTERNAL_SERVER_ERROR),
ERROR_UNKNOWN(HttpStatus.INTERNAL_SERVER_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.liberty52.product.global.exception.external.ErrorResponse;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
Expand All @@ -11,7 +12,7 @@ public class FeignRestExceptionHandler {

@ExceptionHandler(FeignClientException.class)
public ResponseEntity<ErrorResponse> handleFeignClientException(FeignClientException ex, HttpServletRequest request) {
return ResponseEntity.status(ex.getHttpStatus())
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.createErrorResponse(ex, request.getRequestURI()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ public interface OrderRetrieveService {

List<OrdersRetrieveResponse> retrieveOrders(String authId);

OrderDetailRetrieveResponse retrieveOrderDetail(String authId, String orderId);
OrderDetailRetrieveResponse retrieveOrderDetail(String authId, String orderId);

OrderDetailRetrieveResponse retrieveGuestOrderDetail(String authId, String orderNumber);

AdminOrderListResponse retrieveOrdersByAdmin(String role, Pageable pageable);

OrderDetailRetrieveResponse retrieveOrderDetailByAdmin(String role, String orderId);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.liberty52.product.service.applicationservice;

import com.liberty52.product.global.adapter.cloud.AuthServiceClient;
import com.liberty52.product.global.adapter.cloud.dto.AuthProfileDto;
import com.liberty52.product.global.exception.external.badrequest.CannotAccessOrderException;
import com.liberty52.product.global.util.Validator;
import com.liberty52.product.service.controller.dto.AdminOrderListResponse;
Expand Down Expand Up @@ -73,4 +74,18 @@ public AdminOrderListResponse retrieveOrdersByAdmin(String role, Pageable pageab
pageInfo.getTotalLastPage()
);
}

@Override
public OrderDetailRetrieveResponse retrieveOrderDetailByAdmin(String role, String orderId) {
Validator.isAdmin(role);

Orders order = orderQueryDslRepository.retrieveOrderDetailByOrderId(orderId)
.orElseThrow(CannotAccessOrderException::new);

String customerId = order.getAuthId();
String customerName = authServiceClient.retrieveAuthData(Set.of(customerId))
.get(customerId).getAuthorName();

return OrderDetailRetrieveResponse.of(order, customerName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ public AdminOrderListResponse retrieveOrdersByAdmin(
return orderRetrieveService.retrieveOrdersByAdmin(role, pageable);
}

@GetMapping("/admin/orders/{orderId}")
@ResponseStatus(HttpStatus.OK)
public OrderDetailRetrieveResponse retrieveOrderDetailByAdmin(
@RequestHeader("LB-Role") String role,
@PathVariable String orderId
) {
return orderRetrieveService.retrieveOrderDetailByAdmin(role, orderId);
}

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

import java.util.*;

@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
Expand Down Expand Up @@ -48,7 +47,6 @@ private AdminOrderListResponse(
}

@Getter
@ToString
public static class AdminOrderContent {
private String orderId;
private String orderNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import static com.liberty52.product.global.contants.RepresentImageUrl.LIBERTY52_FRAME_REPRESENTATIVE_URL;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.liberty52.product.global.exception.external.internalservererror.InvalidFormatException;
import com.liberty52.product.service.entity.OrderDestination;
import com.liberty52.product.service.entity.Orders;
import com.liberty52.product.service.entity.payment.Payment;
import com.querydsl.core.annotations.QueryProjection;
import java.util.HashMap;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import lombok.Data;

@Data
Expand All @@ -33,9 +28,9 @@ public class OrderDetailRetrieveResponse {
private String orderNum;
private String paymentType;
private Payment.PaymentInfo paymentInfo;


private List<OrderRetrieveProductResponse> products;
private String customerName;

public OrderDetailRetrieveResponse(Orders orders) {
this.orderId = orders.getId();
this.orderDate = orders.getOrderDate().toString();
Expand Down Expand Up @@ -88,7 +83,10 @@ public OrderDetailRetrieveResponse(String orderId, String orderDate, String orde
this.totalPrice = totalProductPrice + deliveryFee;
}



public static OrderDetailRetrieveResponse of(Orders entity, String customerName) {
OrderDetailRetrieveResponse response = new OrderDetailRetrieveResponse(entity);
response.customerName = customerName;
return response;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.liberty52.product.service.repository;

import com.liberty52.product.service.entity.Orders;
import org.aspectj.weaver.ast.Or;
import org.springframework.data.domain.Pageable;

import java.util.List;
Expand All @@ -16,6 +17,8 @@ public interface OrderQueryDslRepository {

List<Orders> retrieveOrdersByAdmin(Pageable pageable);

Optional<Orders> retrieveOrderDetailByOrderId(String orderId);

OrderQueryDslRepositoryImpl.PageInfo getPageInfo(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,15 @@ public OrderQueryDslRepositoryImpl(EntityManager em) {
}

public List<Orders> retrieveOrders(String authId) {

return selectOrdersAndAssociatedEntity()
.where(orders.authId.eq(authId).and(orders.orderStatus.ne(OrderStatus.READY)))
.orderBy(orders.orderDate.desc())
.fetch();

}

@Override
public Optional<Orders> retrieveOrderDetail(String authId,
String orderId) {

return Optional.ofNullable(
selectOrdersAndAssociatedEntity()
.where(orders.authId.eq(authId).and(orders.id.eq(orderId)).and(orders.orderStatus.ne(OrderStatus.READY)))
Expand All @@ -69,7 +66,6 @@ public Optional<Orders> retrieveOrderDetail(String authId,

@Override
public Optional<Orders> retrieveGuestOrderDetail(String guestId, String orderNumber) {

return Optional.ofNullable(
selectOrdersAndAssociatedEntity()
.where(orders.authId.eq(guestId).and(orders.orderNum.eq(orderNumber)).and(orders.orderStatus.ne(OrderStatus.READY)))
Expand All @@ -81,6 +77,15 @@ public List<Orders> retrieveOrdersByAdmin(Pageable pageable) {
return fetchOrdersByAdmin(pageable).fetch();
}

@Override
public Optional<Orders> retrieveOrderDetailByOrderId(String orderId) {
return Optional.ofNullable(
selectOrdersAndAssociatedEntity()
.where(orders.id.eq(orderId))
.fetchOne()
);
}


private JPAQuery<Orders> selectOrdersAndAssociatedEntity() {
return queryFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.liberty52.product.global.exception.external.badrequest.CannotAccessOrderException;
import com.liberty52.product.global.exception.external.forbidden.InvalidRoleException;
import com.liberty52.product.service.controller.dto.AdminOrderListResponse;
import com.liberty52.product.service.controller.dto.OrderDetailRetrieveResponse;
import com.liberty52.product.service.entity.OrderDestination;
import com.liberty52.product.service.entity.Orders;
import com.liberty52.product.service.utils.MockConstants;
Expand Down Expand Up @@ -137,4 +138,61 @@ void test_retrieveOrdersByAdmin_throw_InvalidRoleException() {
);
}

@Test
void test_retrieveOrderDetailByAdmin() {
final String ROLE_ADMIN = "ADMIN";
final String ORDER_ID = "GORDER-000";

OrderDetailRetrieveResponse response = orderRetrieveService.retrieveOrderDetailByAdmin(ROLE_ADMIN, ORDER_ID);

Assertions.assertNotNull(response);
Assertions.assertEquals(ORDER_ID, response.getOrderId());
Assertions.assertFalse(response.getOrderDate().isBlank());
Assertions.assertFalse(response.getOrderStatus().isBlank());
Assertions.assertFalse(response.getAddress().isBlank());
Assertions.assertFalse(response.getReceiverEmail().isBlank());
Assertions.assertFalse(response.getReceiverName().isBlank());
Assertions.assertFalse(response.getReceiverEmail().isBlank());
Assertions.assertFalse(response.getReceiverPhoneNumber().isBlank());
Assertions.assertNotEquals(0, response.getTotalProductPrice());
Assertions.assertNotEquals(0, response.getDeliveryFee());
Assertions.assertNotEquals(0, response.getTotalPrice());
Assertions.assertFalse(response.getOrderNum().isBlank());
Assertions.assertFalse(response.getPaymentType().isBlank());
Assertions.assertNotNull(response.getPaymentInfo());
Assertions.assertFalse(response.getCustomerName().isBlank());

Assertions.assertFalse(response.getProducts().isEmpty());
response.getProducts().forEach(product -> {
Assertions.assertFalse(product.getName().isBlank());
Assertions.assertNotEquals(0, product.getQuantity());
Assertions.assertNotEquals(0, product.getPrice());
Assertions.assertFalse(product.getProductUrl().isBlank());
Assertions.assertFalse(product.getOptions().isEmpty());
});
}

@Test
void test_retrieveOrderDetailByAdmin_throw_InvalidRoleException() {
final String ROLE_USER = "USER";
final String ORDER_ID = "GORDER-000";

Assertions.assertThrows(
InvalidRoleException.class,
() -> orderRetrieveService.retrieveOrderDetailByAdmin(ROLE_USER, ORDER_ID)
);
}

@Test
void test_retrieveOrderDetailByAdmin_throw_CannotAccessOrderException() {
final String ROLE_ADMIN = "ADMIN";
final String ORDER_ID = "GORDER-00x";

Assertions.assertThrows(
CannotAccessOrderException.class,
() -> orderRetrieveService.retrieveOrderDetailByAdmin(ROLE_ADMIN, ORDER_ID)
);
}


}

0 comments on commit 9095d4e

Please sign in to comment.