1 | package com.example.skychasemk.services;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.model.Destination;
|
---|
4 | import com.example.skychasemk.repository.DestinationRepository;
|
---|
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 DestinationService {
|
---|
13 |
|
---|
14 | @Autowired
|
---|
15 | private DestinationRepository destinationRepository;
|
---|
16 |
|
---|
17 | // Get all destinations
|
---|
18 | public List<Destination> getAllDestinations() {
|
---|
19 | return destinationRepository.findAll();
|
---|
20 | }
|
---|
21 |
|
---|
22 | // Get destination by ID
|
---|
23 | public Optional<Destination> getDestinationById(Integer destinationID) {
|
---|
24 | return destinationRepository.findById(destinationID);
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Save a new destination
|
---|
28 | public Destination saveDestination(Destination destination) {
|
---|
29 | return destinationRepository.save(destination);
|
---|
30 | }
|
---|
31 |
|
---|
32 | // Update an existing destination
|
---|
33 | public Destination updateDestination(Integer destinationID, Destination destination) {
|
---|
34 | if (destinationRepository.existsById(destinationID)) {
|
---|
35 | destination.setDestinationID(destinationID);
|
---|
36 | return destinationRepository.save(destination);
|
---|
37 | } else {
|
---|
38 | throw new RuntimeException("Destination not found with id " + destinationID);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Delete destination
|
---|
43 | public void deleteDestination(Integer destinationID) {
|
---|
44 | destinationRepository.deleteById(destinationID);
|
---|
45 | }
|
---|
46 |
|
---|
47 | public List<String> getAllCities() {
|
---|
48 | return destinationRepository.findAllCities();
|
---|
49 | }
|
---|
50 | }
|
---|