-
Notifications
You must be signed in to change notification settings - Fork 0
added order model #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Elena-Bruyako
left a comment
There was a problem hiding this 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()); |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove empty line
| private final ShoppingCartRepository shoppingCartRepository; | ||
|
|
||
| @Override | ||
| @Transactional |
There was a problem hiding this comment.
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 " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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 " |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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
| return orderItemMapper.toDto(orderItemRepository.findAllByOrder(order)); | |
| return orderItemMapper.toOrderItemDtoList(order.getOrderItems()); |
|
|
||
| if (existingCartItem.isPresent()) { | ||
| CartItem cartItem = existingCartItem.get(); | ||
| CartItem cartItem = existingCartItem.orElseThrow(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add isDeleted
JJJazl
left a comment
There was a problem hiding this 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)
| order.setUser(userRepository.findById(userId) | ||
| .orElseThrow(() -> new EntityNotFoundException("User with id " | ||
| + userId + " not found"))); |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
No description provided.