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.Arrays;
|
---|
11 | import java.util.Comparator;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Objects;
|
---|
14 | import java.util.stream.Collectors;
|
---|
15 |
|
---|
16 | @Service
|
---|
17 | public class PhoneService {
|
---|
18 | private final PhoneRepository phoneRepository;
|
---|
19 |
|
---|
20 | public PhoneService(PhoneRepository phoneRepository){
|
---|
21 | this.phoneRepository = phoneRepository;
|
---|
22 | }
|
---|
23 |
|
---|
24 |
|
---|
25 | // TODO: insert logic to filter
|
---|
26 | public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue,
|
---|
27 | String ram, String rom, String frontcamera, String backcamera, String chipset,
|
---|
28 | String cpu, String operatingsystem, String color, String battery){
|
---|
29 | List<Phone> phones = phoneRepository.findAll();
|
---|
30 |
|
---|
31 |
|
---|
32 | if(brands != null)
|
---|
33 | {
|
---|
34 | phones = phones.stream()
|
---|
35 | .filter(phone -> brands.contains(phone.getBrand())).collect(Collectors.toList());
|
---|
36 | }
|
---|
37 |
|
---|
38 | if(shops != null)
|
---|
39 | {
|
---|
40 | phones = phones.stream()
|
---|
41 | .filter(phone -> phone.getPhoneOffers().stream().anyMatch(offer -> shops.contains(offer.getOffer_shop())))
|
---|
42 | .collect(Collectors.toList());
|
---|
43 | }
|
---|
44 |
|
---|
45 | if(priceRange != null)
|
---|
46 | {
|
---|
47 | int lowestPrice = Integer.parseInt(priceRange.split("-")[0]);
|
---|
48 | int highestPrice = Integer.parseInt(priceRange.split("-")[1]);
|
---|
49 | phones = phones.stream()
|
---|
50 | .filter(phone -> phone.getLowestPrice() >= lowestPrice && phone.getLowestPrice() <= highestPrice)
|
---|
51 | .collect(Collectors.toList());
|
---|
52 | }
|
---|
53 |
|
---|
54 | if(searchValue != null && !Objects.equals(searchValue.stripIndent(), "")){
|
---|
55 | phones = phones.stream()
|
---|
56 | .filter(phone -> phone.getBrand().toLowerCase().contains(searchValue.stripIndent().toLowerCase())
|
---|
57 | || phone.getModel().toLowerCase().contains(searchValue.stripIndent().toLowerCase()))
|
---|
58 | .collect(Collectors.toList());
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | // specifications filters
|
---|
63 |
|
---|
64 | if(ram != null){
|
---|
65 | String [] memories = ram.split(",");
|
---|
66 | phones = phones.stream()
|
---|
67 | .filter(phone -> Arrays.stream(memories).anyMatch(memory -> phone.getPhoneOffers().stream()
|
---|
68 | .filter(offer -> offer.getRam_memory() != null)
|
---|
69 | .anyMatch(offer -> hasSpecification(offer.getRam_memory(),memory))
|
---|
70 | )
|
---|
71 | )
|
---|
72 | .collect(Collectors.toList());
|
---|
73 | }
|
---|
74 |
|
---|
75 | if(rom != null){
|
---|
76 | String [] memories = rom.split(",");
|
---|
77 | phones = phones.stream()
|
---|
78 | .filter(phone -> Arrays.stream(memories).anyMatch(memory -> phone.getPhoneOffers().stream()
|
---|
79 | .filter(offer -> offer.getRom_memory() != null)
|
---|
80 | .anyMatch(offer -> hasSpecification(offer.getRom_memory(),memory))))
|
---|
81 | .collect(Collectors.toList());
|
---|
82 | }
|
---|
83 |
|
---|
84 | if(frontcamera != null){
|
---|
85 | String [] cameras = frontcamera.split(",");
|
---|
86 | phones = phones.stream()
|
---|
87 | .filter(phone -> Arrays.stream(cameras).anyMatch(camera -> phone.getPhoneOffers().stream()
|
---|
88 | .filter(offer -> offer.getFront_camera() != null)
|
---|
89 | .anyMatch(offer -> hasSpecification(offer.getFront_camera(),camera)
|
---|
90 | )
|
---|
91 | )
|
---|
92 | )
|
---|
93 | .collect(Collectors.toList());
|
---|
94 | }
|
---|
95 |
|
---|
96 | if(backcamera != null){
|
---|
97 | String [] cameras = backcamera.split(",");
|
---|
98 | phones = phones.stream()
|
---|
99 | .filter(phone -> Arrays.stream(cameras).anyMatch(camera -> phone.getPhoneOffers().stream()
|
---|
100 | .filter(offer -> offer.getBack_camera() != null)
|
---|
101 | .anyMatch(offer -> hasSpecification(offer.getBack_camera(),camera))))
|
---|
102 | .collect(Collectors.toList());
|
---|
103 | }
|
---|
104 |
|
---|
105 | if(chipset != null)
|
---|
106 | {
|
---|
107 | String [] chipsets = chipset.split(",");
|
---|
108 | phones = phones.stream()
|
---|
109 | .filter(phone -> Arrays.stream(chipsets).anyMatch(chip -> phone.getPhoneOffers().stream()
|
---|
110 | .filter(offer -> offer.getChipset() != null)
|
---|
111 | .anyMatch(offer -> offer.getChipset().contains(chip))))
|
---|
112 | .collect(Collectors.toList());
|
---|
113 | }
|
---|
114 |
|
---|
115 | if(cpu != null)
|
---|
116 | {
|
---|
117 | String [] cpus = cpu.split(",");
|
---|
118 | phones = phones.stream()
|
---|
119 | .filter(phone -> Arrays.stream(cpus).anyMatch(processor -> phone.getPhoneOffers().stream()
|
---|
120 | .filter(offer -> offer.getCpu() != null)
|
---|
121 | .anyMatch(offer -> offer.getCpu().contains(processor))))
|
---|
122 | .collect(Collectors.toList());
|
---|
123 | }
|
---|
124 |
|
---|
125 | if(operatingsystem != null)
|
---|
126 | {
|
---|
127 | String [] operatingSystems = operatingsystem.split(",");
|
---|
128 | phones = phones.stream()
|
---|
129 | .filter(phone -> Arrays.stream(operatingSystems).anyMatch(os -> phone.getPhoneOffers().stream()
|
---|
130 | .filter(offer -> offer.getOperating_system() != null)
|
---|
131 | .anyMatch(offer -> offer.getOperating_system().contains(os))))
|
---|
132 | .collect(Collectors.toList());
|
---|
133 | }
|
---|
134 |
|
---|
135 | if(color != null)
|
---|
136 | {
|
---|
137 | String [] colors = color.split(",");
|
---|
138 | phones = phones.stream()
|
---|
139 | .filter(phone -> Arrays.stream(colors).anyMatch(c -> phone.getPhoneOffers().stream()
|
---|
140 | .filter(offer -> offer.getColor() != null)
|
---|
141 | .anyMatch(offer -> offer.getColor().contains(c))))
|
---|
142 | .collect(Collectors.toList());
|
---|
143 | }
|
---|
144 |
|
---|
145 | if(battery != null)
|
---|
146 | {
|
---|
147 | String [] batteries = battery.split(",");
|
---|
148 | phones = phones.stream()
|
---|
149 | .filter(phone -> Arrays.stream(batteries).anyMatch(b -> phone.getPhoneOffers().stream()
|
---|
150 | .filter(offer -> offer.getBattery() != null)
|
---|
151 | .anyMatch(offer -> offer.getBattery().contains(b))))
|
---|
152 | .collect(Collectors.toList());
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
|
---|
157 | .collect(Collectors.toList());
|
---|
158 | if(sortBy != null)
|
---|
159 | {
|
---|
160 | if(sortBy.equals("ascending")) {
|
---|
161 | phones = phones.stream()
|
---|
162 | .sorted(Comparator.comparing(Phone::getLowestPrice))
|
---|
163 | .collect(Collectors.toList());
|
---|
164 | }
|
---|
165 |
|
---|
166 | if(sortBy.equals("descending")) {
|
---|
167 | phones = phones.stream()
|
---|
168 | .sorted(Comparator.comparing(Phone::getLowestPrice).reversed())
|
---|
169 | .collect(Collectors.toList());
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return phones;
|
---|
174 | }
|
---|
175 |
|
---|
176 | public boolean hasSpecification(String specification, String filter){
|
---|
177 | if(specification.contains(filter))
|
---|
178 | {
|
---|
179 | if(specification.indexOf(filter)-1 < 0) {
|
---|
180 | return true;
|
---|
181 | }
|
---|
182 |
|
---|
183 | if(!Character.isDigit(specification
|
---|
184 | .charAt(specification.indexOf(filter)-1))) {
|
---|
185 | return true;
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | if(specification.contains(filter.split("GB")[0]+" GB"))
|
---|
190 | {
|
---|
191 | if(specification.indexOf(filter.split("GB")[0]+" GB")-1 < 0) {
|
---|
192 | return true;
|
---|
193 | }
|
---|
194 |
|
---|
195 | if(!Character.isDigit(specification
|
---|
196 | .charAt(specification.indexOf(filter.split("GB")[0]+" GB")-1))) {
|
---|
197 | return true;
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 | if(specification.contains(filter.split("MP")[0]+" MP"))
|
---|
202 | {
|
---|
203 | if(specification.indexOf(filter.split("MP")[0]+" MP")-1 < 0) {
|
---|
204 | return true;
|
---|
205 | }
|
---|
206 |
|
---|
207 | if(!Character.isDigit(specification
|
---|
208 | .charAt(specification.indexOf(filter.split("MP")[0]+" MP")-1))) {
|
---|
209 | return true;
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | if(specification.contains(filter.split("MB")[0]+" MB"))
|
---|
214 | {
|
---|
215 | if(specification.indexOf(filter.split("MB")[0]+" MB")-1 < 0) {
|
---|
216 | return true;
|
---|
217 | }
|
---|
218 |
|
---|
219 | if(!Character.isDigit(specification
|
---|
220 | .charAt(specification.indexOf(filter.split("MB")[0]+" MB")-1))) {
|
---|
221 | return true;
|
---|
222 | }
|
---|
223 | }
|
---|
224 |
|
---|
225 | return false;
|
---|
226 | }
|
---|
227 |
|
---|
228 | public List<String> getBrands(){
|
---|
229 | return phoneRepository.findAll().stream()
|
---|
230 | .map(Phone::getBrand).distinct()
|
---|
231 | .collect(Collectors.toList());
|
---|
232 | }
|
---|
233 |
|
---|
234 | public Phone getPhoneById(Long phoneId) {
|
---|
235 | boolean exists = phoneRepository.existsById(phoneId);
|
---|
236 | if(!exists)
|
---|
237 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
238 | return phoneRepository.findById(phoneId).get();
|
---|
239 | }
|
---|
240 | }
|
---|