Changeset e5b84dc for phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java
- Timestamp:
- 09/11/22 18:03:58 (2 years ago)
- Branches:
- master
- Children:
- 775e15e
- Parents:
- 527b93f
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java
r527b93f re5b84dc 10 10 import java.util.Comparator; 11 11 import java.util.List; 12 import java.util.Objects; 12 13 import java.util.stream.Collectors; 13 14 … … 22 23 23 24 // TODO: insert logic to filter 24 public List<Phone> getPhones(){ 25 return phoneRepository.findAll(); 25 public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue){ 26 List<Phone> phones = phoneRepository.findAll(); 27 28 29 if(brands != null) 30 { 31 phones = phones.stream() 32 .filter(phone -> brands.contains(phone.getBrand())).collect(Collectors.toList()); 33 } 34 35 if(shops != null) 36 { 37 phones = phones.stream() 38 .filter(phone -> phone.getPhoneOffers().stream().anyMatch(offer -> shops.contains(offer.getOffer_shop()))) 39 .collect(Collectors.toList()); 40 } 41 42 if(priceRange != null) 43 { 44 int lowestPrice = Integer.parseInt(priceRange.split("-")[0]); 45 int highestPrice = Integer.parseInt(priceRange.split("-")[1]); 46 phones = phones.stream() 47 .filter(phone -> phone.getLowestPrice() >= lowestPrice && phone.getLowestPrice() <= highestPrice) 48 .collect(Collectors.toList()); 49 } 50 51 if(searchValue != null && !Objects.equals(searchValue.stripIndent(), "")){ 52 phones = phones.stream() 53 .filter(phone -> phone.getBrand().toLowerCase().contains(searchValue.stripIndent().toLowerCase()) 54 || phone.getModel().toLowerCase().contains(searchValue.stripIndent().toLowerCase())) 55 .collect(Collectors.toList()); 56 } 57 58 phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed()) 59 .collect(Collectors.toList()); 60 if(sortBy != null) 61 { 62 if(sortBy.equals("ascending")) { 63 phones = phones.stream() 64 .sorted(Comparator.comparing(Phone::getLowestPrice)) 65 .collect(Collectors.toList()); 66 } 67 68 if(sortBy.equals("descending")) { 69 phones = phones.stream() 70 .sorted(Comparator.comparing(Phone::getLowestPrice).reversed()) 71 .collect(Collectors.toList()); 72 } 73 } 74 75 return phones; 26 76 } 27 77
Note:
See TracChangeset
for help on using the changeset viewer.