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 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 @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse

if (authentication != null && authentication.isAuthenticated()) {
String userId = ((JwtAuthenticationToken)authentication).getToken().getSubject();
request.setAttribute("userId", userId);
request.setAttribute("user_id", userId);
}

filterChain.doFilter(request, response);
Expand Down
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 @@ -30,15 +30,15 @@ public class AddressController {
@PostMapping()
public ApiSuccessResponse createAddrss(
@Valid @RequestBody AddressRequest.Create request,
@RequestAttribute("userId") String userAuthId) {
@RequestAttribute("user_id") String userAuthId) {
addressFacade.create(AddressRequest.Create.toDto(request, userAuthId));
return ApiSuccessResponse.success();
}

@PreAuthorize("hasAuthority('ROLE_USER')")
@GetMapping("/{address_id}")
public ApiSuccessResponse<AddressResponse.Get> getAddress(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@PathVariable(name = "address_id") Long addressId) {
return ApiSuccessResponse.success(
AddressResponse.Get.of(addressFacade.get(addressId, userAuthId)));
Expand All @@ -47,7 +47,7 @@ public ApiSuccessResponse<AddressResponse.Get> getAddress(
@PreAuthorize("hasAuthority('ROLE_USER')")
@GetMapping()
public ApiSuccessResponse.PageList<AddressResponse.Get> getAddresses(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size) {
return ApiSuccessResponse.success(AddressResponse.Get.of(addressFacade.getList(userAuthId, page - 1, size)));
Expand All @@ -56,7 +56,7 @@ public ApiSuccessResponse.PageList<AddressResponse.Get> getAddresses(
@PreAuthorize("hasAuthority('ROLE_USER')")
@DeleteMapping("/{address_id}")
public ApiSuccessResponse deleteAddress(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@PathVariable(name = "address_id") Long addressId) {
addressFacade.delete(userAuthId, addressId);
return ApiSuccessResponse.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ public class CartController {

@PostMapping()
public ApiSuccessResponse createCart(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestBody @Valid CartRequest.Create request) {
cartFacade.create(CartRequest.Create.toDto(request, userAuthId));
return ApiSuccessResponse.success();
}

@DeleteMapping()
public ApiSuccessResponse deleteCart(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestBody @Valid CartRequest.Delete request) {
cartFacade.delete(CartRequest.Delete.toDto(request, userAuthId));
return ApiSuccessResponse.success();
}

@PatchMapping()
public ApiSuccessResponse updateCart(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestBody @Valid CartRequest.Update request
) {
cartFacade.update(CartRequest.Update.toDto(request, userAuthId));
Expand All @@ -52,7 +52,7 @@ public ApiSuccessResponse updateCart(

@GetMapping()
public ApiSuccessResponse.PageList<CartResponse.Get> getCart(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestParam(value = "page", defaultValue = "1") int page,
@RequestParam(value = "size", defaultValue = "10") int size
) {
Expand Down
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 All @@ -24,7 +23,7 @@ public class OrderController {

@PostMapping()
public ApiSuccessResponse createOrder(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestBody OrderRequest.Create request
) {
orderFacade.create(OrderRequest.Create.toDto(request, userAuthId));
Expand All @@ -33,7 +32,7 @@ public ApiSuccessResponse createOrder(

@DeleteMapping("/{order_id}")
public ApiSuccessResponse cancelOrder(
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@PathVariable(name = "order_id") Long orderId
) {
orderFacade.cancel(userAuthId, orderId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class PaymentController {
@PostMapping("/{order_id}")
public ApiSuccessResponse createPayment(
@PathVariable("order_id") Long orderId,
@RequestAttribute("userId") String userAuthId,
@RequestAttribute("user_id") String userAuthId,
@RequestBody PaymentRequest.Create request
) {
paymentFacade.createPayment(PaymentRequest.Create.toDto(request, orderId, userAuthId));
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("user_id") String userAuthId,
@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