Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.store.clothstar.category.service.CategoryService;
import org.store.clothstar.common.dto.MessageDTO;
import org.store.clothstar.common.util.URIBuilder;
import org.store.clothstar.productLine.dto.response.ProductLineWithProductsJPAResponse;
import org.store.clothstar.productLine.dto.response.ProductLineWithProductsResponse;
import org.store.clothstar.productLine.service.ProductLineService;

import java.net.URI;
Expand Down Expand Up @@ -68,21 +68,21 @@ public ResponseEntity<MessageDTO> updateCategories(

@Operation(summary = "카테고리별 상품 조회 (Offset Paging)", description = "카테고리 ID로 해당 카테고리에 속하는 모든 상품을 Offset Paging을 통해 조회한다.")
@GetMapping("/{categoryId}/productLines/offset")
public ResponseEntity<Page<ProductLineWithProductsJPAResponse>> getProductLinesByCategory(
public ResponseEntity<Page<ProductLineWithProductsResponse>> getProductLinesByCategory(
@PathVariable Long categoryId,
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword) {
Page<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getProductLinesByCategoryWithOffsetPaging(categoryId, pageable, keyword);
Page<ProductLineWithProductsResponse> productLineResponses = productLineService.getProductLinesByCategoryWithOffsetPaging(categoryId, pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}

@Operation(summary = "카테고리별 상품 조회 (Slice Paging)", description = "카테고리 ID로 해당 카테고리에 속하는 모든 상품을 Slice Paging을 통해 조회한다.")
@GetMapping("/{categoryId}/productLines/slice")
public ResponseEntity<Slice<ProductLineWithProductsJPAResponse>> getProductLinesByCategorySlice(
public ResponseEntity<Slice<ProductLineWithProductsResponse>> getProductLinesByCategorySlice(
@PathVariable Long categoryId,
@PageableDefault(size = 18) Pageable pageable,
@RequestParam(required = false) String keyword) {
Slice<ProductLineWithProductsJPAResponse> productLineResponses = productLineService.getProductLinesByCategoryWithSlicePaging(categoryId, pageable, keyword);
Slice<ProductLineWithProductsResponse> productLineResponses = productLineService.getProductLinesByCategoryWithSlicePaging(categoryId, pageable, keyword);
return ResponseEntity.ok().body(productLineResponses);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.product.domain.Product;
import org.store.clothstar.productLine.domain.ProductLine;

@Getter
@AllArgsConstructor
Expand All @@ -23,15 +23,15 @@ public class OrderDetailDTO {
private int quantity;
private int totalPrice; // 상품 종류 하나당 총 가격

public static OrderDetailDTO from(OrderDetail orderDetail, ProductEntity productEntity, ProductLineEntity productLineEntity){
public static OrderDetailDTO from(OrderDetail orderDetail, Product product, ProductLine productLine){

return OrderDetailDTO.builder()
.orderDetailId(orderDetail.getOrderDetailId())
.productName(productLineEntity.getName())
.optionName(productEntity.getName())
.brandName(productLineEntity.getSeller().getBrandName())
.productPrice(productLineEntity.getPrice())
.extraCharge(productEntity.getExtraCharge())
.productName(productLine.getName())
.optionName(product.getName())
.brandName(productLine.getSeller().getBrandName())
.productPrice(productLine.getPrice())
.extraCharge(product.getExtraCharge())
.quantity(orderDetail.getQuantity())
.totalPrice(orderDetail.getPrice().getOneKindTotalPrice())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.domain.vo.Price;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.product.domain.Product;
import org.store.clothstar.productLine.domain.ProductLine;

@Getter
@AllArgsConstructor
Expand All @@ -38,16 +38,16 @@ public class AddOrderDetailRequest {
private int quantity;


public OrderDetail toOrderDetail(Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) {
public OrderDetail toOrderDetail(Order order, ProductLine productLine, Product product) {
Price price = Price.builder()
.fixedPrice(productLineEntity.getPrice())
.oneKindTotalPrice(quantity * productLineEntity.getPrice())
.fixedPrice(productLine.getPrice())
.oneKindTotalPrice(quantity * productLine.getPrice())
.build();

return OrderDetail.builder()
.order(order)
.productLineId(productLineEntity.getProductLineId())
.productId(productEntity.getProductId())
.productLineId(productLine.getProductLineId())
.productId(product.getProductId())
.quantity(quantity)
.price(price)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import org.store.clothstar.order.domain.Order;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.domain.vo.Price;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.product.domain.Product;
import org.store.clothstar.productLine.domain.ProductLine;

@Getter
@AllArgsConstructor
Expand All @@ -34,16 +34,16 @@ public class CreateOrderDetailRequest {
private int quantity;


public OrderDetail toOrderDetail(Order order, ProductLineEntity productLineEntity, ProductEntity productEntity) {
public OrderDetail toOrderDetail(Order order, ProductLine productLine, Product product) {
Price price = Price.builder()
.fixedPrice(productLineEntity.getPrice())
.oneKindTotalPrice(quantity * productLineEntity.getPrice())
.fixedPrice(productLine.getPrice())
.oneKindTotalPrice(quantity * productLine.getPrice())
.build();

return OrderDetail.builder()
.order(order)
.productLineId(productLineEntity.getProductLineId())
.productId(productEntity.getProductId())
.productLineId(productLine.getProductLineId())
.productId(product.getProductId())
.quantity(quantity)
.price(price)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
import org.store.clothstar.order.domain.type.Status;
import org.store.clothstar.order.dto.request.AddOrderDetailRequest;
import org.store.clothstar.order.repository.order.OrderDetailRepository;
import org.store.clothstar.order.repository.order.OrderUserRepository;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.product.repository.ProductJPARepository;
import org.store.clothstar.product.domain.Product;
import org.store.clothstar.product.repository.ProductRepository;
import org.store.clothstar.product.service.ProductService;
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.productLine.repository.ProductLineJPARepository;
import org.store.clothstar.productLine.domain.ProductLine;
import org.store.clothstar.productLine.repository.ProductLineRepository;

import java.util.List;

Expand All @@ -25,20 +24,52 @@ public class OrderDetailService {
private final OrderUserRepository orderUserRepository;
private final ProductService productService;
private final OrderDetailRepository orderDetailRepository;
private final ProductJPARepository productJPARepository;
private final ProductLineJPARepository productLineJPARepository;
private final ProductRepository productRepository;
private final ProductLineRepository productLineRepository;

public OrderDetailService(
OrderDetailRepository orderDetailRepository,
OrderUserRepository orderUserRepository, ProductService productService
, ProductJPARepository productJPARepository
, ProductLineJPARepository productLineJPARepository
, ProductRepository productRepository
, ProductLineRepository productLineRepository
) {
this.orderUserRepository = orderUserRepository;
this.orderDetailRepository = orderDetailRepository;
this.productService = productService;
this.productJPARepository = productJPARepository;
this.productLineJPARepository = productLineJPARepository;
this.productRepository = productRepository;
this.productLineRepository = productLineRepository;
}

// 주문 생성시 같이 호출되는 주문 상세 생성 메서드 - 하나의 트랜잭션으로 묶임
@Transactional
public void saveOrderDetailWithOrder(CreateOrderDetailRequest createOrderDetailRequest, long orderId) {

Order order = orderUserRepository.findById(orderId)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다."));

ProductLineEntity productLineEntity = productLineJPARepository.findById(createOrderDetailRequest.getProductLineId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션 정보를 찾을 수 없습니다."));

ProductEntity productEntity = productJPARepository.findById(createOrderDetailRequest.getProductId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다."));

// 주문상세 생성 유효성 검사: 주문 수량이 상품 재고보다 클 경우, 주문이 생성되지 않는다.
if (createOrderDetailRequest.getQuantity() > productEntity.getStock()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다.");
}

OrderDetail orderDetail = createOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity);
orderDetailRepository.save(orderDetail);

// 주문 정보 업데이트: 주문 상세 생성에 따른, 총 상품 금액과 총 결제 금액 업데이트
int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice();
int newTotalPaymentPrice =
order.getTotalPrice().getProducts() + order.getTotalPrice().getShipping() + orderDetail.getPrice().getOneKindTotalPrice();

order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice);

// 주문 수량만큼 상품 재고 차감
updateProductStock(productEntity, orderDetail.getQuantity());
}

// 주문 상세 추가 생성
Expand All @@ -49,20 +80,20 @@ public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) {
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "주문 정보를 찾을 수 없습니다."));

ProductLineEntity productLineEntity = productLineJPARepository.findById(addOrderDetailRequest.getProductLineId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션 정보를 찾을 수 없습니다."));

ProductEntity productEntity = productJPARepository.findById(addOrderDetailRequest.getProductId())
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 옵션 정보를 찾을 수 없습니다."));
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "상품 정보를 찾을 수 없습니다."));

if (addOrderDetailRequest.getQuantity() > productEntity.getStock()) {
if (addOrderDetailRequest.getQuantity() > product.getStock()) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문 개수가 재고보다 더 많습니다.");
}

if (!order.getStatus().equals(Status.WAITING)) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "주문이 이미 처리된 상태에서는 추가 주문이 불가능합니다.");
}

OrderDetail orderDetail = addOrderDetailRequest.toOrderDetail(order, productLineEntity, productEntity);
OrderDetail orderDetail = addOrderDetailRequest.toOrderDetail(order, productLine, product);
orderDetailRepository.save(orderDetail);

int newTotalProductsPrice = order.getTotalPrice().getProducts() + orderDetail.getPrice().getOneKindTotalPrice();
Expand All @@ -71,7 +102,7 @@ public Long addOrderDetail(AddOrderDetailRequest addOrderDetailRequest) {

order.getTotalPrice().updatePrices(newTotalProductsPrice, newTotalPaymentPrice);

updateProductStock(productEntity, orderDetail.getQuantity());
updateProductStock(product, orderDetail.getQuantity());

return orderDetail.getOrderDetailId();
}
Expand All @@ -90,9 +121,9 @@ public void updateDeleteAt(Long orderDetailId) {
}

@Transactional
public void updateProductStock(ProductEntity productEntity, int quantity) {
long updatedStock = productEntity.getStock() - quantity;
productEntity.updateStock(updatedStock);
public void updateProductStock(Product product, int quantity) {
long updatedStock = product.getStock() - quantity;
product.updateStock(updatedStock);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
import org.store.clothstar.order.dto.reponse.OrderResponse;
import org.store.clothstar.order.repository.order.OrderUserRepository;
import org.store.clothstar.order.repository.orderSeller.OrderSellerRepository;
import org.store.clothstar.product.entity.ProductEntity;
import org.store.clothstar.order.domain.type.Status;
import org.store.clothstar.order.domain.OrderDetail;
import org.store.clothstar.order.domain.vo.OrderDetailDTO;
import org.store.clothstar.product.domain.Product;
import org.store.clothstar.product.service.ProductService;
import org.store.clothstar.productLine.entity.ProductLineEntity;
import org.store.clothstar.productLine.domain.ProductLine;
import org.store.clothstar.productLine.service.ProductLineService;

import java.util.List;
Expand Down Expand Up @@ -71,16 +74,16 @@ public List<OrderResponse> getWaitingOrder() {
List<Long> productIds = orderDetails.stream().map(OrderDetail::getProductId).collect(Collectors.toList());
List<Long> productLineIds = orderDetails.stream().map(OrderDetail::getProductLineId).collect(Collectors.toList());

List<ProductEntity> products = productService.findByIdIn(productIds);
List<ProductLineEntity> productLines = productLineService.findByIdIn(productLineIds);
List<Product> products = productService.findByIdIn(productIds);
List<ProductLine> productLines = productLineService.findByIdIn(productLineIds);

Map<Long, ProductEntity> productMap = products.stream().collect(Collectors.toMap(ProductEntity::getId, product -> product));
Map<Long, ProductLineEntity> productLineMap = productLines.stream().collect(Collectors.toMap(ProductLineEntity::getId, productLine -> productLine));
Map<Long, Product> productMap = products.stream().collect(Collectors.toMap(Product::getId, product -> product));
Map<Long, ProductLine> productLineMap = productLines.stream().collect(Collectors.toMap(ProductLine::getId, productLine -> productLine));

List<OrderDetailDTO> orderDetailDTOList = orderDetails.stream().map(orderDetail -> {
ProductEntity productEntity = productMap.get(orderDetail.getProductId());
ProductLineEntity productLineEntity = productLineMap.get(orderDetail.getProductLineId());
return OrderDetailDTO.from(orderDetail, productEntity, productLineEntity);
Product product = productMap.get(orderDetail.getProductId());
ProductLine productLine = productLineMap.get(orderDetail.getProductLineId());
return OrderDetailDTO.from(orderDetail, product, productLine);
}).collect(Collectors.toList());

orderResponse.setterOrderDetailList(orderDetailDTOList);
Expand Down
Loading