Skip to content

Commit

Permalink
applied suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
TarasYashchuk committed Jul 26, 2024
1 parent f14f181 commit fb65d91
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 63 deletions.
6 changes: 3 additions & 3 deletions src/main/java/mate/academy/controller/OrderController.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public OrderDto placeOrder(
@AuthenticationPrincipal User userDetails,
@RequestBody @Parameter(description = "Details for placing an order", required = true)
PlaceOrderRequestDto requestDto) {
return orderService.createOrder(userDetails.getId(), requestDto.getShippingAddress());
return orderService.createOrder(userDetails.getId(), requestDto.shippingAddress());
}

@PreAuthorize("hasRole('USER')")
Expand Down Expand Up @@ -111,12 +111,12 @@ public OrderItemDto getOrderItem(
@ApiResponse(responseCode = "401", description = "Unauthorized"),
@ApiResponse(responseCode = "404", description = "Order not found")
})
public void updateOrderStatus(
public OrderDto updateOrderStatus(
@PathVariable
@Parameter(description = "ID of the order to update", required = true) Long orderId,
@RequestBody
@Parameter(description = "Details for updating the order status", required = true)
UpdateOrderStatusRequestDto requestDto) {
orderService.updateOrderStatus(orderId, requestDto.getStatus());
return orderService.updateOrderStatus(orderId, requestDto.status().name());
}
}
17 changes: 7 additions & 10 deletions src/main/java/mate/academy/dto/order/OrderDto.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package mate.academy.dto.order;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Set;

public record OrderDto(
@NotNull Long id,
@NotNull Long userId,
@NotBlank String status,
@NotNull @Positive BigDecimal total,
@NotNull LocalDateTime orderDate,
@NotBlank String shippingAddress,
@NotNull Set<OrderItemDto> orderItems
Long id,
Long userId,
String status,
BigDecimal total,
LocalDateTime orderDate,
String shippingAddress,
Set<OrderItemDto> orderItems
) {
}
13 changes: 6 additions & 7 deletions src/main/java/mate/academy/dto/order/OrderItemDto.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package mate.academy.dto.order;

import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.math.BigDecimal;

