Changeset 5201690
- Timestamp:
- 09/21/22 18:46:15 (2 years ago)
- Branches:
- master
- Children:
- d66b8eb
- Parents:
- 7e88e46
- Location:
- phonelux-backend/src/main/java/finki/it/phoneluxbackend
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneOfferController.java
r7e88e46 r5201690 7 7 import lombok.AllArgsConstructor; 8 8 import org.springframework.beans.factory.annotation.Autowired; 9 import org.springframework.web.bind.annotation.GetMapping; 10 import org.springframework.web.bind.annotation.PathVariable; 11 import org.springframework.web.bind.annotation.RequestMapping; 12 import org.springframework.web.bind.annotation.RestController; 9 import org.springframework.web.bind.annotation.*; 13 10 14 11 import java.util.List; … … 22 19 public List<PhoneOffer> getOffersForPhone(@PathVariable("phoneId") Long phoneId){ 23 20 return phoneOfferService.getPhoneOffersForPhone(phoneId); 21 } 22 23 @GetMapping(path = "/multipleoffers") 24 public List<PhoneOffer> getPhoneOffer(@RequestParam("offerIds") String offerIds){ 25 return phoneOfferService.getMultiplePhoneOffers(offerIds); 26 } 27 28 @GetMapping(path = "/phoneoffer/shop/{shop}") 29 public List<PhoneOffer> getOffersFromShop(@PathVariable("shop") String shop){ 30 return phoneOfferService.getOffersFromShop(shop); 24 31 } 25 32 -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/PhoneOffer.java
r7e88e46 r5201690 14 14 @Setter 15 15 @ToString 16 @EqualsAndHashCode 16 17 @Entity(name = "PhoneOffer") 17 18 @Table(name = "phone_offers") … … 76 77 77 78 @ManyToMany(mappedBy = "favouriteOffers") 79 @JsonIgnore 78 80 private List<User> users = new ArrayList<User>(); 79 81 … … 112 114 this.phone = phone; 113 115 } 116 117 public PhoneOffer(Long id, 118 String offer_shop, 119 String offer_name, 120 Integer price, 121 String ram_memory, 122 String rom_memory, 123 String color, 124 String front_camera, 125 String back_camera, 126 String chipset, 127 String battery, 128 String operating_system, 129 String cpu, 130 String image_url, 131 String offer_url, 132 Date last_updated, 133 Boolean is_validated, 134 String offer_description, 135 String offer_shop_code) { 136 this.id = id; 137 this.offer_shop = offer_shop; 138 this.offer_name = offer_name; 139 this.price = price; 140 this.ram_memory = ram_memory; 141 this.rom_memory = rom_memory; 142 this.color = color; 143 this.front_camera = front_camera; 144 this.back_camera = back_camera; 145 this.chipset = chipset; 146 this.battery = battery; 147 this.operating_system = operating_system; 148 this.cpu = cpu; 149 this.image_url = image_url; 150 this.offer_url = offer_url; 151 this.last_updated = last_updated; 152 this.is_validated = is_validated; 153 this.offer_description = offer_description; 154 this.offer_shop_code = offer_shop_code; 155 } 114 156 } -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/entities/User.java
r7e88e46 r5201690 52 52 @JsonIgnore 53 53 private List<PhoneOffer> favouriteOffers = new ArrayList<PhoneOffer>(); 54 55 private String specifications; 54 56 55 57 public User(String firstName, String lastName, String email, String password, UserRole userRole) { -
phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/configs/WebSecurityConfig.java
r7e88e46 r5201690 43 43 .antMatchers("/management/**") 44 44 .hasAnyAuthority("SUPERADMIN") 45 .and() 46 .authorizeRequests() 47 .antMatchers("/admin/**") 48 .hasAnyAuthority("ADMIN","SUPERADMIN") 45 49 .anyRequest().permitAll(); 46 50 -
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.