1 | package finki.it.phoneluxbackend.services;
|
---|
2 |
|
---|
3 | import finki.it.phoneluxbackend.entities.Phone;
|
---|
4 | import finki.it.phoneluxbackend.entities.PhoneOffer;
|
---|
5 | import finki.it.phoneluxbackend.repositories.PhoneRepository;
|
---|
6 | import org.springframework.data.domain.PageRequest;
|
---|
7 | import org.springframework.data.domain.Sort;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.util.Comparator;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.stream.Collectors;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class PhoneService {
|
---|
16 | private final PhoneRepository phoneRepository;
|
---|
17 |
|
---|
18 | public PhoneService(PhoneRepository phoneRepository){
|
---|
19 | this.phoneRepository = phoneRepository;
|
---|
20 | }
|
---|
21 |
|
---|
22 |
|
---|
23 | // TODO: insert logic to filter
|
---|
24 | public List<Phone> getPhones(){
|
---|
25 | return phoneRepository.findAll();
|
---|
26 | }
|
---|
27 |
|
---|
28 | public List<String> getBrands(){
|
---|
29 | return phoneRepository.findAll().stream()
|
---|
30 | .map(Phone::getBrand).distinct()
|
---|
31 | .collect(Collectors.toList());
|
---|
32 | }
|
---|
33 |
|
---|
34 | public Phone getPhoneById(Long phoneId) {
|
---|
35 | boolean exists = phoneRepository.existsById(phoneId);
|
---|
36 | if(!exists)
|
---|
37 | throw new IllegalStateException("Phone with id "+phoneId+" does not exist");
|
---|
38 | return phoneRepository.findById(phoneId).get();
|
---|
39 | }
|
---|
40 | }
|
---|