source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java@ 5201690

Last change on this file since 5201690 was 5201690, checked in by Marko <Marko@…>, 22 months ago

Admin and specifications controllers added

  • Property mode set to 100644
File size: 10.4 KB
Line 
1package finki.it.phoneluxbackend.services;
2
3import finki.it.phoneluxbackend.entities.Phone;
4import finki.it.phoneluxbackend.entities.PhoneOffer;
5import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
6import finki.it.phoneluxbackend.repositories.PhoneRepository;
7import org.springframework.http.ResponseEntity;
8import org.springframework.stereotype.Service;
9
10import java.util.*;
11import java.util.stream.Collectors;
12
13@Service
14public 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 .distinct()
234 .sorted()
235 .collect(Collectors.toList());
236 }
237
238 public List<String> getFrontCameras() {
239 List<PhoneOffer> offers = phoneOfferRepository.findAll();
240
241 return offers.stream()
242 .map(PhoneOffer::getFront_camera)
243 .filter(camera -> camera != null && !camera.equals("") && !camera.equals("/"))
244 .map(camera -> camera.split("MP")[0].stripIndent()+"MP")
245 .filter(camera -> !camera.contains("\n"))
246 .distinct()
247 .sorted()
248 .collect(Collectors.toList());
249 }
250
251 public List<String> getBackCameras() {
252 List<PhoneOffer> offers = phoneOfferRepository.findAll();
253
254 return offers.stream()
255 .map(PhoneOffer::getBack_camera)
256 .filter(camera -> camera != null && !camera.equals("") && !camera.equals("/"))
257 .map(camera -> camera.split("[\n,]")[0].replaceAll("\t",""))
258 .distinct()
259 .sorted()
260 .collect(Collectors.toList());
261 }
262
263 public List<String> getBatteries() {
264 List<PhoneOffer> offers = phoneOfferRepository.findAll();
265
266 return offers.stream()
267 .map(PhoneOffer::getBattery)
268 .filter(battery -> battery != null && !battery.equals("") && !battery.equals("/"))
269 .map(battery -> battery.split(",")[0].stripIndent())
270 .distinct()
271 .sorted(Comparator.reverseOrder())
272 .collect(Collectors.toList());
273 }
274
275 public List<String> getOperatingSystems() {
276 List<PhoneOffer> offers = phoneOfferRepository.findAll();
277
278 return offers.stream()
279 .map(PhoneOffer::getOperating_system)
280 .filter(os -> os != null && !os.equals("") && !os.equals("/"))
281 .map(os -> os.split("[,(-]")[0].stripIndent())
282 .distinct()
283 .sorted()
284 .collect(Collectors.toList());
285 }
286}
Note: See TracBrowser for help on using the repository browser.