[dfd5d87] | 1 | package finki.it.phoneluxbackend.services;
|
---|
| 2 |
|
---|
| 3 | import finki.it.phoneluxbackend.entities.PhoneOffer;
|
---|
| 4 | import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
|
---|
| 5 | import finki.it.phoneluxbackend.repositories.PhoneRepository;
|
---|
| 6 | import org.springframework.stereotype.Service;
|
---|
| 7 |
|
---|
| 8 | import java.util.ArrayList;
|
---|
[f25d07e] | 9 | import java.util.Comparator;
|
---|
[dfd5d87] | 10 | import java.util.List;
|
---|
[775e15e] | 11 | import java.util.Objects;
|
---|
[f25d07e] | 12 | import java.util.stream.Collectors;
|
---|
[dfd5d87] | 13 |
|
---|
| 14 | @Service
|
---|
| 15 | public class PhoneOfferService {
|
---|
| 16 | private final PhoneOfferRepository phoneOfferRepository;
|
---|
| 17 | private final PhoneRepository phoneRepository;
|
---|
| 18 |
|
---|
| 19 | public PhoneOfferService(PhoneOfferRepository phoneOfferRepository, PhoneRepository phoneRepository) {
|
---|
| 20 | this.phoneOfferRepository = phoneOfferRepository;
|
---|
| 21 | this.phoneRepository = phoneRepository;
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | public List<PhoneOffer> getPhoneOffersForPhone(Long phoneId) {
|
---|
| 25 | boolean exists = phoneRepository.existsById(phoneId);
|
---|
| 26 | if(!exists)
|
---|
| 27 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
| 28 |
|
---|
[f25d07e] | 29 | return phoneRepository.findById(phoneId).get().getPhoneOffers()
|
---|
| 30 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice)).collect(Collectors.toList());
|
---|
[dfd5d87] | 31 | }
|
---|
[f25d07e] | 32 |
|
---|
[e5b84dc] | 33 | public PhoneOffer getPhoneOffer(Long offerId){
|
---|
| 34 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
| 35 |
|
---|
| 36 | if(!exists)
|
---|
| 37 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
| 38 |
|
---|
| 39 | return phoneOfferRepository.findById(offerId).get();
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 |
|
---|
[f25d07e] | 43 | public List<String> getShops() {
|
---|
| 44 | return phoneOfferRepository.findAll().stream()
|
---|
| 45 | .map(PhoneOffer::getOffer_shop)
|
---|
| 46 | .distinct()
|
---|
| 47 | .collect(Collectors.toList());
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 | public int getLowestPrice() {
|
---|
| 52 | return phoneOfferRepository.findAll()
|
---|
| 53 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice))
|
---|
| 54 | .collect(Collectors.toList()).get(0).getPrice();
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public int getHighestPrice() {
|
---|
| 58 | return phoneOfferRepository.findAll()
|
---|
| 59 | .stream().sorted(Comparator.comparing(PhoneOffer::getPrice).reversed())
|
---|
| 60 | .collect(Collectors.toList()).get(0).getPrice();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[775e15e] | 63 | public List<PhoneOffer> getCheaperOffers(Long offerId) {
|
---|
| 64 | boolean exists = phoneOfferRepository.existsById(offerId);
|
---|
| 65 |
|
---|
| 66 | if(!exists)
|
---|
| 67 | throw new IllegalStateException("Phone offer with id "+offerId+" does not exist");
|
---|
| 68 |
|
---|
| 69 | PhoneOffer offer = phoneOfferRepository.findById(offerId).get();
|
---|
| 70 |
|
---|
| 71 | return phoneOfferRepository.findAll()
|
---|
| 72 | .stream().filter(phoneOffer ->
|
---|
| 73 | Objects.equals(phoneOffer.getPhone().getModel(), offer.getPhone().getModel())
|
---|
| 74 | && phoneOffer.getPrice() < offer.getPrice())
|
---|
| 75 | .sorted(Comparator.comparing(PhoneOffer::getPrice).reversed())
|
---|
| 76 | .collect(Collectors.toList());
|
---|
| 77 | }
|
---|
[dfd5d87] | 78 | }
|
---|