paykova

Untitled

Dec 6th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. @Service
  2. public class ProductServiceImpl implements ProductService {
  3.  
  4. private final ProductRepository productRepository;
  5. private final CategoryRepository categoryRepository;
  6. private final UserRepository userRepository;
  7. private final ValidatorUtil validatorUtil;
  8. private final ModelMapper modelMapper;
  9.  
  10. @Autowired
  11. public ProductServiceImpl(ProductRepository productRepository, CategoryRepository categoryRepository, UserRepository userRepository, ValidatorUtil validatorUtil, ModelMapper modelMapper) {
  12. this.productRepository = productRepository;
  13. this.categoryRepository = categoryRepository;
  14. this.userRepository = userRepository;
  15. this.validatorUtil = validatorUtil;
  16. this.modelMapper = modelMapper;
  17. }
  18.  
  19.  
  20. @Override
  21. public void seedProducts(ProductSeedDto[] productSeedDtos) {
  22. for (ProductSeedDto productSeedDto : productSeedDtos) {
  23. if (!this.validatorUtil.isValid(productSeedDto)) {
  24. this.validatorUtil.violations(productSeedDto)
  25. .forEach(violation -> System.out.println(violation.getMessage()));
  26. continue;
  27. }
  28. Product entity = this.modelMapper.map(productSeedDto, Product.class);
  29. entity.setSeller(this.getRandomUser());
  30.  
  31. Random random = new Random();
  32.  
  33. if (random.nextInt() % 13 != 0) {
  34. entity.setBuyer(this.getRandomUser());
  35. }
  36. entity.setCategories(this.getRandomCategories());
  37. this.productRepository.saveAndFlush(entity);
  38. }
  39. }
  40.  
  41. @Override
  42. public List<ProductInRangeDto> productInRange(BigDecimal more, BigDecimal less) {
  43. List<Product> productEntities = this.productRepository.findAllByPriceBetweenAndBuyerOrderByPrice(more, less, null);
  44. List<ProductInRangeDto> productInRangeDtos = new ArrayList<>();
  45. for (Product productEntity : productEntities) {
  46. ProductInRangeDto productInRangeDto = this.modelMapper.map(productEntity, ProductInRangeDto.class);
  47. productInRangeDto.setSeller(String.format("%s %s", productEntity.getSeller().getFirstName(), productEntity.getSeller().getLastName()));
  48.  
  49. productInRangeDtos.add(productInRangeDto);
  50. }
  51. return productInRangeDtos;
  52. }
  53.  
  54.  
  55. private User getRandomUser() {
  56. Random random = new Random();
  57. return this.userRepository.getOne(random.nextInt((int) this.userRepository.count() - 1) + 1);
  58. }
  59.  
  60. private List<Category> getRandomCategories() {
  61. Random random = new Random();
  62. List<Category> categories = new ArrayList<>();
  63. int categoriesCount = random.nextInt((int) this.categoryRepository.count() - 1) + 1;
  64. for (int i = 0; i < categoriesCount; i++) {
  65. categories.add(this.categoryRepository.getOne(random.nextInt((int) this.categoryRepository.count() - 1) + 1));
  66. }
  67. return categories;
  68. }
  69.  
  70. }
Add Comment
Please, Sign In to add comment