Skip to content

Implementation of Order feature.#15

Merged
SnkvS merged 3 commits into
mainfrom
order-feature
Jun 2, 2025
Merged

Implementation of Order feature.#15
SnkvS merged 3 commits into
mainfrom
order-feature

Conversation

@SnkvS

@SnkvS SnkvS commented May 29, 2025

Copy link
Copy Markdown
Owner

No description provided.

@liliia-ponomarenko liliia-ponomarenko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job! Let’s improve your solution ;)

Comment on lines +6 to +7
@Size(max = 255, message = "Shipping address must not exceed 255 characters")
String shippingAddress

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NotNull

Comment on lines +6 to +7
public record OrderStatusUpdateDto(
@NotBlank

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CreationTimestamp

Comment on lines +27 to +28
@ManyToOne
@JoinColumn(name = "order_id", nullable = false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FetchType.LAZY

@ManyToOne
@JoinColumn(name = "order_id", nullable = false)
private Order order;
@ManyToOne

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the same

Comment on lines +84 to +87
if (!orderRepository.existsByIdAndUserId(orderId, userId)) {
throw new EntityNotFoundException(
NO_ORDER_FOR_USER_MESSAGE.formatted(userId, orderId));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you really need this check

return orderMapper.toDto(order);
}

private void resolveShippingAddress(User user, OrderRequestDto orderRequestDto,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid throwing ArithmeticException

Comment on lines +150 to +155
private void emptyShoppingCart(ShoppingCart shoppingCart) {
shoppingCart.getCartItems()
.forEach(item -> item.setShoppingCart(null));
shoppingCart.getCartItems().clear();

}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a separate clear shopping cart method in ShoppingCartService

}

private ShoppingCart getUsersShoppingCart(Long userId) {
protected ShoppingCart getUsersShoppingCart(Long userId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why protected?

@Elena-Bruyako Elena-Bruyako left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments

@PatchMapping("/{orderId}")
public OrderResponseDto updateStatus(
@PathVariable Long orderId,
@RequestBody OrderStatusUpdateDto dto

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@RequestBody OrderStatusUpdateDto dto
@RequestBody @Valid OrderStatusUpdateDto dto


public record OrderRequestDto(
@Size(max = 255, message = "Shipping address must not exceed 255 characters")
@NotNull

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@NotNull
@NotBlank

@CreationTimestamp
@Column(name = "order_date", nullable = false)
private LocalDateTime orderDate = LocalDateTime.now();
@Column(name = "shipping_address", nullable = false)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


void createUsersCart(User user);

default void clearShoppingCart(ShoppingCart shoppingCart) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add Override

public OrderResponseDto createOrder(Long userId, OrderRequestDto orderRequestDto) {
ShoppingCart shoppingCart = getShoppingCart(userId);
User owner = shoppingCart.getUser();
if (shoppingCartService.isCartEmpty(shoppingCart)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you nee separate method for this logic?

Suggested change
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return orderItemRepository.findByIdAndOrderId(itemId, orderId).map(orderItemMapper::toDto)
return orderItemRepository.findByIdAndOrderIdAndUserId(itemId, orderId).map(orderItemMapper::toDto)

@SnkvS SnkvS merged commit d71081c into main Jun 2, 2025
2 checks passed
@SnkvS SnkvS deleted the order-feature branch June 2, 2025 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants