-
Notifications
You must be signed in to change notification settings - Fork 0
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
implemented order model #12
Conversation
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId); | ||
if (shoppingCart == null || shoppingCart.getCartItems().isEmpty()) { | ||
throw new EntityNotFoundException("Shopping cart is empty"); | ||
} | ||
|
||
User user = userRepository.findById(userId) | ||
.orElseThrow(() -> new EntityNotFoundException("User not found")); | ||
|
||
Order order = makeOrder(shippingAddress, user); | ||
|
||
BigDecimal total = BigDecimal.ZERO; | ||
|
||
for (CartItem cartItem : shoppingCart.getCartItems()) { | ||
OrderItem orderItem = new OrderItem(); | ||
orderItem.setOrder(order); | ||
orderItem.setBook(cartItem.getBook()); | ||
orderItem.setQuantity(cartItem.getQuantity()); | ||
orderItem.setPrice(cartItem.getBook().getPrice() | ||
.multiply(BigDecimal.valueOf(cartItem.getQuantity()))); | ||
orderItem.setDeleted(false); | ||
order.getOrderItems().add(orderItem); | ||
|
||
total = total.add(orderItem.getPrice()); | ||
} | ||
|
||
order.setTotal(total); | ||
orderRepository.save(order); | ||
orderItemRepository.saveAll(order.getOrderItems()); | ||
|
||
shoppingCart.getCartItems().clear(); | ||
shoppingCartRepository.save(shoppingCart); | ||
|
||
return orderMapper.toDto(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.
let's divide this method into private helpful
@ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
@JoinColumn(name = "book_id", nullable = false) | ||
private Book book; | ||
|
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.
private static Order makeOrder(String shippingAddress, User user) { | ||
Order order = new Order(); | ||
order.setUser(user); | ||
order.setStatus(Order.Status.PENDING); | ||
order.setOrderDate(LocalDateTime.now()); | ||
order.setShippingAddress(shippingAddress); | ||
order.setDeleted(false); | ||
return 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.
why do you need static?
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.
i used "extract method" from intellij and this func makes methods static by default, i just do not noticed that.
private BigDecimal calculateTotalAndAddOrderItems(ShoppingCart shoppingCart, Order order) { | ||
BigDecimal total = BigDecimal.ZERO; |
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.
no Single Responsibility method
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.
see comments
@ApiResponse(responseCode = "401", description = "Unauthorized"), | ||
@ApiResponse(responseCode = "404", description = "Order not found") | ||
}) | ||
public void updateOrderStatus( |
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.
Update endpoint should return dto
import java.util.Set; | ||
|
||
public record OrderDto( | ||
@NotNull Long id, |
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.
Do we need validate response?
import java.math.BigDecimal; | ||
|
||
public record OrderItemDto(@NotNull Long id, | ||
@NotNull 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.
same
import lombok.Data; | ||
|
||
@Data | ||
public class PlaceOrderRequestDto { |
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.
Why is not record?
import mate.academy.model.Order; | ||
|
||
@Data | ||
public class UpdateOrderStatusRequestDto { |
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.
same
@Override | ||
public void updateOrderStatus(Long orderId, Order.Status status) { | ||
Order order = orderRepository.findById(orderId) | ||
.orElseThrow(() -> new EntityNotFoundException("Order 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.
same
order.setStatus(Order.Status.PENDING); | ||
order.setOrderDate(LocalDateTime.now()); | ||
order.setShippingAddress(shippingAddress); | ||
order.setDeleted(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.
order.setStatus(Order.Status.PENDING); | |
order.setOrderDate(LocalDateTime.now()); | |
order.setShippingAddress(shippingAddress); | |
order.setDeleted(false); | |
order.setShippingAddress(shippingAddress); |
} | ||
|
||
private ShoppingCart getShoppingCart(Long userId) { | ||
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId); |
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.
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId); | |
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId).orElseThrow(... |
|
||
private ShoppingCart getShoppingCart(Long userId) { | ||
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId); | ||
if (shoppingCart == null || shoppingCart.getCartItems().isEmpty()) { |
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.
if (shoppingCart == null || shoppingCart.getCartItems().isEmpty()) { | |
if (shoppingCart.getCartItems().isEmpty()) { |
private ShoppingCart getShoppingCart(Long userId) { | ||
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId); | ||
if (shoppingCart == null || 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(.... |
No description provided.