|
1 | 1 | package com.manir.springbootecommercerestapi.service.Impl;
|
2 | 2 |
|
| 3 | +import com.manir.springbootecommercerestapi.dto.OrderProductsDto; |
| 4 | +import com.manir.springbootecommercerestapi.exception.EcommerceApiException; |
3 | 5 | import com.manir.springbootecommercerestapi.model.OrderProducts;
|
| 6 | +import com.manir.springbootecommercerestapi.model.User; |
4 | 7 | import com.manir.springbootecommercerestapi.repository.OrderProductsRepository;
|
5 | 8 | import com.manir.springbootecommercerestapi.service.OrderProductsService;
|
6 | 9 | import lombok.AllArgsConstructor;
|
| 10 | +import org.modelmapper.ModelMapper; |
| 11 | +import org.modelmapper.convention.MatchingStrategies; |
| 12 | +import org.springframework.http.HttpStatus; |
7 | 13 | import org.springframework.stereotype.Service;
|
8 | 14 |
|
9 | 15 | import javax.annotation.Resource;
|
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Collectors; |
10 | 18 |
|
11 | 19 | @Service
|
12 | 20 | @AllArgsConstructor
|
13 | 21 | public class OrderProductsServiceImpl implements OrderProductsService {
|
14 | 22 |
|
15 | 23 | @Resource(name = "orderProductsRepository")
|
16 | 24 | private final OrderProductsRepository orderProductsRepository;
|
| 25 | + @Resource(name = "modelMapper") |
| 26 | + private final ModelMapper modelMapper; |
17 | 27 |
|
18 | 28 | @Override
|
19 | 29 | public void addOrderProducts(OrderProducts orderProducts) {
|
20 | 30 | orderProductsRepository.save(orderProducts);
|
21 | 31 | }
|
| 32 | + |
| 33 | + @Override |
| 34 | + public List<OrderProductsDto> findOrderItemsByCustomer(User customer) { |
| 35 | + |
| 36 | + List<OrderProducts> orderProducts = orderProductsRepository.findByCustomer(customer); |
| 37 | + if (orderProducts.size() == 0){ |
| 38 | + throw new EcommerceApiException("User has no ordered products", HttpStatus.BAD_REQUEST); |
| 39 | + } |
| 40 | + List<OrderProductsDto> orderProductsDtoList = orderProducts.stream() |
| 41 | + .map(orderProduct -> mapToDto(orderProduct)) |
| 42 | + .collect(Collectors.toList()); |
| 43 | + return orderProductsDtoList; |
| 44 | + } |
| 45 | + |
| 46 | + //map to dto |
| 47 | + private OrderProductsDto mapToDto(OrderProducts orderProducts){ |
| 48 | + modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); |
| 49 | + OrderProductsDto orderProductsDto = modelMapper.map(orderProducts, OrderProductsDto.class); |
| 50 | + |
| 51 | + return orderProductsDto; |
| 52 | + } |
22 | 53 | }
|
0 commit comments