Ignore:
Timestamp:
09/11/22 18:03:58 (22 months ago)
Author:
Marko <Marko@…>
Branches:
master
Children:
775e15e
Parents:
527b93f
Message:

Prototype version

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

Legend:

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

    r527b93f re5b84dc  
    2222//     handle request parameters for filtering phones
    2323    @GetMapping(path = "/phones")
    24     public List<Phone> getPhones(){
    25         return phoneService.getPhones().stream()
    26                 .sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
    27                 .collect(Collectors.toList());
     24    public List<Phone> getPhones(@RequestParam(name = "shops", required = false) String shops,
     25                                 @RequestParam(name = "brands", required = false) String brands,
     26                                 @RequestParam(name = "sortBy", required = false) String sortBy,
     27                                 @RequestParam(name = "priceRange", required = false) String priceRange,
     28                                 @RequestParam(name = "searchValue", required = false) String searchValue){
     29
     30        return phoneService.getPhones(shops,brands,sortBy,priceRange,searchValue);
    2831    }
    2932
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/controllers/PhoneOfferController.java

    r527b93f re5b84dc  
    1616@RestController
    1717@AllArgsConstructor
    18 @RequestMapping(path = "/phones/offers/{phoneId}")
    1918public class PhoneOfferController {
    2019    private final PhoneOfferService phoneOfferService;
    2120    private final PhoneService phoneService;
    2221
    23     @GetMapping
     22    @GetMapping(path = "/phones/offers/{phoneId}")
    2423    public List<PhoneOffer> getOffersForPhone(@PathVariable("phoneId") Long phoneId){
    2524        return phoneOfferService.getPhoneOffersForPhone(phoneId);
    2625    }
    2726
     27    @GetMapping(path = "/phoneoffer/{offerId}")
     28    public PhoneOffer getPhoneOffer(@PathVariable("offerId") Long offerId){
     29        return phoneOfferService.getPhoneOffer(offerId);
     30    }
     31
    2832}
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/security/CustomAuthenticationFilter.java

    r527b93f re5b84dc  
    3333    @Override
    3434    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException {
    35         String email = request.getParameter("email"); // mozda ke treba da se smeni vo username
     35        String email = request.getParameter("email");
    3636        String password = request.getParameter("password");
    3737        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(email,password);
     
    4646        String access_token = JWT.create()
    4747                .withSubject(user.getEmail())
    48                 .withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 1000))
     48                .withExpiresAt(new Date(System.currentTimeMillis() + 10 * 60 * 100000)) // approx. 16.5 hours
    4949                .withIssuer(request.getRequestURL().toString())
    5050                .withClaim("role", user.getAuthorities().stream()
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java

    r527b93f re5b84dc  
    3030    }
    3131
     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
    3242    public List<String> getShops() {
    3343        return phoneOfferRepository.findAll().stream()
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneService.java

    r527b93f re5b84dc  
    1010import java.util.Comparator;
    1111import java.util.List;
     12import java.util.Objects;
    1213import java.util.stream.Collectors;
    1314
     
    2223
    2324    // TODO: insert logic to filter
    24     public List<Phone> getPhones(){
    25         return phoneRepository.findAll();
     25    public List<Phone> getPhones(String shops, String brands, String sortBy, String priceRange, String searchValue){
     26        List<Phone> phones = phoneRepository.findAll();
     27
     28
     29        if(brands != null)
     30        {
     31            phones = phones.stream()
     32                    .filter(phone -> brands.contains(phone.getBrand())).collect(Collectors.toList());
     33        }
     34
     35        if(shops != null)
     36        {
     37            phones = phones.stream()
     38                    .filter(phone -> phone.getPhoneOffers().stream().anyMatch(offer -> shops.contains(offer.getOffer_shop())))
     39                    .collect(Collectors.toList());
     40        }
     41
     42        if(priceRange != null)
     43        {
     44            int lowestPrice = Integer.parseInt(priceRange.split("-")[0]);
     45            int highestPrice = Integer.parseInt(priceRange.split("-")[1]);
     46            phones = phones.stream()
     47                    .filter(phone -> phone.getLowestPrice() >= lowestPrice && phone.getLowestPrice() <= highestPrice)
     48                    .collect(Collectors.toList());
     49        }
     50
     51        if(searchValue != null && !Objects.equals(searchValue.stripIndent(), "")){
     52            phones = phones.stream()
     53                    .filter(phone -> phone.getBrand().toLowerCase().contains(searchValue.stripIndent().toLowerCase())
     54                            || phone.getModel().toLowerCase().contains(searchValue.stripIndent().toLowerCase()))
     55                    .collect(Collectors.toList());
     56        }
     57
     58        phones = phones.stream().sorted(Comparator.comparing(Phone::getTotal_offers).reversed())
     59                .collect(Collectors.toList());
     60        if(sortBy != null)
     61        {
     62            if(sortBy.equals("ascending")) {
     63                phones = phones.stream()
     64                        .sorted(Comparator.comparing(Phone::getLowestPrice))
     65                        .collect(Collectors.toList());
     66            }
     67
     68            if(sortBy.equals("descending")) {
     69                phones = phones.stream()
     70                        .sorted(Comparator.comparing(Phone::getLowestPrice).reversed())
     71                        .collect(Collectors.toList());
     72            }
     73        }
     74
     75        return phones;
    2676    }
    2777
  • phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/UserService.java

    r527b93f re5b84dc  
    4040           User userToRegister =  userRepository.findByEmail(user.getEmail()).get();
    4141           if(userToRegister.getEnabled()) {
    42                return ResponseEntity.badRequest().body("Error: Email "+user.getEmail()+" already taken!");
     42               return ResponseEntity.badRequest().body("Error:Е-маил адресата е веќе зафатена!");
    4343           }
    4444           else {
    45                return ResponseEntity.badRequest().body("Email "+user.getEmail()+" not activated!" );
     45               return ResponseEntity.badRequest().body("Error:Профилот не е активиран. Потврдете на вашата е-маил адреса!" );
    4646           }
    4747       }
Note: See TracChangeset for help on using the changeset viewer.