source: src/main/java/com/example/skychasemk/services/DestinationService.java@ 62bba0c

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

Initial commit

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