Implemented api for all entities

This commit is contained in:
grata
2023-04-27 11:39:39 +02:00
parent 10fd85cf8f
commit 3a7586f495
7 changed files with 261 additions and 52 deletions

View File

@@ -1,15 +1,57 @@
package ch.progetto152.services;
import ch.progetto152.entity.Location;
import ch.progetto152.repository.LocationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LocationService {
public static List<Location> getLocations() {
return null;
private final LocationRepository locationRepository;
@Autowired
public LocationService(LocationRepository locationRepository) {
this.locationRepository = locationRepository;
}
public static void createLocation(Location location) {
public List<Location> getAllLocations() {
return locationRepository.findAll();
}
public Location getLocationByIdService(Long id) {
return locationRepository.findById(id).orElse(null);
}
public Location getLocationByNameService(String name){
return locationRepository.findLocationByLocation(name).orElse(null);
}
public Location createLocation(Location Location) {
return locationRepository.save(Location);
}
public Location updateLocation(Long id, Location Location) {
Location location1 = getLocationByIdService(id);
if (location1 != null) {
location1.setLocation(Location.getLocation());
location1.setRegion(Location.getRegion());
location1.setLat(Location.getLat());
location1.setLon(Location.getLon());
return locationRepository.save(location1);
} else {
return null;
}
}
public boolean deleteLocation(Long id) {
boolean exists = locationRepository.existsById(id);
if(!exists){
return false;
}
locationRepository.deleteById(id);
return true;
}
}

View File

@@ -1,13 +1,59 @@
package ch.progetto152.services;
import ch.progetto152.entity.Waypoints;
import ch.progetto152.repository.WaypointRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class WaypointService {
public static List<Waypoints> getWaypoints() {
return null;
private final WaypointRepository waypointRepository;
@Autowired
public WaypointService(WaypointRepository waypointRepository) {
this.waypointRepository = waypointRepository;
}
public List<Waypoints> getAllWaypoints() {
return waypointRepository.findAll();
}
public Waypoints getWaypointByIdService(Long id) {
return waypointRepository.findById(id).orElse(null);
}
public Waypoints getWaypointByNameService(String name) {
return waypointRepository.findWaypointByName(name).orElse(null);
}
public Waypoints createWaypoint(Waypoints waypoint) {
return waypointRepository.save(waypoint);
}
public Waypoints updateWaypoint(Long id, Waypoints waypoint) {
Waypoints waypoint1 = getWaypointByIdService(id);
if (waypoint1 != null) {
waypoint1.setName(waypoint.getName());
waypoint1.setLat(waypoint.getLat());
waypoint1.setLon(waypoint.getLon());
waypoint1.setDescription(waypoint.getDescription());
waypoint1.setImg(waypoint.getImg());
waypoint1.setLocationId(waypoint.getLocationId());
return waypointRepository.save(waypoint1);
} else {
return null;
}
}
public boolean deleteWaypoint(Long id) {
boolean exists = waypointRepository.existsById(id);
if (!exists) {
return false;
}
waypointRepository.deleteById(id);
return true;
}
}