Changeset 5201690 for phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java
- Timestamp:
- 09/21/22 18:46:15 (2 years ago)
- Branches:
- master
- Children:
- d66b8eb
- Parents:
- 7e88e46
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java
r7e88e46 r5201690 1 1 package finki.it.phoneluxbackend.services; 2 2 3 import finki.it.phoneluxbackend.entities.Phone; 3 4 import finki.it.phoneluxbackend.entities.PhoneOffer; 4 5 import finki.it.phoneluxbackend.repositories.PhoneOfferRepository; 5 6 import finki.it.phoneluxbackend.repositories.PhoneRepository; 7 import org.springframework.http.ResponseEntity; 6 8 import org.springframework.stereotype.Service; 7 9 8 import java.util.ArrayList; 9 import java.util.Comparator; 10 import java.util.List; 11 import java.util.Objects; 10 import java.util.*; 12 11 import java.util.stream.Collectors; 13 12 … … 76 75 .collect(Collectors.toList()); 77 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 } 78 286 }
Note:
See TracChangeset
for help on using the changeset viewer.