Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14] 운송장 번호 입력 #49

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -4,6 +4,8 @@

import org.example.commerce_site.application.order.dto.OrderDetailResponseDto;
import org.example.commerce_site.application.order.dto.OrderRequestDto;
import org.example.commerce_site.common.exception.CustomException;
import org.example.commerce_site.common.exception.ErrorCode;
import org.example.commerce_site.domain.Order;
import org.example.commerce_site.domain.OrderDetail;
import org.example.commerce_site.infrastructure.order.OrderDetailBulkRepository;
Expand All @@ -28,8 +30,15 @@ public void createOrderDetails(List<OrderRequestDto.CreateDetail> details,
orderDetailBulkRepository.saveAll(orderDetails, order.getId());
}

@Transactional
@Transactional(readOnly = true)
public List<OrderDetailResponseDto.Get> getOrderDetails(Long orderId) {
return OrderDetailResponseDto.Get.toDtoList(orderDetailRepository.findAllByOrderId(orderId));
}

@Transactional(readOnly = true)
public OrderDetail getOrderDetail(Long orderDetailId) {
return orderDetailRepository.findById(orderDetailId).orElseThrow(
() -> new CustomException(ErrorCode.ORDER_DETAIL_NOT_FOUND)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public void create(OrderRequestDto.Create dto) {
public void cancel(String userAuthId, Long orderId) {
User user = userService.getUser(userAuthId);
Order order = orderService.getOrder(orderId, user.getId());
if (!OrderStatus.isPhaseCanCancelOrder(order.getStatus())){
if (!OrderStatus.isPhaseCanCancelOrder(order.getStatus())) {
throw new CustomException(ErrorCode.ORDER_ALREADY_SHIPPED);
}
orderService.cancelOrder(order);
orderService.updateStatus(order, OrderStatus.CANCELLED);
List<OrderDetailResponseDto.Get> orderDetails = orderDetailService.getOrderDetails(order.getId());
productService.restoreStockOnCancel(orderDetails);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public Order getOrder(Long orderId, Long userId) {
}

@Transactional
public void cancelOrder(Order order) {
order.updateOrderStatus(OrderStatus.CANCELLED);
public void updateStatus(Order order, OrderStatus orderStatus) {
order.updateOrderStatus(orderStatus);
orderRepository.save(order);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.example.commerce_site.application.shipment;

import org.example.commerce_site.application.order.OrderDetailService;
import org.example.commerce_site.application.order.OrderService;
import org.example.commerce_site.application.partner.PartnerService;
import org.example.commerce_site.application.product.ProductService;
import org.example.commerce_site.application.shipment.dto.ShipmentRequestDto;
import org.example.commerce_site.attribute.OrderStatus;
import org.example.commerce_site.common.exception.CustomException;
import org.example.commerce_site.common.exception.ErrorCode;
import org.example.commerce_site.domain.OrderDetail;
import org.example.commerce_site.domain.Partner;
import org.example.commerce_site.domain.Product;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class ShipmentFacade {
private final OrderService orderService;
private final OrderDetailService orderDetailService;
private final ShipmentService shipmentService;
private final PartnerService partnerService;
private final ProductService productService;

@Transactional
public void updateTrackingCode(ShipmentRequestDto.UpdateTrackingNumber dto) {
OrderDetail orderDetail = orderDetailService.getOrderDetail(dto.getOrderDetailId());
if (orderDetail.getOrder().getStatus() == OrderStatus.CANCELLED) {
throw new CustomException(ErrorCode.ORDER_CAN_NOT_UPDATE_TRACK_CODE);
}

Product product = productService.getProduct(orderDetail.getProductId());
Partner partner = partnerService.getPartner(dto.getUserAuthId());
if (product.getPartnerId() != partner.getId()) {
throw new CustomException(ErrorCode.ACCESS_DENIED);
}

shipmentService.updateTrackingCode(orderDetail.getId(), dto.getTrackingNumber());
orderService.updateStatus(orderDetail.getOrder(), OrderStatus.SHIPPED);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import org.example.commerce_site.application.order.dto.OrderDetailRequestDto;
import org.example.commerce_site.application.order.dto.OrderDetailResponseDto;
import org.example.commerce_site.attribute.ShipmentStatus;
import org.example.commerce_site.common.exception.CustomException;
import org.example.commerce_site.common.exception.ErrorCode;
import org.example.commerce_site.domain.Address;
import org.example.commerce_site.domain.Order;
import org.example.commerce_site.domain.Shipment;
import org.example.commerce_site.infrastructure.shipment.ShipmentBulkRepository;
import org.example.commerce_site.infrastructure.shipment.ShipmentRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -17,6 +20,7 @@
@Service
@RequiredArgsConstructor
public class ShipmentService {
private final ShipmentRepository shipmentRepository;
private final ShipmentBulkRepository shipmentBulkRepository;

@Transactional
Expand All @@ -28,4 +32,14 @@ public void createShipment(Order order, List<OrderDetailResponseDto.Get> orderDe
.status(ShipmentStatus.PENDING)
.build()).toList());
}

@Transactional
public void updateTrackingCode(Long orderDetailId, String trackingNumber) {
Shipment shipment = shipmentRepository.findByOrderDetailId(orderDetailId).orElseThrow(
() -> new CustomException(ErrorCode.SHIPMENT_NOT_FOUND)
);
shipment.updateTrackingNumber(trackingNumber);
shipment.updateStatus(ShipmentStatus.SHIPPED);
shipmentRepository.save(shipment);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.example.commerce_site.application.shipment.dto;

import lombok.Builder;
import lombok.Getter;
import lombok.ToString;

public class ShipmentRequestDto {
@Getter
@Builder
@ToString
public static class UpdateTrackingNumber {
private Long orderDetailId;
private String userAuthId;
private String trackingNumber;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public enum ErrorCode {
//order
ORDER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "주문 정보를 찾을 수 없습니다."),
ORDER_ALREADY_SHIPPED(HttpStatus.BAD_REQUEST, 400, "주문이 이미 배송중입니다."),
ORDER_DETAIL_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "주문 상세 정보를 찾을 수 없습니다."),
ORDER_CAN_NOT_UPDATE_TRACK_CODE(HttpStatus.BAD_REQUEST, 400, "배송 정보를 입력할 수 없는 단계입니다."),

//partner
PARTNER_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "파트너 회원 정보를 찾을 수 없습니다."),
Expand All @@ -41,7 +43,10 @@ public enum ErrorCode {
PRODUCT_OUT_OF_STOCK(HttpStatus.CONFLICT, 409, "상품의 재고가 부족합니다"),

//category
CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "카테고리 정보를 찾을 수 없습니다.");
CATEGORY_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "카테고리 정보를 찾을 수 없습니다."),

//shipment
SHIPMENT_NOT_FOUND(HttpStatus.NOT_FOUND, 404, "배송 정보를 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final int code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,10 @@ public GroupedOpenApi paymentsOpenApi() {
String[] paths = {"/payments/**"};
return GroupedOpenApi.builder().group("PAYMENT API").pathsToMatch(paths).build();
}

@Bean
public GroupedOpenApi shipmentsOpenApi() {
String[] paths = {"/shipments/**"};
return GroupedOpenApi.builder().group("SHIPMENT API").pathsToMatch(paths).build();
}
}
1 change: 1 addition & 0 deletions src/main/java/org/example/commerce_site/domain/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class Order extends BaseTimeEntity {
private Long userId;
private BigDecimal totalAmount;
//TODO : Order status 제거하고 OrderDetail 에 status 를 추가하도록 수정
@Enumerated(EnumType.STRING)
private OrderStatus status;
@OneToMany(mappedBy = "order")
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/example/commerce_site/domain/Shipment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,12 @@ public class Shipment extends BaseTimeEntity {

@Enumerated(EnumType.STRING)
private ShipmentStatus status;

public void updateTrackingNumber(String trackingNumber) {
this.trackingNumber = trackingNumber;
}

public void updateStatus(ShipmentStatus status) {
this.status = status;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.example.commerce_site.infrastructure.shipment;

import java.util.Optional;

import org.example.commerce_site.domain.Shipment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ShipmentRepository extends JpaRepository<Shipment, Long> {
Optional<Shipment> findByOrderDetailId(Long orderDetailId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.example.commerce_site.representation.shipment;

import org.example.commerce_site.application.shipment.ShipmentFacade;
import org.example.commerce_site.common.response.ApiSuccessResponse;
import org.example.commerce_site.representation.shipment.dto.ShipmentRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@PreAuthorize("hasAuthority('ROLE_PARTNER')")
@RequestMapping("/shipments")
public class ShipmentController {
private final ShipmentFacade shipmentFacade;

@PutMapping("/{order_detail_id}")
public ApiSuccessResponse updateShipment(
@PathVariable("order_detail_id") Long orderDetailId,
@RequestAttribute("userId") String userAuthId,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase와 snake_case가 섞여 있네요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

모두 snake_case 로 수정하겠습니다

@RequestBody ShipmentRequest.UpdateTrackingNumber request
) {
shipmentFacade.updateTrackingCode(
ShipmentRequest.UpdateTrackingNumber.toDto(request, userAuthId, orderDetailId));
return ApiSuccessResponse.success();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.example.commerce_site.representation.shipment.dto;

import org.example.commerce_site.application.shipment.dto.ShipmentRequestDto;

import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.ToString;

public class ShipmentRequest {
@Getter
@ToString
public static class UpdateTrackingNumber {
@NotNull
private String trackingNumber;

public static ShipmentRequestDto.UpdateTrackingNumber toDto(ShipmentRequest.UpdateTrackingNumber request,
String userAuthId, Long orderDetailId) {
return ShipmentRequestDto.UpdateTrackingNumber.builder()
.trackingNumber(request.trackingNumber)
.userAuthId(userAuthId)
.orderDetailId(orderDetailId)
.build();
}
}
}
Loading