-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#18 - Feat: 주문 생성 기능 구현(장바구니에서 선택 방식 지원), Style: CartItem member->buy…
…er 변수명 수정
- Loading branch information
Showing
11 changed files
with
278 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...ion/mutbooks/src/main/java/com/example/mutbooks/app/order/controller/OrderController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.mutbooks.app.order.controller; | ||
|
||
import com.example.mutbooks.app.base.security.dto.MemberContext; | ||
import com.example.mutbooks.app.member.entity.Member; | ||
import com.example.mutbooks.app.order.service.OrderService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
@Controller | ||
@RequiredArgsConstructor | ||
@RequestMapping("/order") | ||
public class OrderController { | ||
private final OrderService orderService; | ||
|
||
// 주문 생성 | ||
@PreAuthorize("isAuthenticated()") | ||
@PostMapping("/create") | ||
@ResponseBody | ||
public String createOrder(@AuthenticationPrincipal MemberContext memberContext, String ids) { | ||
Member buyer = memberContext.getMember(); | ||
|
||
orderService.createOrder(buyer, ids); | ||
|
||
return "성공"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...mutbooks/src/main/java/com/example/mutbooks/app/order/repository/OrderItemRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.mutbooks.app.order.repository; | ||
|
||
import com.example.mutbooks.app.order.entity.OrderItem; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OrderItemRepository extends JpaRepository<OrderItem, Long> { | ||
} |
7 changes: 7 additions & 0 deletions
7
...ion/mutbooks/src/main/java/com/example/mutbooks/app/order/repository/OrderRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.example.mutbooks.app.order.repository; | ||
|
||
import com.example.mutbooks.app.order.entity.Order; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface OrderRepository extends JpaRepository<Order, Long> { | ||
} |
69 changes: 69 additions & 0 deletions
69
...k_Mission/mutbooks/src/main/java/com/example/mutbooks/app/order/service/OrderService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.example.mutbooks.app.order.service; | ||
|
||
import com.example.mutbooks.app.cart.entity.CartItem; | ||
import com.example.mutbooks.app.cart.service.CartService; | ||
import com.example.mutbooks.app.member.entity.Member; | ||
import com.example.mutbooks.app.order.entity.Order; | ||
import com.example.mutbooks.app.order.entity.OrderItem; | ||
import com.example.mutbooks.app.order.repository.OrderItemRepository; | ||
import com.example.mutbooks.app.order.repository.OrderRepository; | ||
import com.example.mutbooks.app.product.entity.Product; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class OrderService { | ||
private final CartService cartService; | ||
private final OrderItemRepository orderItemRepository; | ||
private final OrderRepository orderRepository; | ||
|
||
// 선택한 장바구니 품목으로부터 주문 생성 | ||
@Transactional | ||
public Order createOrder(Member buyer, String ids) { | ||
// 주문 생성해야하는 cartItem id 리스트 | ||
String[] idsArr = ids.split(","); | ||
// Array -> List | ||
List<Long> cartItemIds = Arrays.stream(idsArr) | ||
.mapToLong(Long::parseLong) | ||
.boxed() | ||
.collect(Collectors.toList()); | ||
// 1. 주문 생성해야하는 CartItem 조회 | ||
List<CartItem> cartItems = cartService.findByBuyerAndIdInOrderByIdDesc(buyer, cartItemIds); | ||
|
||
// 2. 주문 품목 리스트 생성 | ||
List<OrderItem> orderItems = new ArrayList<>(); | ||
for(CartItem cartItem : cartItems) { | ||
// 2-1. 상품으로부터 주문 품목 생성 | ||
Product product = cartItem.getProduct(); | ||
orderItems.add(new OrderItem(product)); // Order 정보는 비어있는 상태에서 먼저 생성 | ||
// 2-2. 장바구니 품목 삭제 | ||
cartService.deleteCartItem(buyer, product); | ||
} | ||
|
||
// 3. 주문 생성 | ||
return create(buyer, orderItems); | ||
} | ||
|
||
@Transactional | ||
public Order create(Member buyer, List<OrderItem> orderItems) { | ||
Order order = Order.builder() | ||
.buyer(buyer) | ||
.build(); | ||
|
||
for(OrderItem orderItem : orderItems) { | ||
order.addOrderItem(orderItem); | ||
} | ||
order.makeName(); | ||
orderRepository.save(order); | ||
|
||
return order; | ||
} | ||
} |
Oops, something went wrong.