[b68ae8d] | 1 | package finki.it.phoneluxbackend.controllers;
|
---|
| 2 |
|
---|
| 3 | import finki.it.phoneluxbackend.entities.Phone;
|
---|
| 4 | import finki.it.phoneluxbackend.entities.PhoneOffer;
|
---|
[f25d07e] | 5 | import finki.it.phoneluxbackend.services.PhoneOfferService;
|
---|
[b68ae8d] | 6 | import finki.it.phoneluxbackend.services.PhoneService;
|
---|
[f25d07e] | 7 | import lombok.AllArgsConstructor;
|
---|
[b68ae8d] | 8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 9 | import org.springframework.web.bind.annotation.*;
|
---|
| 10 |
|
---|
[f25d07e] | 11 | import java.util.Comparator;
|
---|
[b68ae8d] | 12 | import java.util.List;
|
---|
[f25d07e] | 13 | import java.util.stream.Collectors;
|
---|
[b68ae8d] | 14 |
|
---|
| 15 | @RestController
|
---|
[f25d07e] | 16 | @AllArgsConstructor
|
---|
[b68ae8d] | 17 | @RequestMapping(path = "/")
|
---|
| 18 | public class PhoneController {
|
---|
| 19 | private final PhoneService phoneService;
|
---|
[f25d07e] | 20 | private final PhoneOfferService phoneOfferService;
|
---|
[b68ae8d] | 21 |
|
---|
[f25d07e] | 22 | // handle request parameters for filtering phones
|
---|
| 23 | @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());
|
---|
[b68ae8d] | 28 | }
|
---|
| 29 |
|
---|
[f25d07e] | 30 | @GetMapping(path = "/phones/{phoneId}")
|
---|
| 31 | public Phone getPhoneById(@PathVariable("phoneId") Long phoneId)
|
---|
| 32 | {
|
---|
| 33 | return phoneService.getPhoneById(phoneId);
|
---|
| 34 | }
|
---|
[dfd5d87] | 35 |
|
---|
[f25d07e] | 36 | @GetMapping(path = "/brands")
|
---|
| 37 | public List<String> getBrands(){
|
---|
| 38 | return phoneService.getBrands();
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | @GetMapping(path = "/shops")
|
---|
| 42 | public List<String> getShops(){
|
---|
| 43 | return phoneOfferService.getShops();
|
---|
[b68ae8d] | 44 | }
|
---|
| 45 |
|
---|
[f25d07e] | 46 | @GetMapping(path = "/lowestPrice")
|
---|
| 47 | public int getLowestPrice()
|
---|
| 48 | {
|
---|
| 49 | return phoneOfferService.getLowestPrice();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | @GetMapping(path = "/highestPrice")
|
---|
| 53 | public int getHighestPrice()
|
---|
| 54 | {
|
---|
| 55 | return phoneOfferService.getHighestPrice();
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 |
|
---|
[b68ae8d] | 59 | }
|
---|