Skip to content

Conversation

@trokhim03
Copy link
Owner

No description provided.

Copy link

@Elena-Bruyako Elena-Bruyako left a comment

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

description = "Retrieves a list of orders for the currently authenticated user.")
@GetMapping
public List<OrderResponseDto> getOrders(Authentication authentication) {
return orderService.getOrderByUser((User) authentication.getPrincipal());

Choose a reason for hiding this comment

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

You can create separate method for returning Long userId instead of providing
(User) authentication.getPrincipal() to service

Check other places

description = "Retrieves a specific item from an order "
+ "by itemId, within the order identified by orderId.")
@GetMapping("/{orderId}/items/{itemId}")
public OrderItemResponseDto getOrderItem(@PathVariable Long orderId,

Choose a reason for hiding this comment

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

provide Authentication

List<OrderItem> findAllByOrder(Order order);

Optional<OrderItem> findByIdAndOrderId(Long orderItemId, Long orderId);

Choose a reason for hiding this comment

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

remove empty line

Suggested change

private final ShoppingCartRepository shoppingCartRepository;

@Override
@Transactional

Choose a reason for hiding this comment

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

replace under service

+ " for user with id: " + user.getId()
));
if (shoppingCart.getCartItems().isEmpty()) {
throw new EntityNotFoundException("Shopping cart is empty "

Choose a reason for hiding this comment

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

Suggested change
throw new EntityNotFoundException("Shopping cart is empty "
throw new OrderProcessingException("Shopping cart is empty "

public List<OrderItemResponseDto> getOrderItemsByOrderId(Long orderId, Long userId) {
Order order = orderRepository
.findByIdAndUserId(orderId, userId)
.orElseThrow(() -> new EntityNotFoundException("Order items not found "

Choose a reason for hiding this comment

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

wrong message, here you try to find order

.orElseThrow(() -> new EntityNotFoundException("Order items not found "
+ "for order id: " + orderId + " and user id: " + userId
));
return orderItemMapper.toDto(orderItemRepository.findAllByOrder(order));

Choose a reason for hiding this comment

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

create List<OrderItemDto> toOrderItemDtoList(List<OrderItem> orderItems); in OrderItemMapper

Suggested change
return orderItemMapper.toDto(orderItemRepository.findAllByOrder(order));
return orderItemMapper.toOrderItemDtoList(order.getOrderItems());


if (existingCartItem.isPresent()) {
CartItem cartItem = existingCartItem.get();
CartItem cartItem = existingCartItem.orElseThrow();

Choose a reason for hiding this comment

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

is you use orElseThrow() it's better to provide exception with informative message and parameter

Copy link
Owner Author

Choose a reason for hiding this comment

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

@JJJazl wrote me that "It is recommended to use orElseThrow() instead of `get()'. You can also see this in the java doc"

Copy link

Choose a reason for hiding this comment

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

You can use get() or just orElseThrow() here, since above you are checking that the value is present

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.hibernate.ddl-auto=validate
spring.jpa.hibernate.ddl-auto=update

Choose a reason for hiding this comment

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

revert to validate

name: shipping_address
type: varchar(255)
constraints:
nullable: false

Choose a reason for hiding this comment

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

add isDeleted

Copy link

@JJJazl JJJazl left a comment

Choose a reason for hiding this comment

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

Great! Let's move on)

Comment on lines +52 to +54
order.setUser(userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("User with id "
+ userId + " not found")));
Copy link

Choose a reason for hiding this comment

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

You can pass User user directly to the service method to avoid additional SELECT query


if (existingCartItem.isPresent()) {
CartItem cartItem = existingCartItem.get();
CartItem cartItem = existingCartItem.orElseThrow();
Copy link

Choose a reason for hiding this comment

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

You can use get() or just orElseThrow() here, since above you are checking that the value is present

@trokhim03 trokhim03 merged commit 0d47a7d into main Mar 6, 2025
2 checks passed
@trokhim03 trokhim03 deleted the pr-12 branch March 6, 2025 11:33
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.

4 participants