1 | package com.example.skychasemk.services;
|
---|
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 org.springframework.beans.factory.annotation.Autowired;
|
---|
8 | import org.springframework.stereotype.Service;
|
---|
9 |
|
---|
10 | import java.time.LocalDate;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Optional;
|
---|
13 |
|
---|
14 | @Service
|
---|
15 | public class FlightService {
|
---|
16 |
|
---|
17 | @Autowired
|
---|
18 | private FlightRepository flightRepository;
|
---|
19 |
|
---|
20 | @Autowired
|
---|
21 | private DestinationRepository destinationRepository;
|
---|
22 |
|
---|
23 | public Optional<Flight> getFlightById(Long flightID) {
|
---|
24 | return flightRepository.findById(flightID);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public Flight saveFlight(Flight flight) {
|
---|
28 | return flightRepository.save(flight);
|
---|
29 | }
|
---|
30 |
|
---|
31 | public Flight updateFlight(Long flightID, Flight flight) {
|
---|
32 | if (flightRepository.existsById(flightID)) {
|
---|
33 | flight.setFlightID(flightID);
|
---|
34 | return flightRepository.save(flight);
|
---|
35 | } else {
|
---|
36 | throw new RuntimeException("Flight not found with id " + flightID);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | public void deleteFlight(Long flightID) {
|
---|
41 | flightRepository.deleteById(flightID);
|
---|
42 | }
|
---|
43 |
|
---|
44 |
|
---|
45 |
|
---|
46 | public List<Flight> getFlights(String departureCity, String destination, LocalDate departureDate, LocalDate returnDate) {
|
---|
47 | List<Destination> destinations = destinationRepository.findAll();
|
---|
48 |
|
---|
49 | Integer departureID = Destination.getAirportID(departureCity, destinations);
|
---|
50 | Integer destinationID = Destination.getAirportID(destination, destinations);
|
---|
51 |
|
---|
52 | return flightRepository.findFlights(departureID, destinationID, departureDate);
|
---|
53 | }
|
---|
54 |
|
---|
55 | }
|
---|
56 |
|
---|