Skip to content
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

Merged
merged 5 commits into from
Jul 27, 2024
Merged

implemented order model #12

merged 5 commits into from
Jul 27, 2024

Conversation

TarasYashchuk
Copy link
Owner

No description provided.

Comment on lines 39 to 71
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);

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;

Choose a reason for hiding this comment

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

Suggested change

Comment on lines 86 to 94
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;
}

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?

Copy link
Owner Author

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.

Comment on lines 109 to 110
private BigDecimal calculateTotalAndAddOrderItems(ShoppingCart shoppingCart, Order order) {
BigDecimal total = BigDecimal.ZERO;

Choose a reason for hiding this comment

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

no Single Responsibility method

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.

see comments

@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "404", description = "Order not found")
})
public void updateOrderStatus(

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,

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,

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 {

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 {

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"));

Choose a reason for hiding this comment

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

same

Comment on lines 91 to 94
order.setStatus(Order.Status.PENDING);
order.setOrderDate(LocalDateTime.now());
order.setShippingAddress(shippingAddress);
order.setDeleted(false);

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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

Choose a reason for hiding this comment

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

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

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(....

@TarasYashchuk TarasYashchuk merged commit e294193 into master Jul 27, 2024
2 checks passed
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