Skip to content

Commit

Permalink
Merge pull request #75 from ClothingStoreService/refactor/#6-add-orde…
Browse files Browse the repository at this point in the history
…r-inquery

Refactor/#6 add order inquery
  • Loading branch information
axhtl authored Jul 8, 2024
2 parents 9f35e82 + da03b0b commit 31998dd
Show file tree
Hide file tree
Showing 18 changed files with 275 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.store.clothstar.common.dto.MessageDTO;
import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.order.service.OrderSellerService;

Expand All @@ -14,15 +15,15 @@
@Tag(name = "OrderSeller", description = "판매자(OrderSeller)의 주문 정보 관리에 대한 API 입니다.")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/seller/orders")
@RequestMapping("/v1/orders")
public class OrderSellerController {

private final OrderSellerService orderSellerService;

@Operation(summary = "(판매자) WAITING 주문 리스트 조회", description = "(판매자) 주문상태가 '승인대기'인 주문 리스트를 조회한다.")
@GetMapping
public ResponseEntity<List<OrderEntity>> getWaitingOrder() {
List<OrderEntity> orderResponseList = orderSellerService.getWaitingOrder();
public ResponseEntity<List<OrderResponse>> getWaitingOrder() {
List<OrderResponse> orderResponseList = orderSellerService.getWaitingOrder();
return ResponseEntity.ok(orderResponseList);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,99 @@
package org.store.clothstar.order.dto.reponse;

import com.querydsl.core.annotations.QueryProjection;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.store.clothstar.member.entity.AddressEntity;
import org.store.clothstar.member.entity.MemberEntity;
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.order.type.PaymentMethod;
import org.store.clothstar.order.type.Status;
import org.store.clothstar.orderDetail.dto.OrderDetailDTO;
import org.store.clothstar.orderDetail.entity.OrderDetailEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Schema(description = "주문 조회용 Response")
public class OrderResponse {

@Schema(description = "주문 id", example = "1")
private Long orderId;

@Schema(description = "회원 id", example = "1")
private Long memberId;

@Schema(description = "배송지 id", example = "1")
private Long addressId;
@Schema(description = "주문자 이름", example = "수빈")
private String ordererName;

@Schema(description = "주문 생성 날짜", example = "2024-05-15")
private LocalDate createdAt;

@Schema(description = "주문 상태", example = "WAITING")
private Status status;

//address
private String receiverName;
private String addressBasic;
private String addressDetail;
private String telNo;
private String deliveryRequest;

@Schema(description = "결제 수단", example = "CARD")
private PaymentMethod paymentMethod;

@Schema(description = "총 배송비", example = "3000")
private int totalShippingPrice;

@Schema(description = "총 상품 금액", example = "15000")
private int totalProductsPrice;

@Schema(description = "결제 수단", example = "CARD")
private PaymentMethod paymentMethod;

@Schema(description = "총 결제 금액", example = "18000")
private int totalPaymentPrice;

@Builder.Default
private List<OrderDetailDTO> orderDetailList = new ArrayList<>();

public OrderResponse(OrderEntity orderEntity, OrderDetailEntity orderDetailEntity, MemberEntity memberEntity, AddressEntity addressEntity,
ProductLineEntity productLineEntity, List<OrderDetailDTO> orderDetailList) {
this(orderEntity,
orderDetailEntity,
memberEntity, addressEntity, productLineEntity);
this.orderDetailList = orderDetailList != null ? orderDetailList : new ArrayList<>();
}


@QueryProjection
public OrderResponse(OrderEntity orderEntity,
OrderDetailEntity orderDetailEntity,
MemberEntity memberEntity, AddressEntity addressEntity,
ProductLineEntity productLineEntity){
this.orderId = orderEntity.getOrderId();
this.ordererName = memberEntity.getName();
this.createdAt = orderEntity.getCreatedAt().toLocalDate();
this.status = orderEntity.getStatus();
this.totalShippingPrice = orderEntity.getTotalShippingPrice();
this.totalProductsPrice = orderEntity.getTotalProductsPrice();
this.paymentMethod = orderEntity.getPaymentMethod();
this.totalPaymentPrice = orderEntity.getTotalPaymentPrice();
this.receiverName = addressEntity.getReceiverName();
this.addressBasic = addressEntity.getAddressBasic();
this.addressDetail = addressEntity.getAddressDetail();
this.telNo = addressEntity.getTelNo();
this.deliveryRequest = addressEntity.getDeliveryRequest();
this.orderDetailList = new ArrayList<>();
}

public static OrderResponse fromOrderEntity(OrderEntity orderEntity) {
return OrderResponse.builder()
.orderId(orderEntity.getOrderId())
.memberId(orderEntity.getMember().getMemberId())
.addressId(orderEntity.getAddress().getAddressId())
.ordererName(orderEntity.getMember().getName())
.createdAt(orderEntity.getCreatedAt().toLocalDate())
.status(orderEntity.getStatus())
.totalShippingPrice(orderEntity.getTotalShippingPrice())
Expand All @@ -55,4 +102,8 @@ public static OrderResponse fromOrderEntity(OrderEntity orderEntity) {
.totalPaymentPrice(orderEntity.getTotalPaymentPrice())
.build();
}

public void setterOrderDetailList(List<OrderDetailDTO> orderDetailDTOList){
this.orderDetailList=orderDetailDTOList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import org.store.clothstar.member.entity.MemberEntity;
import org.store.clothstar.order.type.PaymentMethod;
import org.store.clothstar.order.type.Status;
import org.store.clothstar.orderDetail.entity.OrderDetailEntity;

import java.time.LocalDateTime;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -21,6 +23,9 @@ public class OrderEntity {
@Id
private Long orderId;

@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
private List<OrderDetailEntity> orderDetails;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_at")
private LocalDateTime createdAt;
Expand Down Expand Up @@ -48,6 +53,9 @@ public class OrderEntity {
@OneToOne
@JoinColumn(name = "address_id")
private AddressEntity address;
//
// @ManyToOne
// private OrderDetailEntity orderDetail;

public void setTotalProductsPrice(int totalProductsPrice) {
this.totalProductsPrice = totalProductsPrice;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.order.repository.orderSeller.OrderEntityRepositoryCustom;

public interface JpaOrderRepository extends JpaRepository<OrderEntity, Long>, OrderRepository, OrderEntityRepositoryCustom {

public interface JpaOrderRepository extends JpaRepository<OrderEntity, Long>, OrderRepository {
@Transactional
@Modifying
@Query("UPDATE orders o SET o.status ='CONFIRM' WHERE o.orderId = :orderId")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.store.clothstar.order.repository.order;

import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.entity.OrderEntity;

import java.util.Optional;
Expand All @@ -8,6 +9,8 @@ public interface OrderRepository {

Optional<OrderEntity> findById(Long orderId);

OrderResponse findOrderWithDetails(Long orderId);

OrderEntity save(OrderEntity orderEntity);

void deliveredToConfirmOrder(Long orderId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.store.clothstar.order.repository.orderSeller;

import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.entity.OrderEntity;

import java.util.List;

public interface OrderEntityRepositoryCustom {
List<OrderEntity> findWaitingOrders();
OrderResponse findOrderWithDetails(Long orderId);

List<OrderResponse> findWaitingOrders();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.store.clothstar.order.entity.OrderEntity;
import org.store.clothstar.member.entity.QAddressEntity;
import org.store.clothstar.member.entity.QMemberEntity;
import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.dto.reponse.QOrderResponse;
import org.store.clothstar.order.entity.QOrderEntity;
import org.store.clothstar.order.type.Status;
import org.store.clothstar.orderDetail.dto.OrderDetailDTO;
import org.store.clothstar.orderDetail.dto.QOrderDetailDTO;
import org.store.clothstar.orderDetail.entity.QOrderDetailEntity;
import org.store.clothstar.product.entity.QProductEntity;
import org.store.clothstar.productLine.entity.QProductLineEntity;

import java.util.List;

Expand All @@ -15,13 +23,83 @@ public class OrderEntityRepositoryCustomImpl implements OrderEntityRepositoryCus

private final JPAQueryFactory jpaQueryFactory;

@Override
public List<OrderEntity> findWaitingOrders() {
QOrderEntity qOrderEntity = QOrderEntity.orderEntity;
QOrderEntity qOrderEntity = QOrderEntity.orderEntity;
QOrderDetailEntity qOrderDetailEntity = QOrderDetailEntity.orderDetailEntity;
QMemberEntity qMemberEntity = QMemberEntity.memberEntity;
QAddressEntity qAddressEntity = QAddressEntity.addressEntity;
QProductEntity qProductEntity = QProductEntity.productEntity;
QProductLineEntity qProductLineEntity = QProductLineEntity.productLineEntity;

return jpaQueryFactory.select(qOrderEntity)
@Override
public List<OrderResponse> findWaitingOrders() {
// return jpaQueryFactory.select(qOrderEntity)
// .from(qOrderEntity)
// .where(qOrderEntity.status.eq(Status.WAITING))
// .fetch();
List<OrderResponse> results = jpaQueryFactory
.select(new QOrderResponse(
qOrderEntity,
qOrderDetailEntity,
qMemberEntity,
qAddressEntity,
qProductLineEntity))
.from(qOrderEntity)
.innerJoin(qOrderEntity.member, qMemberEntity)
.innerJoin(qOrderEntity.address, qAddressEntity)
.innerJoin(qOrderEntity.orderDetails, qOrderDetailEntity)
.innerJoin(qOrderDetailEntity.product, qProductEntity)
.innerJoin(qProductEntity.productLine, qProductLineEntity)
.where(qOrderEntity.status.eq(Status.WAITING))
.groupBy(qOrderEntity.orderId)
.fetch();

if (results != null) {
results.forEach(result -> {
List<OrderDetailDTO> orderDetailList = jpaQueryFactory
.select(new QOrderDetailDTO(
qOrderDetailEntity
))
.from(qOrderDetailEntity)
.where(qOrderDetailEntity.order.orderId.eq(result.getOrderId()))
.fetch();

result.setterOrderDetailList(orderDetailList);
});
}
return results;
}

@Override
public OrderResponse findOrderWithDetails(Long orderId){

OrderResponse result = jpaQueryFactory
.select(new QOrderResponse(
qOrderEntity,
qOrderDetailEntity,
qMemberEntity,
qAddressEntity,
qProductLineEntity))
.from(qOrderEntity)
.innerJoin(qOrderEntity.member, qMemberEntity)
.innerJoin(qOrderEntity.address, qAddressEntity)
.innerJoin(qOrderEntity.orderDetails, qOrderDetailEntity)
.innerJoin(qOrderDetailEntity.product, qProductEntity)
.innerJoin(qProductEntity.productLine, qProductLineEntity)
.where(qOrderEntity.orderId.eq(orderId))
.groupBy(qOrderEntity.orderId)
.fetchOne();

if (result != null) {
List<OrderDetailDTO> orderDetailList = jpaQueryFactory
.select(new QOrderDetailDTO(
qOrderDetailEntity
))
.from(qOrderDetailEntity)
.where(qOrderDetailEntity.order.orderId.eq(orderId))
.fetch();

result.setterOrderDetailList(orderDetailList);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.store.clothstar.order.repository.orderSeller;

import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.entity.OrderEntity;

import java.util.List;

public interface OrderSellerRepository {

List<OrderEntity> findWaitingOrders();
List<OrderResponse> findWaitingOrders();

void approveOrder(Long orderId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public OrderSellerService(
}

@Transactional(readOnly = true)
public List<OrderEntity> getWaitingOrder() {
public List<OrderResponse> getWaitingOrder() {

return orderSellerRepository.findWaitingOrders().stream()
.collect(Collectors.toList());
Expand All @@ -53,10 +53,6 @@ public MessageDTO approveOrder(Long orderId) {
orderSellerRepository.approveOrder(orderId);
messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 승인 되었습니다.");

orderRepository.findById(orderId)
.map(OrderResponse::fromOrderEntity)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "처리 후 주문 정보를 찾을 수 없습니다."));

return messageDTO;
}

Expand All @@ -73,10 +69,6 @@ public MessageDTO cancelOrder(Long orderId) {
orderDetailService.restoreStockByOrder(orderId);
messageDTO = new MessageDTO(HttpStatus.OK.value(), "주문이 정상적으로 취소 되었습니다.");

orderRepository.findById(orderId)
.map(OrderResponse::fromOrderEntity)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "처리 후 주문 정보를 찾을 수 없습니다."));

return messageDTO;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public OrderService(
@Transactional(readOnly = true)
public OrderResponse getOrder(Long orderId) {

return orderRepository.findById(orderId)
.map(OrderResponse::fromOrderEntity)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.BAD_REQUEST, "존재하지 않는 주문번호입니다."));
return orderRepository.findOrderWithDetails(orderId);
}

@Transactional
Expand Down
Loading

0 comments on commit 31998dd

Please sign in to comment.