diff --git a/src/main/java/ch/progetto152/controller/Controller.java b/src/main/java/ch/progetto152/controller/Controller.java deleted file mode 100644 index e9dbba5..0000000 --- a/src/main/java/ch/progetto152/controller/Controller.java +++ /dev/null @@ -1,47 +0,0 @@ -package ch.progetto152.controller; - - -import ch.progetto152.entity.Location; -import ch.progetto152.entity.User; -import ch.progetto152.entity.Waypoints; -import ch.progetto152.services.LocationService; -import ch.progetto152.services.UserService; -import ch.progetto152.services.WaypointService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping(value = "api/progetto152", produces = MediaType.APPLICATION_JSON_VALUE) -public class Controller { - - private static final String location1 = "location"; - private static final String waypoint1 = "waypoint"; - - @Autowired - private UserService userService; - - - @GetMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getLocations() { - List locations = LocationService.getLocations(); - return new ResponseEntity<>(locations, HttpStatus.OK); - } - - @GetMapping(value= waypoint1, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity> getWaypoints() { - List waypoints = WaypointService.getWaypoints(); - return new ResponseEntity<>(waypoints, HttpStatus.OK); - } - - @PostMapping(value= location1, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity createLocation(@RequestBody Location location) { - LocationService.createLocation(location); - return new ResponseEntity<>(HttpStatus.CREATED); - } - -} diff --git a/src/main/java/ch/progetto152/controller/LocationController.java b/src/main/java/ch/progetto152/controller/LocationController.java new file mode 100644 index 0000000..737346f --- /dev/null +++ b/src/main/java/ch/progetto152/controller/LocationController.java @@ -0,0 +1,73 @@ +package ch.progetto152.controller; + +import ch.progetto152.entity.Location; +import ch.progetto152.services.LocationService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/locations") +public class LocationController { + private final LocationService locationService; + + @Autowired + public LocationController(LocationService locationService) { + this.locationService = locationService; + } + + @GetMapping("/get/all") + public ResponseEntity> getAllLocations() { + List Locations = locationService.getAllLocations(); + return new ResponseEntity<>(Locations, HttpStatus.OK); + } + + @GetMapping("/get/{id}") + public ResponseEntity getLocationById(@PathVariable("id") Long id) { + Location location = locationService.getLocationByIdService(id); + if (location != null) { + return new ResponseEntity<>(location, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @GetMapping("/get/name/{name}") + public ResponseEntity getLocationByName(@PathVariable("name") String name) { + Location location = locationService.getLocationByNameService(name); + if (location != null) { + return new ResponseEntity<>(location, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @PostMapping("/create/{id}") + public ResponseEntity createLocation(@RequestBody Location location) { + Location createdLocation = locationService.createLocation(location); + return new ResponseEntity<>(createdLocation, HttpStatus.CREATED); + } + + @PutMapping("/put/{id}") + public ResponseEntity updateLocation(@PathVariable("id") Long id, @RequestBody Location location) { + Location location1 = locationService.updateLocation(id, location); + if (location1 != null) { + return new ResponseEntity<>(location1, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteLocation(@PathVariable("id") Long id) { + boolean deleted = locationService.deleteLocation(id); + if (deleted) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } +} diff --git a/src/main/java/ch/progetto152/controller/WaypointController.java b/src/main/java/ch/progetto152/controller/WaypointController.java new file mode 100644 index 0000000..b65a93c --- /dev/null +++ b/src/main/java/ch/progetto152/controller/WaypointController.java @@ -0,0 +1,73 @@ +package ch.progetto152.controller; + +import ch.progetto152.entity.Waypoints; +import ch.progetto152.services.WaypointService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/waypoints") +public class WaypointController { + private final WaypointService waypointService; + + @Autowired + public WaypointController(WaypointService waypointService) { + this.waypointService = waypointService; + } + + @GetMapping("/get/all") + public ResponseEntity> getAllWaypoints() { + List waypoint = waypointService.getAllWaypoints(); + return new ResponseEntity<>(waypoint, HttpStatus.OK); + } + + @GetMapping("/get/{id}") + public ResponseEntity getWaypointById(@PathVariable("id") Long id) { + Waypoints waypoint = waypointService.getWaypointByIdService(id); + if (waypoint != null) { + return new ResponseEntity<>(waypoint, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @GetMapping("/get/name/{name}") + public ResponseEntity getWaypointByName(@PathVariable("name") String name) { + Waypoints waypoint = waypointService.getWaypointByNameService(name); + if (waypoint != null) { + return new ResponseEntity<>(waypoint, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @PostMapping("/create/{id}") + public ResponseEntity createWaypoint(@RequestBody Waypoints waypoint) { + Waypoints createdWaypoint = waypointService.createWaypoint(waypoint); + return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); + } + + @PutMapping("/put/{id}") + public ResponseEntity updateWaypoint(@PathVariable("id") Long id, @RequestBody Waypoints waypoint) { + Waypoints updatedWaypoint = waypointService.updateWaypoint(id, waypoint); + if (updatedWaypoint != null) { + return new ResponseEntity<>(updatedWaypoint, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + + @DeleteMapping("/delete/{id}") + public ResponseEntity deleteWaypoint(@PathVariable("id") Long id) { + boolean deleted = waypointService.deleteWaypoint(id); + if (deleted) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } +} diff --git a/src/main/java/ch/progetto152/repository/LocationRepository.java b/src/main/java/ch/progetto152/repository/LocationRepository.java new file mode 100644 index 0000000..d37bac5 --- /dev/null +++ b/src/main/java/ch/progetto152/repository/LocationRepository.java @@ -0,0 +1,11 @@ +package ch.progetto152.repository; + +import ch.progetto152.entity.Location; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface LocationRepository extends JpaRepository { + + Optional findLocationByLocation(String name); +} diff --git a/src/main/java/ch/progetto152/repository/WaypointRepository.java b/src/main/java/ch/progetto152/repository/WaypointRepository.java new file mode 100644 index 0000000..0e8ce27 --- /dev/null +++ b/src/main/java/ch/progetto152/repository/WaypointRepository.java @@ -0,0 +1,11 @@ +package ch.progetto152.repository; + +import ch.progetto152.entity.Waypoints; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface WaypointRepository extends JpaRepository { + + Optional findWaypointByName(String name); +} diff --git a/src/main/java/ch/progetto152/services/LocationService.java b/src/main/java/ch/progetto152/services/LocationService.java index a8719d1..8b505cb 100644 --- a/src/main/java/ch/progetto152/services/LocationService.java +++ b/src/main/java/ch/progetto152/services/LocationService.java @@ -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 getLocations() { - return null; + private final LocationRepository locationRepository; + + @Autowired + public LocationService(LocationRepository locationRepository) { + this.locationRepository = locationRepository; } - public static void createLocation(Location location) { + public List 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; } } diff --git a/src/main/java/ch/progetto152/services/WaypointService.java b/src/main/java/ch/progetto152/services/WaypointService.java index f737f67..314ee48 100644 --- a/src/main/java/ch/progetto152/services/WaypointService.java +++ b/src/main/java/ch/progetto152/services/WaypointService.java @@ -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 getWaypoints() { - return null; + private final WaypointRepository waypointRepository; + + @Autowired + public WaypointService(WaypointRepository waypointRepository) { + this.waypointRepository = waypointRepository; + } + + public List 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; } }