source: phonelux-backend/src/main/java/finki/it/phoneluxbackend/services/PhoneOfferService.java@ 775e15e

Last change on this file since 775e15e was 775e15e, checked in by Marko <Marko@…>, 22 months ago

Added more controllers

  • Property mode set to 100644
File size: 2.9 KB
Line 
1package finki.it.phoneluxbackend.services;
2
3import finki.it.phoneluxbackend.entities.PhoneOffer;
4import finki.it.phoneluxbackend.repositories.PhoneOfferRepository;
5import finki.it.phoneluxbackend.repositories.PhoneRepository;
6import org.springframework.stereotype.Service;
7
8import java.util.ArrayList;
9import java.util.Comparator;
10import java.util.List;
11import java.util.Objects;
12import java.util.stream.Collectors;
13
14@Service
15public 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
29 return phoneRepository.findById(phoneId).get().getPhoneOffers()
30 .stream().sorted(Comparator.comparing(PhoneOffer::getPrice)).collect(Collectors.toList());
31 }
32
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
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
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 }
78}
Note: See TracBrowser for help on using the repository browser.