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.PhoneOfferRepository;
|
---|
6 | import finki.it.phoneluxbackend.repositories.PhoneRepository;
|
---|
7 | import org.apache.coyote.Response;
|
---|
8 | import org.springframework.http.ResponseEntity;
|
---|
9 | import org.springframework.stereotype.Service;
|
---|
10 |
|
---|
11 | import java.util.*;
|
---|
12 | import java.util.stream.Collectors;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class PhoneOfferService {
|
---|
16 | private final PhoneOfferRepository phoneOfferRepository;
|
---|
17 | private final PhoneRepository phoneRepository;
|
---|
18 |
|
---|
19 | public PhoneOfferService(PhoneOfferRepository phoneOfferRepository, PhoneRepository phoneRepository) {
|
---|
20 | this.phoneOfferRepository = phoneOfferRepository;
|
---|
21 | this.phoneRepository = phoneRepository;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public List<PhoneOffer> getPhoneOffersForPhone(Long phoneId) {
|
---|
25 | boolean exists = phoneRepository.existsById(phoneId);
|
---|
26 | if(!exists)
|
---|
27 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
28 |
|
---|
29 | return phoneRepository.findById(phoneId).get().getPhoneOffers()
|
---|
30 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice)).collect(Collectors.toList());
|
---|
31 | }
|
---|
32 |
|
---|
33 | public PhoneOffer getPhoneOffer(Long offerId){
|
---|
34 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
35 |
|
---|
36 | if(!exists)
|
---|
37 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
38 |
|
---|
39 | return phoneOfferRepository.findById(offerId).get();
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | public List<String> getShops() {
|
---|
44 | return phoneOfferRepository.findAll().stream()
|
---|
45 | .map(PhoneOffer::getOffer_shop)
|
---|
46 | .distinct()
|
---|
47 | .collect(Collectors.toList());
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 | public int getLowestPrice() {
|
---|
52 | return phoneOfferRepository.findAll()
|
---|
53 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice))
|
---|
54 | .collect(Collectors.toList()).get(0).getPrice();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public int getHighestPrice() {
|
---|
58 | return phoneOfferRepository.findAll()
|
---|
59 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice).reversed())
|
---|
60 | .collect(Collectors.toList()).get(0).getPrice();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public List<PhoneOffer> getCheaperOffers(Long offerId) {
|
---|
64 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
65 |
|
---|
66 | if(!exists)
|
---|
67 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
68 |
|
---|
69 | PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
|
---|
70 |
|
---|
71 | return phoneOfferRepository.findAll()
|
---|
72 | .stream().filter(phoneOffer ->
|
---|
73 | Objects.equals(phoneOffer.getPhone().getModel(), offer.getPhone().getModel())
|
---|
74 | && phoneOffer.getPrice() < offer.getPrice())
|
---|
75 | .sorted(Comparator.comparing(PhoneOffer::getPrice).reversed())
|
---|
76 | .collect(Collectors.toList());
|
---|
77 | }
|
---|
78 |
|
---|
79 | public ResponseEntity<Object> editOffer(Long offerId, PhoneOffer editedOffer) {
|
---|
80 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
81 |
|
---|
82 | if(!exists)
|
---|
83 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
84 |
|
---|
85 | PhoneOffer oldOffer = phoneOfferRepository.findById(offerId).get();
|
---|
86 |
|
---|
87 | editedOffer.setPhone(oldOffer.getPhone());
|
---|
88 | editedOffer.setUsers(oldOffer.getUsers());
|
---|
89 | editedOffer.setIs_validated(false);
|
---|
90 | editedOffer.setLast_updated(new Date());
|
---|
91 |
|
---|
92 | phoneOfferRepository.save(editedOffer);
|
---|
93 |
|
---|
94 | return ResponseEntity.ok().build();
|
---|
95 | }
|
---|
96 |
|
---|
97 | public ResponseEntity<Object> validateOffer(Long offerId) {
|
---|
98 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
99 |
|
---|
100 | if(!exists)
|
---|
101 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
102 |
|
---|
103 | PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
|
---|
104 |
|
---|
105 | offer.setIs_validated(true);
|
---|
106 | offer.setLast_updated(new Date());
|
---|
107 | phoneOfferRepository.save(offer);
|
---|
108 |
|
---|
109 | return ResponseEntity.ok().build();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public List<PhoneOffer> getMultiplePhoneOffers(String offerIds) {
|
---|
113 | List<Long> idList = Arrays.stream(offerIds.split(","))
|
---|
114 | .map(Long::parseLong)
|
---|
115 | .collect(Collectors.toList());
|
---|
116 |
|
---|
117 | List<PhoneOffer> phoneOffers = new ArrayList<>();
|
---|
118 |
|
---|
119 | idList.stream().forEach(id -> {
|
---|
120 | phoneOffers.add(phoneOfferRepository.findById(id).get());
|
---|
121 | });
|
---|
122 |
|
---|
123 | return phoneOffers;
|
---|
124 | }
|
---|
125 |
|
---|
126 | public List<PhoneOffer> getOffersFromShop(String shop) {
|
---|
127 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
128 |
|
---|
129 | if(shop.equalsIgnoreCase("mobigo"))
|
---|
130 | shop = "Mobi Go";
|
---|
131 |
|
---|
132 | if(shop.equalsIgnoreCase("mobilezone"))
|
---|
133 | shop = "Mobile Zone";
|
---|
134 | String finalShop = shop;
|
---|
135 |
|
---|
136 | return offers.stream()
|
---|
137 | .filter(offer -> offer.getOffer_shop().stripIndent().equalsIgnoreCase(finalShop.stripIndent()))
|
---|
138 | .collect(Collectors.toList());
|
---|
139 | }
|
---|
140 |
|
---|
141 | public List<String> getRamMemories() {
|
---|
142 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
143 |
|
---|
144 | List<String> temp = new ArrayList<>();
|
---|
145 |
|
---|
146 | offers.stream()
|
---|
147 | .map(PhoneOffer::getRam_memory)
|
---|
148 | .filter(ram -> ram != null && (ram.toLowerCase().contains("gb") || ram.toLowerCase().contains("mb")))
|
---|
149 | .forEach(ram -> {
|
---|
150 | temp.addAll(Arrays.asList(ram.replaceAll("\\s+", "")
|
---|
151 | .replaceAll("Ram", "")
|
---|
152 | .split("[,/]")));
|
---|
153 | });
|
---|
154 |
|
---|
155 | return getMemories(temp);
|
---|
156 | }
|
---|
157 |
|
---|
158 | public List<String> getRomMemories() {
|
---|
159 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
160 |
|
---|
161 | List<String> temp = new ArrayList<>();
|
---|
162 |
|
---|
163 | offers.stream()
|
---|
164 | .map(PhoneOffer::getRom_memory)
|
---|
165 | .filter(rom -> rom != null && (rom.toLowerCase().contains("gb") || rom.toLowerCase().contains("mb")))
|
---|
166 | .forEach(ram -> {
|
---|
167 | temp.addAll(Arrays.asList(ram.replaceAll("\\s+", "")
|
---|
168 | .replaceAll("Rom", "")
|
---|
169 | .replaceAll("storage", "")
|
---|
170 | .split("[,/]")));
|
---|
171 | });
|
---|
172 |
|
---|
173 | return getMemories(temp);
|
---|
174 | }
|
---|
175 |
|
---|
176 | private List<String> getMemories(List<String> temp) {
|
---|
177 | List<String> memories = new ArrayList<>();
|
---|
178 |
|
---|
179 | temp.stream()
|
---|
180 | .filter(memory -> memory.toLowerCase().contains("mb"))
|
---|
181 | .sorted()
|
---|
182 | .forEach(memories::add);
|
---|
183 |
|
---|
184 | temp.stream()
|
---|
185 | .filter(memory -> memory.toLowerCase().contains("gb"))
|
---|
186 | .sorted()
|
---|
187 | .forEach(memories::add);
|
---|
188 |
|
---|
189 |
|
---|
190 | return memories.stream()
|
---|
191 | .filter(memory -> memory.matches("\\S*\\d+\\S*"))
|
---|
192 | .distinct()
|
---|
193 | .collect(Collectors.toList());
|
---|
194 | }
|
---|
195 |
|
---|
196 | public List<String> getColors() {
|
---|
197 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
198 | List<String> colors = new ArrayList<>();
|
---|
199 |
|
---|
200 | return offers.stream()
|
---|
201 | .map(PhoneOffer::getColor)
|
---|
202 | .filter(colorRow -> colorRow != null && !colorRow.equals("") && !colorRow.equals("/"))
|
---|
203 | .flatMap(color -> Arrays.stream(color.split(",")))
|
---|
204 | .map(String::stripIndent)
|
---|
205 | .filter(color -> !color.matches("\\S+\\d+\\S+") && !color.equals(""))
|
---|
206 | .distinct()
|
---|
207 | .sorted()
|
---|
208 | .collect(Collectors.toList());
|
---|
209 | }
|
---|
210 |
|
---|
211 | public List<String> getChipsets() {
|
---|
212 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
213 | List<String> temp = new ArrayList<>();
|
---|
214 | List<String> chipsets = new ArrayList<>();
|
---|
215 |
|
---|
216 | temp = offers.stream()
|
---|
217 | .map(PhoneOffer::getChipset)
|
---|
218 | .filter(chipset -> chipset != null && !chipset.equals("") && !chipset.equals("/"))
|
---|
219 | .distinct()
|
---|
220 | .collect(Collectors.toList());
|
---|
221 |
|
---|
222 | temp.stream()
|
---|
223 | .forEach(chipset -> chipsets.add(chipset.replaceAll("5G ", "")));
|
---|
224 |
|
---|
225 | return chipsets.stream()
|
---|
226 | .filter(chipset -> !chipset.contains("\r"))
|
---|
227 | .map(offer -> offer.split("\\(")[0].stripIndent())
|
---|
228 | .distinct()
|
---|
229 | .sorted()
|
---|
230 | .collect(Collectors.toList());
|
---|
231 | }
|
---|
232 |
|
---|
233 | public List<String> getCPUs() {
|
---|
234 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
235 |
|
---|
236 | return offers.stream()
|
---|
237 | .map(PhoneOffer::getCpu)
|
---|
238 | .filter(cpu -> cpu!=null && !cpu.equals("") && !cpu.equals("/"))
|
---|
239 | .map(cpu -> cpu.split("\n")[0].stripIndent().replaceAll("\n",""))
|
---|
240 | .filter(cpu -> !cpu.contains("Snapdragon") && !cpu.contains("Exynos"))
|
---|
241 | .filter(cpu -> Character.isAlphabetic(cpu.charAt(0)))
|
---|
242 | .distinct()
|
---|
243 | .sorted()
|
---|
244 | .collect(Collectors.toList());
|
---|
245 | }
|
---|
246 |
|
---|
247 | public List<String> getFrontCameras() {
|
---|
248 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
249 |
|
---|
250 | return offers.stream()
|
---|
251 | .map(PhoneOffer::getFront_camera)
|
---|
252 | .filter(camera -> camera != null && !camera.equals("") && !camera.equals("/"))
|
---|
253 | .map(camera -> camera.split("MP")[0].stripIndent()+"MP")
|
---|
254 | .filter(camera -> !camera.contains("\n"))
|
---|
255 | .distinct()
|
---|
256 | .sorted()
|
---|
257 | .collect(Collectors.toList());
|
---|
258 | }
|
---|
259 |
|
---|
260 | public List<String> getBackCameras() {
|
---|
261 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
262 |
|
---|
263 | List<String> cameras = offers.stream()
|
---|
264 | .map(PhoneOffer::getBack_camera)
|
---|
265 | .filter(camera -> camera != null && !camera.equals("") && !camera.equals("/"))
|
---|
266 | .map(camera -> camera.split("[\n,]")[0].replaceAll("\t",""))
|
---|
267 | .flatMap(camera -> Arrays.stream(camera.split("[+/]")))
|
---|
268 | .map(camera -> camera.replaceAll("MP","").stripIndent())
|
---|
269 | .distinct()
|
---|
270 | .sorted()
|
---|
271 | .collect(Collectors.toList());
|
---|
272 |
|
---|
273 | cameras.stream()
|
---|
274 | .forEach(camera -> {
|
---|
275 | if(Character.isDigit(camera.charAt(0)))
|
---|
276 | cameras.set(cameras.indexOf(camera), camera+"MP");
|
---|
277 |
|
---|
278 | });
|
---|
279 |
|
---|
280 | return cameras;
|
---|
281 | }
|
---|
282 |
|
---|
283 | public List<String> getBatteries() {
|
---|
284 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
285 |
|
---|
286 | return offers.stream()
|
---|
287 | .map(PhoneOffer::getBattery)
|
---|
288 | .filter(battery -> battery != null && !battery.equals("") && !battery.equals("/"))
|
---|
289 | .map(battery -> battery.split(",")[0]
|
---|
290 | .split("\n")[0]
|
---|
291 | .replaceAll("'","")
|
---|
292 | .replaceAll("\t"," ")
|
---|
293 | .stripIndent())
|
---|
294 | .map(battery -> battery.replaceAll("battery", "").stripIndent())
|
---|
295 | .distinct()
|
---|
296 | .sorted(Comparator.reverseOrder())
|
---|
297 | .collect(Collectors.toList());
|
---|
298 | }
|
---|
299 |
|
---|
300 | public List<String> getOperatingSystems() {
|
---|
301 | List<PhoneOffer> offers = phoneOfferRepository.findAll();
|
---|
302 |
|
---|
303 | return offers.stream()
|
---|
304 | .map(PhoneOffer::getOperating_system)
|
---|
305 | .filter(os -> os != null && !os.equals("") && !os.equals("/"))
|
---|
306 | .map(os -> os.split("[,(-]")[0].stripIndent())
|
---|
307 | .distinct()
|
---|
308 | .sorted()
|
---|
309 | .collect(Collectors.toList());
|
---|
310 | }
|
---|
311 |
|
---|
312 | public ResponseEntity<Object> addOffer(PhoneOffer offer) {
|
---|
313 | phoneOfferRepository.save(offer);
|
---|
314 | return ResponseEntity.ok().build();
|
---|
315 | }
|
---|
316 |
|
---|
317 | public ResponseEntity<Object> deleteOffer(Long offerId) {
|
---|
318 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
319 |
|
---|
320 | if(!exists)
|
---|
321 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
322 |
|
---|
323 | phoneOfferRepository.deleteById(offerId);
|
---|
324 |
|
---|
325 | return ResponseEntity.ok().build();
|
---|
326 | }
|
---|
327 |
|
---|
328 | public List<PhoneOffer> getAllOffers() {
|
---|
329 | return phoneOfferRepository.findAll();
|
---|
330 | }
|
---|
331 |
|
---|
332 | public ResponseEntity<Object> addPhoneModelToOffer(Long offerId, Long phoneId) {
|
---|
333 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
334 |
|
---|
335 | if(!exists)
|
---|
336 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
337 |
|
---|
338 | PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
|
---|
339 |
|
---|
340 | exists = phoneRepository.existsById(phoneId);
|
---|
341 |
|
---|
342 | if(!exists)
|
---|
343 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
344 |
|
---|
345 | Phone phone = phoneRepository.findById(phoneId).get();
|
---|
346 |
|
---|
347 | offer.setPhone(phone);
|
---|
348 | phoneOfferRepository.save(offer);
|
---|
349 |
|
---|
350 | return ResponseEntity.ok().build();
|
---|
351 | }
|
---|
352 |
|
---|
353 | public ResponseEntity<Object> changePriceForOffer(Long offerId, int price) {
|
---|
354 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
355 |
|
---|
356 | if(!exists)
|
---|
357 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
358 |
|
---|
359 | PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
|
---|
360 | offer.setPrice(price);
|
---|
361 | phoneOfferRepository.save(offer);
|
---|
362 |
|
---|
363 | return ResponseEntity.ok().build();
|
---|
364 | }
|
---|
365 | }
|
---|