Implementation of Order feature.#15
Conversation
liliia-ponomarenko
left a comment
There was a problem hiding this comment.
Good job! Let’s improve your solution ;)
| @Size(max = 255, message = "Shipping address must not exceed 255 characters") | ||
| String shippingAddress |
| public record OrderStatusUpdateDto( | ||
| @NotBlank |
There was a problem hiding this comment.
| public record OrderStatusUpdateDto( | |
| @NotBlank | |
| public record OrderStatusUpdateDto( | |
| @NotNull |
| private OrderStatus orderStatus = OrderStatus.CREATED; | ||
| @Column(name = "total", nullable = false) | ||
| private BigDecimal total = BigDecimal.ZERO; | ||
| @Column(name = "order_date", nullable = false) |
There was a problem hiding this comment.
CreationTimestamp
| @ManyToOne | ||
| @JoinColumn(name = "order_id", nullable = false) |
| @ManyToOne | ||
| @JoinColumn(name = "order_id", nullable = false) | ||
| private Order order; | ||
| @ManyToOne |
| if (!orderRepository.existsByIdAndUserId(orderId, userId)) { | ||
| throw new EntityNotFoundException( | ||
| NO_ORDER_FOR_USER_MESSAGE.formatted(userId, orderId)); | ||
| } |
There was a problem hiding this comment.
Do you really need this check
| return orderMapper.toDto(order); | ||
| } | ||
|
|
||
| private void resolveShippingAddress(User user, OrderRequestDto orderRequestDto, |
There was a problem hiding this comment.
Make shippingAddress required field and avoid this check
| return order.getOrderItems().stream() | ||
| .map(orderItem -> orderItem.getPrice() | ||
| .multiply(new BigDecimal(orderItem.getQuantity()))) | ||
| .reduce(BigDecimal::add).orElseThrow(() -> new ArithmeticException( |
There was a problem hiding this comment.
Avoid throwing ArithmeticException
| private void emptyShoppingCart(ShoppingCart shoppingCart) { | ||
| shoppingCart.getCartItems() | ||
| .forEach(item -> item.setShoppingCart(null)); | ||
| shoppingCart.getCartItems().clear(); | ||
|
|
||
| } |
There was a problem hiding this comment.
Create a separate clear shopping cart method in ShoppingCartService
| } | ||
|
|
||
| private ShoppingCart getUsersShoppingCart(Long userId) { | ||
| protected ShoppingCart getUsersShoppingCart(Long userId) { |
| @PatchMapping("/{orderId}") | ||
| public OrderResponseDto updateStatus( | ||
| @PathVariable Long orderId, | ||
| @RequestBody OrderStatusUpdateDto dto |
There was a problem hiding this comment.
| @RequestBody OrderStatusUpdateDto dto | |
| @RequestBody @Valid OrderStatusUpdateDto dto |
|
|
||
| public record OrderRequestDto( | ||
| @Size(max = 255, message = "Shipping address must not exceed 255 characters") | ||
| @NotNull |
There was a problem hiding this comment.
| @NotNull | |
| @NotBlank |
| @CreationTimestamp | ||
| @Column(name = "order_date", nullable = false) | ||
| private LocalDateTime orderDate = LocalDateTime.now(); | ||
| @Column(name = "shipping_address", nullable = false) |
There was a problem hiding this comment.
| @Column(name = "shipping_address", nullable = false) | |
| @Column(nullable = false) |
| @OneToMany(fetch = FetchType.LAZY, mappedBy = "order", | ||
| cascade = CascadeType.ALL, orphanRemoval = true) | ||
| private Set<OrderItem> orderItems = new HashSet<>(); | ||
| @Column(name = "is_deleted", columnDefinition = "TINYINT", nullable = false) |
There was a problem hiding this comment.
| @Column(name = "is_deleted", columnDefinition = "TINYINT", nullable = false) | |
| @Column(columnDefinition = "TINYINT", nullable = false) |
| private LocalDateTime orderDate = LocalDateTime.now(); | ||
| @Column(name = "shipping_address", nullable = false) | ||
| private String shippingAddress; | ||
| @OneToMany(fetch = FetchType.LAZY, mappedBy = "order", |
There was a problem hiding this comment.
| @OneToMany(fetch = FetchType.LAZY, mappedBy = "order", | |
| @OneToMany(mappedBy = "order", |
| @ManyToOne(fetch = FetchType.LAZY) | ||
| @JoinColumn(name = "book_id", nullable = false) | ||
| private Book book; | ||
| @Column(name = "quantity", nullable = false) |
|
|
||
| void createUsersCart(User user); | ||
|
|
||
| default void clearShoppingCart(ShoppingCart shoppingCart) { |
There was a problem hiding this comment.
you can create method in ShoppingCart entity
public void clearCart() {
cartItems.clear();
}
| private final OrderItemMapper orderItemMapper; | ||
| private final ShoppingCartService shoppingCartService; | ||
|
|
||
| public OrderResponseDto createOrder(Long userId, OrderRequestDto orderRequestDto) { |
| public OrderResponseDto createOrder(Long userId, OrderRequestDto orderRequestDto) { | ||
| ShoppingCart shoppingCart = getShoppingCart(userId); | ||
| User owner = shoppingCart.getUser(); | ||
| if (shoppingCartService.isCartEmpty(shoppingCart)) { |
There was a problem hiding this comment.
why do you nee separate method for this logic?
| if (shoppingCartService.isCartEmpty(shoppingCart)) { | |
| if (shoppingCart.getCartItems().isEmpty()) { |
|
|
||
| @Override | ||
| public OrderItemResponseDto getSpecificItem(Long itemId, Long orderId, Long userId) { | ||
| return orderItemRepository.findByIdAndOrderId(itemId, orderId).map(orderItemMapper::toDto) |
There was a problem hiding this comment.
| return orderItemRepository.findByIdAndOrderId(itemId, orderId).map(orderItemMapper::toDto) | |
| return orderItemRepository.findByIdAndOrderIdAndUserId(itemId, orderId).map(orderItemMapper::toDto) |
No description provided.