Skip to content

Commit

Permalink
Merge pull request #87 from Uni-Made/feature/#86
Browse files Browse the repository at this point in the history
๐Ÿ› [BUG] ํ…Œ์ŠคํŠธ ์œ„ํ•ด ํ† ํฐ ์ •๋ณด ์ œ๊ฑฐ
  • Loading branch information
moonyaeyoon authored Jul 27, 2024
2 parents 486e0f2 + 8e0927c commit dd9f455
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ public interface BuyerRepository extends JpaRepository<Buyer, Long> {
Buyer findBySocialIdAndProvider(String socialId, Provider provider);

Optional<Buyer> findBySocialId(String socialId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import umc.unimade.domain.accounts.entity.Buyer;
import umc.unimade.domain.accounts.repository.BuyerRepository;
import umc.unimade.domain.orders.dto.*;
import umc.unimade.domain.accounts.exception.UserExceptionHandler;
import umc.unimade.domain.orders.dto.OrderRequest;
Expand All @@ -31,13 +32,14 @@
public class OrderController {
private final OrderCommandService orderCommandService;
private final OrderQueryService orderQueryService;
private final BuyerRepository buyerRepository;

@Tag(name = "Order", description = "๊ตฌ๋งค ๊ด€๋ จ API")
@Operation(summary = "์ƒํ’ˆ ๊ตฌ๋งคํ•˜๊ธฐ")
@PostMapping("/{productId}")
public ResponseEntity<ApiResponse<OrderResponse>> createOrder (@AuthenticationPrincipal Buyer currentBuyer, @PathVariable Long productId, @RequestBody OrderRequest orderRequest){
@PostMapping("/{productId}/{buyerId}")
public ResponseEntity<ApiResponse<OrderResponse>> createOrder (@PathVariable Long productId, @PathVariable Long buyerId, @RequestBody OrderRequest orderRequest){
try {
return ResponseEntity.ok(ApiResponse.onSuccess(orderCommandService.createOrder(productId, currentBuyer, orderRequest)));
return ResponseEntity.ok(ApiResponse.onSuccess(orderCommandService.createOrder(productId, buyerId, orderRequest)));
}
catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ApiResponse.onFailure(HttpStatus.BAD_REQUEST.name(), e.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import umc.unimade.domain.accounts.entity.Buyer;
import umc.unimade.domain.accounts.exception.UserExceptionHandler;
import umc.unimade.domain.accounts.repository.BuyerRepository;
import umc.unimade.domain.orders.dto.OrderRequest;
import umc.unimade.domain.orders.dto.OrderResponse;
import umc.unimade.domain.orders.entity.*;
Expand All @@ -30,12 +32,15 @@ public class OrderCommandService {
private final ProductRepository productRepository;
private final OptionValueRepository optionValueRepository;
private final OrderItemRepository orderItemRepository;
private final BuyerRepository buyerRepository;

@Transactional
public OrderResponse createOrder(Long productId, Buyer buyer, OrderRequest orderRequest) {
public OrderResponse createOrder(Long productId, Long buyerId, OrderRequest orderRequest) {
if (!orderRequest.getPurchaseForm().getIsAgree()) {
throw new OrderExceptionHandler(ErrorCode.ORDER_AGREEMENT_REQUIRED);
}

Buyer buyer = findBuyerById(buyerId);
Products product = findProductById(productId);

// PurchaseForm ์—”ํ‹ฐํ‹ฐ ์ƒ์„ฑ ๋ฐ ์ €์žฅ
Expand Down Expand Up @@ -71,6 +76,10 @@ public OrderResponse createOrder(Long productId, Buyer buyer, OrderRequest order
return OrderResponse.from(order, product, totalPrice);
}

private Buyer findBuyerById(Long buyerId) {
return buyerRepository.findById(buyerId)
.orElseThrow(() -> new UserExceptionHandler(ErrorCode.BUYER_NOT_FOUND));
}
private Products findProductById(Long productId){
return productRepository.findById(productId)
.orElseThrow(() -> new ProductsExceptionHandler(ErrorCode.PRODUCT_NOT_FOUND));
Expand Down

0 comments on commit dd9f455

Please sign in to comment.