1 | package com.example.skychasemk.controller;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.model.Destination;
|
---|
4 | import com.example.skychasemk.model.Flight;
|
---|
5 | import com.example.skychasemk.repository.DestinationRepository;
|
---|
6 | import com.example.skychasemk.repository.FlightRepository;
|
---|
7 | import com.example.skychasemk.services.FlightService;
|
---|
8 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
9 | import org.springframework.format.annotation.DateTimeFormat;
|
---|
10 | import org.springframework.http.ResponseEntity;
|
---|
11 | import org.springframework.web.bind.annotation.*;
|
---|
12 |
|
---|
13 | import java.time.LocalDate;
|
---|
14 | import java.util.List;
|
---|
15 | import java.util.Optional;
|
---|
16 |
|
---|
17 | import static com.example.skychasemk.model.Destination.getAirportID;
|
---|
18 |
|
---|
19 | @RestController
|
---|
20 | @RequestMapping("/api/flights")
|
---|
21 | public class FlightController {
|
---|
22 |
|
---|
23 | @Autowired
|
---|
24 | private FlightService flightService;
|
---|
25 | @Autowired
|
---|
26 | private FlightRepository flightRepository;
|
---|
27 | @Autowired
|
---|
28 | private DestinationRepository destinationRepository;
|
---|
29 |
|
---|
30 | @GetMapping
|
---|
31 | public List<Flight> getAllFlights() {
|
---|
32 | return flightRepository.findAll();
|
---|
33 | }
|
---|
34 |
|
---|
35 | @GetMapping("/flights/{id}")
|
---|
36 | public Optional<Flight> getFlightById(@PathVariable("id") Long flightID) {
|
---|
37 | return flightService.getFlightById(flightID);
|
---|
38 | }
|
---|
39 |
|
---|
40 | @GetMapping("/flight-search")
|
---|
41 | public ResponseEntity<List<Flight>> searchFlights(
|
---|
42 | @RequestParam String departureCity,
|
---|
43 | @RequestParam String destination,
|
---|
44 | @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate departureDate,
|
---|
45 | @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate returnDate) {
|
---|
46 |
|
---|
47 | List<Destination> destinations = destinationRepository.findAll();
|
---|
48 |
|
---|
49 | Integer departureID = Integer.valueOf(getAirportID(departureCity, destinations));
|
---|
50 | Integer destinationID = Integer.valueOf(getAirportID(destination, destinations));
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | List<Flight> flights = flightRepository.findFlights(departureID,destinationID, departureDate);
|
---|
55 | return ResponseEntity.ok(flights);
|
---|
56 | }
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|