source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java

Last change on this file was ffd50db, checked in by Marko <Marko@…>, 2 years ago

Added few methods in PhoneOffer service

  • Property mode set to 100644
File size: 11.1 KB
Line 
1package finki.it.phoneluxbackend.services;
2
3import finki.it.phoneluxbackend.entities.Phone;
4import finki.it.phoneluxbackend.repositories.PhoneRepository;
5import org.springframework.http.ResponseEntity;
6import org.springframework.stereotype.Service;
7
8import java.util.Arrays;
9import java.util.Comparator;
10import java.util.List;
11import java.util.Objects;
12import java.util.stream.Collectors;
13
14@Service
15public class PhoneService {
16 private final PhoneRepository phoneRepository;
17
18 public PhoneService(PhoneRepository phoneRepository){
19 this.phoneRepository = phoneRepository;
20 }
21
22
23 // TODO: insert logic to filter
24 public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue,
25 String ram, String rom, String frontcamera, String backcamera, String chipset,
26 String cpu, String operatingsystem, String color, String battery){
27 List<Phone> phones = phoneRepository.findAll().stream()
28 .filter(phone -> phone.getTotal_offers() > 0)
29 .collect(Collectors.toList());
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 phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
156 .collect(Collectors.toList());
157 if(sortBy != null)
158 {
159 if(sortBy.equals("ascending")) {
160 phones = phones.stream()
161 .sorted(Comparator.comparing(Phone::getLowestPrice))
162 .collect(Collectors.toList());
163 }
164
165 if(sortBy.equals("descending")) {
166 phones = phones.stream()
167 .sorted(Comparator.comparing(Phone::getLowestPrice).reversed())
168 .collect(Collectors.toList());
169 }
170 }
171
172 return phones;
173 }
174
175 public boolean hasSpecification(String specification, String filter){
176 if(specification.contains(filter))
177 {
178 if(specification.indexOf(filter)-1 < 0) {
179 return true;
180 }
181
182 if(!Character.isDigit(specification
183 .charAt(specification.indexOf(filter)-1))) {
184 return true;
185 }
186 }
187
188 if(specification.contains(filter.split("GB")[0]+" GB"))
189 {
190 if(specification.indexOf(filter.split("GB")[0]+" GB")-1 < 0) {
191 return true;
192 }
193
194 if(!Character.isDigit(specification
195 .charAt(specification.indexOf(filter.split("GB")[0]+" GB")-1))) {
196 return true;
197 }
198 }
199
200 if(specification.contains(filter.split("MP")[0]+" MP"))
201 {
202 if(specification.indexOf(filter.split("MP")[0]+" MP")-1 < 0) {
203 return true;
204 }
205
206 if(!Character.isDigit(specification
207 .charAt(specification.indexOf(filter.split("MP")[0]+" MP")-1))) {
208 return true;
209 }
210 }
211
212 if(specification.contains(filter.split("MB")[0]+" MB"))
213 {
214 if(specification.indexOf(filter.split("MB")[0]+" MB")-1 < 0) {
215 return true;
216 }
217
218 if(!Character.isDigit(specification
219 .charAt(specification.indexOf(filter.split("MB")[0]+" MB")-1))) {
220 return true;
221 }
222 }
223
224 return false;
225 }
226
227 public List<String> getBrands(){
228 return phoneRepository.findAll().stream()
229 .map(phone -> phone.getBrand().stripIndent())
230 .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
241 public Long getTotalOffersForPhone(String phoneModel) {
242 String model = String.join(" ", phoneModel.split("\\*"));
243 boolean exists = phoneRepository.findPhoneByModel(model).isPresent();
244
245 if(!exists)
246 throw new IllegalStateException("Phone with model "+model+" does not exist");
247
248 return phoneRepository.findPhoneByModel(model).get().getPhoneOffers().stream().count();
249 }
250
251 public ResponseEntity<Object> setTotalOffersForPhone(Long phoneId, int totaloffers) {
252 boolean exists = phoneRepository.findById(phoneId).isPresent();
253
254 if(!exists)
255 throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
256
257 Phone phone = phoneRepository.findById(phoneId).get();
258 phone.setTotal_offers(totaloffers);
259 phoneRepository.save(phone);
260 return ResponseEntity.ok().build();
261 }
262
263 public ResponseEntity<Object> setLowestPriceForPhone(Long phoneId, int lowestPrice) {
264 boolean exists = phoneRepository.findById(phoneId).isPresent();
265
266 if(!exists)
267 throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
268
269 Phone phone = phoneRepository.findById(phoneId).get();
270 phone.setLowestPrice(lowestPrice);
271 phoneRepository.save(phone);
272 return ResponseEntity.ok().build();
273 }
274
275
276 public ResponseEntity<Object> setImageUrlForPhone(Long phoneId, String newImageUrl) {
277 boolean exists = phoneRepository.findById(phoneId).isPresent();
278
279 if(!exists)
280 throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
281
282 Phone phone = phoneRepository.findById(phoneId).get();
283 phone.setImage_url(newImageUrl);
284 phoneRepository.save(phone);
285
286 return ResponseEntity.ok().build();
287 }
288}
Note: See TracBrowser for help on using the repository browser.