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