Skip to content

Commit

Permalink
stable version with fixed checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
fedorovychh committed Jan 3, 2024
1 parent 52067ee commit 1ac826f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public class OrderController {
@PreAuthorize("hasRole('ROLE_USER')")
@PostMapping
@Operation(summary = "Place order", description = "Place order")
public OrderResponseDto placeOrder(@RequestBody OrderRequestDto requestDto) {
return orderService.placeOrder(requestDto);
public OrderResponseDto placeOrder(
Authentication authentication,
@RequestBody OrderRequestDto requestDto
) {
User user = (User) authentication.getPrincipal();
return orderService.placeOrder(user, requestDto);
}

@PreAuthorize("hasRole('ROLE_USER')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
import com.app.bookstore.dto.order.OrderRequestDto;
import com.app.bookstore.dto.order.OrderResponseDto;
import com.app.bookstore.dto.order.UpdateOrderRequestDto;
import com.app.bookstore.model.User;
import java.util.List;

public interface OrderService {
OrderResponseDto placeOrder(OrderRequestDto requestDto);
OrderResponseDto placeOrder(User user, OrderRequestDto requestDto);

List<OrderResponseDto> getAll(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,18 @@
import com.app.bookstore.dto.order.UpdateOrderRequestDto;
import com.app.bookstore.exception.EntityNotFoundException;
import com.app.bookstore.mapper.OrderMapper;
import com.app.bookstore.model.CartItem;
import com.app.bookstore.model.Order;
import com.app.bookstore.model.OrderItem;
import com.app.bookstore.model.ShoppingCart;
import com.app.bookstore.model.User;
import com.app.bookstore.repository.order.OrderRepository;
import com.app.bookstore.repository.shopping.cart.ShoppingCartRepository;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -16,11 +25,13 @@
public class OrderServiceImpl implements OrderService {
private final OrderRepository orderRepository;
private final OrderMapper orderMapper;
private final ShoppingCartRepository shoppingCartRepository;

@Override
public OrderResponseDto placeOrder(OrderRequestDto requestDto) {
Order order = orderMapper.toOrder(requestDto);
return orderMapper.toDto(orderRepository.save(order));
public OrderResponseDto placeOrder(User user, OrderRequestDto requestDto) {
Order order = createOrder(user, requestDto);
Order savedOrder = orderRepository.save(order);
return orderMapper.toDto(savedOrder);
}

@Override
Expand All @@ -39,4 +50,39 @@ public OrderResponseDto updateStatus(Long id, UpdateOrderRequestDto requestDto)
order.setStatus(requestDto.getStatus());
return orderMapper.toDto(orderRepository.save(order));
}

private Order createOrder(User user, OrderRequestDto requestDto) {
Order order = new Order();
order.setUser(user);
order.setStatus(Order.Status.PROCESSING);
order.setShippingAddress(requestDto.getShippingAddress());
Set<OrderItem> orderItems = getOrderItems(user);
order.setOrderItemSet(orderItems);
order.setTotal(
orderItems.stream()
.map(oi -> oi.getPrice().multiply(BigDecimal.valueOf(oi.getQuantity())))
.reduce(BigDecimal.ZERO, BigDecimal::add)
);
order.setOrderDate(LocalDateTime.now());
return order;
}

private Set<OrderItem> getOrderItems(User user) {
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(user.getId()).orElseThrow(
() -> new EntityNotFoundException("Can't find cart by user id: " + user.getId())
);
Set<CartItem> cartItems = shoppingCart.getCartItems();
return cartItems.stream()
.map(this::convertToOrderItem)
.collect(Collectors.toSet());

}

private OrderItem convertToOrderItem(CartItem cartItem) {
OrderItem orderItem = new OrderItem();
orderItem.setBook(cartItem.getBook());
orderItem.setQuantity(cartItem.getQuantity());
orderItem.setPrice(cartItem.getBook().getPrice());
return orderItem;
}
}

0 comments on commit 1ac826f

Please sign in to comment.