public record OrderItemDto(@NotNull Long id,
@NotNull Long orderId,
@NotNull Long bookId,
@Positive int quantity,
@Positive BigDecimal price) {
public record OrderItemDto(
Long id,
Long orderId,
Long bookId,
int quantity,
BigDecimal price) {
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package mate.academy.dto.order;

import jakarta.validation.constraints.NotBlank;
import lombok.Data;

@Data
public class PlaceOrderRequestDto {
@NotBlank
private String shippingAddress;
public record PlaceOrderRequestDto(
@NotBlank String shippingAddress
) {
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package mate.academy.dto.order;

import jakarta.validation.constraints.NotNull;
import lombok.Data;
import mate.academy.model.Order;

@Data
public class UpdateOrderStatusRequestDto {
@NotNull
private Order.Status status;
public record UpdateOrderStatusRequestDto(
@NotNull Order.Status status
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package mate.academy.exception;

public class OrderProcessingException extends RuntimeException {
public OrderProcessingException(String message) {
super(message);
}
}
4 changes: 2 additions & 2 deletions src/main/java/mate/academy/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public class Order {
private User user;
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Status status;
private Status status = Status.PENDING;
@Column(nullable = false)
private BigDecimal total;
@Column(nullable = false)
private LocalDateTime orderDate;
private LocalDateTime orderDate = LocalDateTime.now();
@Column(nullable = false)
private String shippingAddress;
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mate.academy.repository.shoppingcart;

import java.util.Optional;
import mate.academy.model.ShoppingCart;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -8,5 +9,5 @@
@Repository
public interface ShoppingCartRepository extends JpaRepository<ShoppingCart, Long> {
@EntityGraph(attributePaths = {"cartItems", "cartItems.book"})
ShoppingCart findByUserId(Long userId);
Optional<ShoppingCart> findByUserId(Long userId);
}
3 changes: 1 addition & 2 deletions src/main/java/mate/academy/service/OrderService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import mate.academy.dto.order.OrderDto;
import mate.academy.dto.order.OrderItemDto;
import mate.academy.model.Order;
import org.springframework.data.domain.Pageable;

public interface OrderService {
Expand All @@ -15,5 +14,5 @@ public interface OrderService {

OrderItemDto getOrderItem(Long orderId, Long itemId);

void updateOrderStatus(Long orderId, Order.Status status);
OrderDto updateOrderStatus(Long orderId, String status);
}
33 changes: 15 additions & 18 deletions src/main/java/mate/academy/service/impl/OrderServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package mate.academy.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
import lombok.RequiredArgsConstructor;
import mate.academy.dto.order.OrderDto;
import mate.academy.dto.order.OrderItemDto;
import mate.academy.exception.EntityNotFoundException;
import mate.academy.exception.OrderProcessingException;
import mate.academy.mapper.OrderItemMapper;
import mate.academy.mapper.OrderMapper;
import mate.academy.model.CartItem;
Expand Down Expand Up @@ -52,55 +52,52 @@ public OrderDto createOrder(Long userId, String shippingAddress) {
return orderMapper.toDto(order);
}

@Transactional(readOnly = true)
@Override
public List<OrderDto> getOrderHistory(Long userId, Pageable pageable) {
return orderRepository.findByUserId(userId, pageable).stream()
.map(orderMapper::toDto)
.toList();
}

@Transactional(readOnly = true)
@Override
public List<OrderItemDto> getOrderItems(Long orderId, Pageable pageable) {
return orderItemRepository.findByOrderId(orderId, pageable).stream()
.map(orderItemMapper::toDto)
.toList();
}

@Transactional(readOnly = true)
@Override
public OrderItemDto getOrderItem(Long orderId, Long itemId) {
OrderItem orderItem = orderItemRepository.findByOrderIdAndId(orderId, itemId)
.orElseThrow(() -> new EntityNotFoundException("Order item not found"));
.orElseThrow(
() -> new OrderProcessingException("Order item with id " + itemId
+ " not found"));
return orderItemMapper.toDto(orderItem);
}

@Transactional
@Override
public void updateOrderStatus(Long orderId, Order.Status status) {
public OrderDto updateOrderStatus(Long orderId, String status) {
Order order = orderRepository.findById(orderId)
.orElseThrow(() -> new EntityNotFoundException("Order not found"));
order.setStatus(status);
orderRepository.save(order);
.orElseThrow(
() -> new OrderProcessingException("Order with id " + orderId
+ " not found"));

order.setStatus(Order.Status.valueOf(status));
Order updatedOrder = orderRepository.save(order);

return orderMapper.toDto(updatedOrder);
}

private 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;
}

private ShoppingCart getShoppingCart(Long userId) {
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId);
if (shoppingCart == null || shoppingCart.getCartItems().isEmpty()) {
throw new EntityNotFoundException("Shopping cart is empty");
}
return shoppingCart;
return shoppingCartRepository.findByUserId(userId)
.orElseThrow(() -> new OrderProcessingException("Shopping cart is empty"));
}

private User getUser(Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,15 @@ public class ShoppingCartServiceImpl implements ShoppingCartService {
private final UserRepository userRepository;

public ShoppingCartDto getShoppingCartForUser(Long userId) {
return shoppingCartMapper.toDto(shoppingCartRepository.findByUserId(userId));
return shoppingCartMapper.toDto(shoppingCartRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException("Cant get shopping cart for"
+ " user with id " + userId)));
}

@Transactional
public ShoppingCartDto addToCart(Long userId, Long bookId, int quantity) {
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId);
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException("Cant add book with id " + bookId));
if (shoppingCart == null) {
shoppingCart = createNewShoppingCart(userId);
}
Expand All @@ -57,10 +60,9 @@ public ShoppingCartDto addToCart(Long userId, Long bookId, int quantity) {

@Transactional
public ShoppingCartDto updateCartItemQuantity(Long cartItemId, Long userId, int quantity) {
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId);
if (shoppingCart == null) {
throw new EntityNotFoundException("Shopping cart not found for user id: " + userId);
}
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException("Shopping cart not found for "
+ "user id: " + userId));

CartItem cartItem = cartItemRepository.findByIdAndShoppingCartId(cartItemId,
shoppingCart.getId())
Expand All @@ -74,10 +76,9 @@ public ShoppingCartDto updateCartItemQuantity(Long cartItemId, Long userId, int

@Transactional
public void removeCartItem(Long cartItemId, Long userId) {
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId);
if (shoppingCart == null) {
throw new EntityNotFoundException("Shopping cart not found for user id: " + userId);
}
ShoppingCart shoppingCart = shoppingCartRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException("Shopping cart not found "
+ "for user id: " + userId));

CartItem cartItem = cartItemRepository.findByIdAndShoppingCartId(cartItemId,
shoppingCart.getId())
Expand Down

0 comments on commit fb65d91

Please sign in to comment.