Rev | Line | |
---|
[57e58a3] | 1 | package com.example.skychasemk.services;
|
---|
| 2 |
|
---|
| 3 | import com.example.skychasemk.model.Airport;
|
---|
| 4 | import com.example.skychasemk.repository.AirportRepository;
|
---|
| 5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
| 6 | import org.springframework.stereotype.Service;
|
---|
| 7 |
|
---|
| 8 | import java.util.List;
|
---|
| 9 | import java.util.Optional;
|
---|
| 10 |
|
---|
| 11 | @Service
|
---|
| 12 | public class AirportService {
|
---|
| 13 |
|
---|
| 14 | @Autowired
|
---|
| 15 | private AirportRepository airportRepository;
|
---|
| 16 |
|
---|
| 17 | // Get all airports
|
---|
| 18 | public List<Airport> getAllAirports() {
|
---|
| 19 | return airportRepository.findAll();
|
---|
| 20 | }
|
---|
| 21 |
|
---|
| 22 | // Get airport by ID
|
---|
| 23 | public Optional<Airport> getAirportById(Integer airportID) {
|
---|
| 24 | return airportRepository.findById(airportID);
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | // Save new airport
|
---|
| 28 | public Airport saveAirport(Airport airport) {
|
---|
| 29 | return airportRepository.save(airport);
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | // Update an airport
|
---|
| 33 | public Airport updateAirport(Integer airportID, Airport airport) {
|
---|
| 34 | if (airportRepository.existsById(airportID)) {
|
---|
| 35 | airport.setAirportID(airportID);
|
---|
| 36 | return airportRepository.save(airport);
|
---|
| 37 | } else {
|
---|
| 38 | throw new RuntimeException("Airport not found with id " + airportID);
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | // Delete airport
|
---|
| 43 | public void deleteAirport(Integer airportID) {
|
---|
| 44 | airportRepository.deleteById(airportID);
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.