Skip to content

Commit c9e3fed

Browse files
authored
Merge pull request #110 from IT-Cotato/feature/108
feat:주문 삭제 기능 추가, 가격 계산 로직 변경
2 parents 2db0841 + 8dcdab6 commit c9e3fed

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

src/main/java/com/ongil/backend/domain/order/controller/OrderController.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.data.domain.Sort;
88
import org.springframework.format.annotation.DateTimeFormat;
99
import org.springframework.security.core.annotation.AuthenticationPrincipal;
10+
import org.springframework.web.bind.annotation.DeleteMapping;
1011
import org.springframework.web.bind.annotation.GetMapping;
1112
import org.springframework.web.bind.annotation.PatchMapping;
1213
import org.springframework.web.bind.annotation.PathVariable;
@@ -83,7 +84,7 @@ public DataResponse<OrderHistoryResponse> getOrderHistory(
8384
@PostMapping
8485
@Operation(
8586
summary = "상품 화면에서 바로 상품 주문",
86-
description = "장바구니를 거치지 않고 개별 상품을 즉시 주문합니다. 성공 시 생성된 주문 ID(orderId)를 반환합니다."
87+
description = "장바구니를 거치지 않고 개별 상품을 즉시 주문합니다. 성공 시 생성된 주문 ID(orderId)를 반환합니다. 토큰 필요"
8788
)
8889
public DataResponse<Long> createOrder(
8990
@AuthenticationPrincipal Long userId, @RequestBody @Valid OrderCreateRequest request
@@ -97,7 +98,7 @@ public DataResponse<Long> createOrder(
9798
summary = "장바구니 상품 주문",
9899
description = "장바구니에서 선택한 하나 이상의 상품들을 한 번에 주문합니다. " +
99100
"cartItemIds 필드에 장바구니 항목 ID 리스트(예: [3, 4])를 담아 보냅니다. " +
100-
"성공 시 생성된 주문 ID(orderId)를 반환하며, 해당 장바구니 항목들은 삭제됩니다."
101+
"성공 시 생성된 주문 ID(orderId)를 반환하며, 해당 장바구니 항목들은 삭제됩니다. 토큰 필요"
101102
)
102103
public DataResponse<Long> createOrderFromCart(
103104
@AuthenticationPrincipal Long userId, @RequestBody @Valid CartOrderRequest request
@@ -107,7 +108,7 @@ public DataResponse<Long> createOrderFromCart(
107108
}
108109

109110
@GetMapping("/{orderId}")
110-
@Operation(summary = "주문 상세 조회", description = "orderId를 통한 주문 상세 조회")
111+
@Operation(summary = "주문 상세 조회", description = "orderId를 통한 주문 상세 조회. 토큰 필요")
111112
public DataResponse<OrderDetailResponse> getOrderDetail(
112113
@AuthenticationPrincipal Long userId, @PathVariable Long orderId
113114
) {
@@ -139,6 +140,18 @@ public DataResponse<OrderCancelResponse> cancelOrder(
139140
return DataResponse.from(orderService.cancelOrder(userId, orderId, request));
140141
}
141142

143+
@DeleteMapping("/{orderId}")
144+
@Operation(
145+
summary = "주문 내역 삭제",
146+
description = "주문 내역을 삭제합니다. 구매 확정 또는 취소된 주문만 삭제 가능합니다. 토큰 필요"
147+
)
148+
public DataResponse<String> deleteOrder(
149+
@AuthenticationPrincipal Long userId, @PathVariable Long orderId
150+
) {
151+
orderService.deleteOrder(userId, orderId);
152+
return DataResponse.from("주문 내역이 삭제되었습니다.");
153+
}
154+
142155
@PatchMapping("/{orderId}/delivery-address")
143156
@Operation(
144157
summary = "주문 배송지 변경",

src/main/java/com/ongil/backend/domain/order/converter/OrderConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Order toOrder(OrderCreateRequest request, User user, int finalAmount) {
5353
public OrderItem toOrderItem(OrderItemRequest itemRequest, Product product) {
5454
return OrderItem.builder()
5555
.product(product)
56-
.priceAtOrder(product.getPrice())
56+
.priceAtOrder(product.getEffectivePrice())
5757
.selectedSize(itemRequest.selectedSize())
5858
.selectedColor(itemRequest.selectedColor())
5959
.quantity(itemRequest.quantity())

src/main/java/com/ongil/backend/domain/order/service/OrderService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Long processPayment(Long userId, OrderCreateRequest request) {
7777
Product product = productRepository.findById(itemRequest.productId())
7878
.orElseThrow(() -> new AppException(ErrorCode.PRODUCT_NOT_FOUND));
7979

80-
totalProductPrice += product.getPrice() * itemRequest.quantity();
80+
totalProductPrice += product.getEffectivePrice() * itemRequest.quantity();
8181

8282
OrderItem orderItem = orderConverter.toOrderItem(itemRequest, product);
8383
orderItems.add(orderItem);
@@ -225,6 +225,17 @@ public OrderDetailResponse updateDeliveryAddress(Long userId, Long orderId, Deli
225225
return orderConverter.toDetailResponse(order, itemDtos);
226226
}
227227

228+
@Transactional
229+
public void deleteOrder(Long userId, Long orderId) {
230+
Order order = getOrderAndValidateOwner(userId, orderId);
231+
232+
if (order.getOrderStatus() != OrderStatus.CONFIRMED && order.getOrderStatus() != OrderStatus.CANCELED) {
233+
throw new AppException(ErrorCode.ORDER_DELETE_NOT_ALLOWED);
234+
}
235+
236+
orderRepository.delete(order);
237+
}
238+
228239
private Order getOrderAndValidateOwner(Long userId, Long orderId) {
229240
Order order = orderRepository.findById(orderId)
230241
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.ORDER_NOT_FOUND));

src/main/java/com/ongil/backend/global/common/exception/ErrorCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public enum ErrorCode {
6868
ORDER_CANCEL_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "주문 접수 상태에서만 취소가 가능합니다.", "ORDER-005"),
6969
ORDER_ALREADY_CANCELED(HttpStatus.BAD_REQUEST, "이미 취소된 주문입니다.", "ORDER-006"),
7070
ORDER_UPDATE_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "주문 접수 상태에서만 변경이 가능합니다.", "ORDER-007"),
71+
ORDER_DELETE_NOT_ALLOWED(HttpStatus.BAD_REQUEST, "구매 확정 또는 취소된 주문만 삭제할 수 있습니다.", "ORDER-008"),
7172

7273
// ADDRESS
7374
ADDRESS_NOT_FOUND(HttpStatus.NOT_FOUND, "배송지 정보를 찾을 수 없습니다.", "ADDRESS-001"),

0 commit comments

Comments
 (0)