Advertisement
Guest User

Untitled

a guest
Jan 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. @RestController
  2. public class ProductApiController {
  3.  
  4. private final ProductRepository repository;
  5.  
  6. @Autowired
  7. protected ProductApiController(final ProductRepository repository) {
  8. this.repository = repository;
  9. }
  10.  
  11. @RequestMapping(method = RequestMethod.GET)
  12. public Page<Product> find(
  13. @PageableDefault(sort = "id", direction = DESC) final Pageable pageable,
  14. @RequestParam(required = false) final String nameLike,
  15. @RequestParam(required = false) final Long priceFrom,
  16. @RequestParam(required = false) final Long priceTo,
  17. @RequestParam(defaultValue = "true") final boolean inStock,
  18. @RequestParam(required = false) final Set<String> categoriesIn) {
  19.  
  20. Specifications<Product> specifications = Specifications.where(stockAvaiable(inStock));
  21.  
  22. if (StringUtils.isNotEmpty(nameLike)) {
  23. specifications = specifications.and(nameLike(nameLike));
  24. }
  25. if (Objects.nonNull(priceFrom)) {
  26. specifications = specifications.and(priceGraterThanOrEqualsTo(priceFrom));
  27. }
  28. if (Objects.nonNull(priceTo)) {
  29. specifications = specifications.and(priceLessThan(priceTo));
  30. }
  31. if (CollectionUtils.isNotEmpty(categoriesIn)) {
  32. specifications = specifications.and(categoriesIn(categoriesIn));
  33. }
  34. specifications = specifications.and(stockAvaiable(inStock));
  35.  
  36. return repository.findAll(specifications, pageable);
  37. }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement