|
| 1 | +package com.manir.springbootecommercerestapi.service.Impl; |
| 2 | + |
| 3 | +import com.manir.springbootecommercerestapi.dto.CartItemDto; |
| 4 | +import com.manir.springbootecommercerestapi.dto.OrderDto; |
| 5 | +import com.manir.springbootecommercerestapi.exception.EcommerceApiException; |
| 6 | +import com.manir.springbootecommercerestapi.model.Order; |
| 7 | +import com.manir.springbootecommercerestapi.model.OrderProducts; |
| 8 | +import com.manir.springbootecommercerestapi.model.User; |
| 9 | +import com.manir.springbootecommercerestapi.repository.CartItemRepository; |
| 10 | +import com.manir.springbootecommercerestapi.repository.OrderRepository; |
| 11 | +import com.manir.springbootecommercerestapi.response.CartItemResponse; |
| 12 | +import com.manir.springbootecommercerestapi.service.OrderProductsService; |
| 13 | +import com.manir.springbootecommercerestapi.service.OrderService; |
| 14 | +import com.manir.springbootecommercerestapi.service.ShoppingCartService; |
| 15 | +import lombok.AllArgsConstructor; |
| 16 | +import org.modelmapper.ModelMapper; |
| 17 | +import org.springframework.beans.factory.annotation.Autowired; |
| 18 | +import org.springframework.http.HttpStatus; |
| 19 | +import org.springframework.stereotype.Service; |
| 20 | + |
| 21 | +import javax.transaction.Transactional; |
| 22 | +import java.util.List; |
| 23 | +import java.util.stream.Collectors; |
| 24 | + |
| 25 | +@Service |
| 26 | +@AllArgsConstructor |
| 27 | +public class OrderServiceImpl implements OrderService { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private final OrderRepository orderRepository; |
| 31 | + @Autowired |
| 32 | + private final OrderProductsService orderProductsService; |
| 33 | + @Autowired |
| 34 | + private final CartItemRepository cartItemRepository; |
| 35 | + @Autowired |
| 36 | + private final ShoppingCartService shoppingCartService; |
| 37 | + @Autowired |
| 38 | + private final ModelMapper modelMapper; |
| 39 | + |
| 40 | + @Override |
| 41 | + @Transactional |
| 42 | + public void placeOrder(User customer) { |
| 43 | + CartItemResponse cartItemDto = shoppingCartService.findByCustomer(customer); |
| 44 | + OrderDto orderDto = new OrderDto(); |
| 45 | + orderDto.setTotalPrice(cartItemDto.getTotalCost()); |
| 46 | + orderDto.setEmail(customer.getEmail()); |
| 47 | + orderDto.setName(customer.getName()); |
| 48 | + OrderDto savedOrder = saveOrder(orderDto, customer); |
| 49 | + List<CartItemDto> cartItemDtoList = cartItemDto.getContent(); |
| 50 | + for(CartItemDto cartItem : cartItemDtoList){ |
| 51 | + OrderProducts orderProducts = new OrderProducts(); |
| 52 | + orderProducts.setCustomer(customer); |
| 53 | + orderProducts.setProduct(cartItem.getProduct()); |
| 54 | + orderProducts.setOrder(mapToEntity(savedOrder)); |
| 55 | + orderProducts.setProductPrice(cartItem.getProduct().getPrice()); |
| 56 | + orderProducts.setProductQuantity(cartItem.getQuantity()); |
| 57 | + orderProducts.setTotalPrice(cartItemDto.getTotalCost()); |
| 58 | + orderProductsService.addOrderProducts(orderProducts); |
| 59 | + } |
| 60 | + cartItemRepository.deleteByCustomerId(customer.getId()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public OrderDto saveOrder(OrderDto orderDto, User customer) { |
| 65 | + //convert to entity |
| 66 | + Order order = mapToEntity(orderDto); |
| 67 | + //save order to db |
| 68 | + Order placedOrder = orderRepository.save(order); |
| 69 | + return mapToDto(placedOrder); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public List<OrderDto> listOrdersByCustomer(User customer) { |
| 74 | + List<Order> orders = orderRepository.findByCustomer(customer); |
| 75 | + if (orders.size() == 0){ |
| 76 | + throw new EcommerceApiException("User has no order", HttpStatus.BAD_REQUEST); |
| 77 | + } |
| 78 | + List<OrderDto> orderDtoList = orders.stream() |
| 79 | + .map(order -> mapToDto(order)) |
| 80 | + .collect(Collectors.toList()); |
| 81 | + return orderDtoList; |
| 82 | + } |
| 83 | + |
| 84 | + //map to Entity |
| 85 | + private Order mapToEntity(OrderDto orderDto){ |
| 86 | + Order order = modelMapper.map(orderDto, Order.class); |
| 87 | + return order; |
| 88 | + } |
| 89 | + //map to Dto |
| 90 | + private OrderDto mapToDto(Order order){ |
| 91 | + OrderDto orderDto = modelMapper.map(order, OrderDto.class); |
| 92 | + return orderDto; |
| 93 | + } |
| 94 | +} |
0 commit comments