source: src/main/java/com/example/skychasemk/services/FlightService.java@ 07fe0be

Last change on this file since 07fe0be was 57e58a3, checked in by ste08 <sjovanoska@…>, 4 months ago

Initial commit

  • Property mode set to 100644
File size: 1.7 KB
Line 
1package com.example.skychasemk.services;
2
3import com.example.skychasemk.model.Destination;
4import com.example.skychasemk.model.Flight;
5import com.example.skychasemk.repository.DestinationRepository;
6import com.example.skychasemk.repository.FlightRepository;
7import org.springframework.beans.factory.annotation.Autowired;
8import org.springframework.stereotype.Service;
9
10import java.time.LocalDate;
11import java.util.List;
12import java.util.Optional;
13
14@Service
15public 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
Note: See TracBrowser for help on using the repository browser.