[b68ae8d] | 1 | package finki.it.phoneluxbackend.services;
|
---|
| 2 |
|
---|
| 3 | import finki.it.phoneluxbackend.entities.Phone;
|
---|
| 4 | import finki.it.phoneluxbackend.entities.PhoneOffer;
|
---|
| 5 | import finki.it.phoneluxbackend.repositories.PhoneRepository;
|
---|
[f25d07e] | 6 | import org.springframework.data.domain.PageRequest;
|
---|
[b68ae8d] | 7 | import org.springframework.data.domain.Sort;
|
---|
| 8 | import org.springframework.stereotype.Service;
|
---|
| 9 |
|
---|
[f25d07e] | 10 | import java.util.Comparator;
|
---|
[b68ae8d] | 11 | import java.util.List;
|
---|
[e5b84dc] | 12 | import java.util.Objects;
|
---|
[f25d07e] | 13 | import java.util.stream.Collectors;
|
---|
[b68ae8d] | 14 |
|
---|
| 15 | @Service
|
---|
| 16 | public class PhoneService {
|
---|
| 17 | private final PhoneRepository phoneRepository;
|
---|
| 18 |
|
---|
| 19 | public PhoneService(PhoneRepository phoneRepository){
|
---|
| 20 | this.phoneRepository = phoneRepository;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
[f25d07e] | 23 |
|
---|
| 24 | // TODO: insert logic to filter
|
---|
[e5b84dc] | 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;
|
---|
[b68ae8d] | 76 | }
|
---|
| 77 |
|
---|
[f25d07e] | 78 | public List<String> getBrands(){
|
---|
| 79 | return phoneRepository.findAll().stream()
|
---|
| 80 | .map(Phone::getBrand).distinct()
|
---|
| 81 | .collect(Collectors.toList());
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | public Phone getPhoneById(Long phoneId) {
|
---|
| 85 | boolean exists = phoneRepository.existsById(phoneId);
|
---|
| 86 | if(!exists)
|
---|
| 87 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
| 88 | return phoneRepository.findById(phoneId).get();
|
---|
| 89 | }
|
---|
[b68ae8d] | 90 | }
|
---|