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

Specification filters logic implemented

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.