Changeset 34950c6


Ignore:
Timestamp:
09/22/22 03:14:04 (20 months ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
436e0da
Parents:
d66b8eb
Message:

Specification filters logic implemented

Location:
phonelux-backend/src/main/java/finki/it/phoneluxbackend
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneController.java

    rd66b8eb r34950c6  
    2424                                 @RequestParam(name = "sortBy", required = false) String sortBy,
    2525                                 @RequestParam(name = "priceRange", required = false) String priceRange,
    26                                  @RequestParam(name = "searchValue", required = false) String searchValue){
     26                                 @RequestParam(name = "searchValue", required = false) String searchValue,
     27                                 @RequestParam(name = "ram", required = false) String ram,
     28                                 @RequestParam(name = "rom", required = false) String rom,
     29                                 @RequestParam(name = "frontcamera", required = false) String frontcamera,
     30                                 @RequestParam(name = "backcamera", required = false) String backcamera,
     31                                 @RequestParam(name = "chipset", required = false) String chipset,
     32                                 @RequestParam(name = "cpu", required = false) String cpu,
     33                                 @RequestParam(name = "operatingsystem", required = false) String operatingsystem,
     34                                 @RequestParam(name = "color", required = false) String color,
     35                                 @RequestParam(name = "battery", required = false) String battery){
    2736
    28         return phoneService.getPhones(shops,brands,sortBy,priceRange,searchValue);
     37        return phoneService.getPhones(shops,brands,sortBy,priceRange,searchValue,
     38                ram, rom, frontcamera, backcamera, chipset, cpu, operatingsystem, color, battery);
    2939    }
    3040
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java

    rd66b8eb r34950c6  
    231231                .map(cpu -> cpu.split("\n")[0].stripIndent().replaceAll("\n",""))
    232232                .filter(cpu -> !cpu.contains("Snapdragon") && !cpu.contains("Exynos"))
     233                .filter(cpu -> Character.isAlphabetic(cpu.charAt(0)))
    233234                .distinct()
    234235                .sorted()
     
    252253        List<PhoneOffer> offers = phoneOfferRepository.findAll();
    253254
    254         return offers.stream()
     255        List<String> cameras = offers.stream()
    255256                .map(PhoneOffer::getBack_camera)
    256257                .filter(camera -> camera != null && !camera.equals("") && !camera.equals("/"))
    257258                .map(camera -> camera.split("[\n,]")[0].replaceAll("\t",""))
    258                 .distinct()
    259                 .sorted()
    260                 .collect(Collectors.toList());
     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;
    261273    }
    262274
     
    267279                .map(PhoneOffer::getBattery)
    268280                .filter(battery -> battery != null && !battery.equals("") && !battery.equals("/"))
    269                 .map(battery -> battery.split(",")[0].stripIndent())
     281                .map(battery -> battery.split(",")[0]
     282                        .split("\n")[0]
     283                        .replaceAll("'","")
     284                        .replaceAll("\t"," ")
     285                        .stripIndent())
     286                .map(battery -> battery.replaceAll("battery", "").stripIndent())
    270287                .distinct()
    271288                .sorted(Comparator.reverseOrder())
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java

    rd66b8eb r34950c6  
    88import org.springframework.stereotype.Service;
    99
     10import java.util.Arrays;
    1011import java.util.Comparator;
    1112import java.util.List;
     
    2324
    2425    // TODO: insert logic to filter
    25     public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue){
     26    public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue,
     27                                 String ram, String rom, String frontcamera, String backcamera, String chipset,
     28                                 String cpu, String operatingsystem, String color, String battery){
    2629        List<Phone> phones = phoneRepository.findAll();
    2730
     
    5558                    .collect(Collectors.toList());
    5659        }
     60
     61
     62        // specifications filters
     63
     64        if(ram != null){
     65            String [] memories = ram.split(",");
     66            phones = phones.stream()
     67                    .filter(phone -> Arrays.stream(memories).anyMatch(memory -> phone.getPhoneOffers().stream()
     68                                    .filter(offer -> offer.getRam_memory() != null)
     69                                    .anyMatch(offer -> hasSpecification(offer.getRam_memory(),memory))
     70                            )
     71                    )
     72                    .collect(Collectors.toList());
     73        }
     74
     75        if(rom != null){
     76            String [] memories = rom.split(",");
     77            phones = phones.stream()
     78                    .filter(phone -> Arrays.stream(memories).anyMatch(memory -> phone.getPhoneOffers().stream()
     79                            .filter(offer -> offer.getRom_memory() != null)
     80                            .anyMatch(offer ->  hasSpecification(offer.getRom_memory(),memory))))
     81                    .collect(Collectors.toList());
     82        }
     83
     84        if(frontcamera != null){
     85            String [] cameras = frontcamera.split(",");
     86            phones = phones.stream()
     87                    .filter(phone -> Arrays.stream(cameras).anyMatch(camera -> phone.getPhoneOffers().stream()
     88                            .filter(offer -> offer.getFront_camera() != null)
     89                            .anyMatch(offer -> hasSpecification(offer.getFront_camera(),camera)
     90                            )
     91                    )
     92                    )
     93                    .collect(Collectors.toList());
     94        }
     95
     96        if(backcamera != null){
     97            String [] cameras = backcamera.split(",");
     98            phones = phones.stream()
     99                    .filter(phone -> Arrays.stream(cameras).anyMatch(camera -> phone.getPhoneOffers().stream()
     100                            .filter(offer -> offer.getBack_camera() != null)
     101                            .anyMatch(offer -> hasSpecification(offer.getBack_camera(),camera))))
     102                    .collect(Collectors.toList());
     103        }
     104
     105        if(chipset != null)
     106        {
     107            String [] chipsets = chipset.split(",");
     108            phones = phones.stream()
     109                    .filter(phone -> Arrays.stream(chipsets).anyMatch(chip -> phone.getPhoneOffers().stream()
     110                            .filter(offer -> offer.getChipset() != null)
     111                            .anyMatch(offer -> offer.getChipset().contains(chip))))
     112                    .collect(Collectors.toList());
     113        }
     114
     115        if(cpu != null)
     116        {
     117            String [] cpus = cpu.split(",");
     118            phones = phones.stream()
     119                    .filter(phone -> Arrays.stream(cpus).anyMatch(processor -> phone.getPhoneOffers().stream()
     120                            .filter(offer -> offer.getCpu() != null)
     121                            .anyMatch(offer -> offer.getCpu().contains(processor))))
     122                    .collect(Collectors.toList());
     123        }
     124
     125        if(operatingsystem != null)
     126        {
     127            String [] operatingSystems = operatingsystem.split(",");
     128            phones = phones.stream()
     129                    .filter(phone -> Arrays.stream(operatingSystems).anyMatch(os -> phone.getPhoneOffers().stream()
     130                            .filter(offer -> offer.getOperating_system() != null)
     131                            .anyMatch(offer -> offer.getOperating_system().contains(os))))
     132                    .collect(Collectors.toList());
     133        }
     134
     135        if(color != null)
     136        {
     137            String [] colors = color.split(",");
     138            phones = phones.stream()
     139                    .filter(phone -> Arrays.stream(colors).anyMatch(c -> phone.getPhoneOffers().stream()
     140                            .filter(offer -> offer.getColor() != null)
     141                            .anyMatch(offer -> offer.getColor().contains(c))))
     142                    .collect(Collectors.toList());
     143        }
     144
     145        if(battery != null)
     146        {
     147            String [] batteries = battery.split(",");
     148            phones = phones.stream()
     149                    .filter(phone -> Arrays.stream(batteries).anyMatch(b -> phone.getPhoneOffers().stream()
     150                            .filter(offer -> offer.getBattery() != null)
     151                            .anyMatch(offer -> offer.getBattery().contains(b))))
     152                    .collect(Collectors.toList());
     153        }
     154
    57155
    58156        phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
     
    74172
    75173        return phones;
     174    }
     175
     176    public boolean hasSpecification(String specification, String filter){
     177        if(specification.contains(filter))
     178        {
     179            if(specification.indexOf(filter)-1 < 0) {
     180                return true;
     181            }
     182
     183            if(!Character.isDigit(specification
     184                    .charAt(specification.indexOf(filter)-1))) {
     185                return true;
     186            }
     187        }
     188
     189        if(specification.contains(filter.split("GB")[0]+" GB"))
     190        {
     191            if(specification.indexOf(filter.split("GB")[0]+" GB")-1 < 0) {
     192                return true;
     193            }
     194
     195            if(!Character.isDigit(specification
     196                    .charAt(specification.indexOf(filter.split("GB")[0]+" GB")-1))) {
     197                return true;
     198            }
     199        }
     200
     201        if(specification.contains(filter.split("MP")[0]+" MP"))
     202        {
     203            if(specification.indexOf(filter.split("MP")[0]+" MP")-1 < 0) {
     204                return true;
     205            }
     206
     207            if(!Character.isDigit(specification
     208                    .charAt(specification.indexOf(filter.split("MP")[0]+" MP")-1))) {
     209                return true;
     210            }
     211        }
     212
     213        if(specification.contains(filter.split("MB")[0]+" MB"))
     214        {
     215            if(specification.indexOf(filter.split("MB")[0]+" MB")-1 < 0) {
     216                return true;
     217            }
     218
     219            if(!Character.isDigit(specification
     220                    .charAt(specification.indexOf(filter.split("MB")[0]+" MB")-1))) {
     221                return true;
     222            }
     223        }
     224
     225        return false;
    76226    }
    77227
Note: See TracChangeset for help on using the changeset viewer.