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;
|
---|
6 | import org.springframework.data.domain.PageRequest;
|
---|
7 | import org.springframework.data.domain.Sort;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.Comparator;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Objects;
|
---|
13 | import java.util.stream.Collectors;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | public class PhoneService {
|
---|
17 | private final PhoneRepository phoneRepository;
|
---|
18 |
|
---|
19 | public PhoneService(PhoneRepository phoneRepository){
|
---|
20 | this.phoneRepository = phoneRepository;
|
---|
21 | }
|
---|
22 |
|
---|
23 |
|
---|
24 | // TODO: insert logic to filter
|
---|
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;
|
---|
76 | }
|
---|
77 |
|
---|
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 | }
|
---|
90 | }
|
---|