From fb21a636a4c98b13719111ef6cfb60ac9d55848b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joe=20Ku=CC=88ng?= Date: Fri, 28 Apr 2023 10:44:37 +0200 Subject: [PATCH] Api changes, error with findAllByLocationName --- .../controller/LocationController.java | 19 +++++------ .../controller/UserController.java | 16 ++-------- .../controller/WaypointController.java | 24 ++++++++------ .../controller/WaypointVisitedController.java | 32 +++++++------------ .../progetto152/entity/WaypointsEntity.java | 4 +++ .../repository/WaypointRepository.java | 4 +++ .../progetto152/services/WaypointService.java | 13 ++++++-- src/main/resources/mysql.sql | 6 +++- src/main/resources/restApi.txt | 1 + 9 files changed, 62 insertions(+), 57 deletions(-) diff --git a/src/main/java/ch/progetto152/controller/LocationController.java b/src/main/java/ch/progetto152/controller/LocationController.java index 1578336..69a37dd 100644 --- a/src/main/java/ch/progetto152/controller/LocationController.java +++ b/src/main/java/ch/progetto152/controller/LocationController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/progetto152/locations") +@RequestMapping("/progetto152/location") public class LocationController { private final LocationService locationService; @@ -25,9 +25,10 @@ public class LocationController { return new ResponseEntity<>(Locations, HttpStatus.OK); } - @GetMapping("/{id}") - public ResponseEntity getLocationById(@PathVariable("id") Long id) { - LocationEntity location = locationService.getLocationById(id); + + @GetMapping("/{name}") + public ResponseEntity getLocationByName(@PathVariable("name") String name) { + LocationEntity location = locationService.getLocationByName(name); if (location != null) { return new ResponseEntity<>(location, HttpStatus.OK); } else { @@ -35,9 +36,9 @@ public class LocationController { } } - @GetMapping("?name={name}") - public ResponseEntity getLocationByName(@PathVariable("location") String name) { - LocationEntity location = locationService.getLocationByName(name); + @GetMapping("id/{id}") + public ResponseEntity getLocationById(@PathVariable("id") Long id) { + LocationEntity location = locationService.getLocationById(id); if (location != null) { return new ResponseEntity<>(location, HttpStatus.OK); } else { @@ -54,7 +55,7 @@ public class LocationController { return new ResponseEntity<>(createdLocation, HttpStatus.CREATED); } - @PutMapping("{id}") + @PutMapping("/{id}") public ResponseEntity updateLocation(@PathVariable("id") Long id, @RequestBody LocationEntity location) { LocationEntity location1 = locationService.updateLocation(id, location); if (location1 != null) { @@ -64,7 +65,7 @@ public class LocationController { } } - @DeleteMapping("{id}") + @DeleteMapping("/{id}") public ResponseEntity deleteLocation(@PathVariable("id") Long id) { boolean deleted = locationService.deleteLocation(id); if (deleted) { diff --git a/src/main/java/ch/progetto152/controller/UserController.java b/src/main/java/ch/progetto152/controller/UserController.java index 8b09aa5..b22f32e 100644 --- a/src/main/java/ch/progetto152/controller/UserController.java +++ b/src/main/java/ch/progetto152/controller/UserController.java @@ -11,7 +11,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/progetto152/users") +@RequestMapping("/progetto152/user") public class UserController { private final UserService userService; @@ -27,7 +27,7 @@ public class UserController { return new ResponseEntity<>(users, HttpStatus.OK); } - @GetMapping("/{id}") + @GetMapping("/id/{id}") public ResponseEntity getUserById(@PathVariable("id") Long id) { UserEntity user = userService.getUserById(id); if (user != null) { @@ -37,17 +37,7 @@ public class UserController { } } - @GetMapping("?name={name}") - public ResponseEntity getUserByName(@PathVariable("name") String name) { - UserEntity user = userService.getUserByName(name); - if (user != null) { - return new ResponseEntity<>(user, HttpStatus.OK); - } else { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - } - - @GetMapping("?username={username}") + @GetMapping("/{username}") public ResponseEntity getUserByUsername(@PathVariable("username") String username) { UserEntity user = userService.getUserByUsername(username); if (user != null) { diff --git a/src/main/java/ch/progetto152/controller/WaypointController.java b/src/main/java/ch/progetto152/controller/WaypointController.java index e3bf172..6574efd 100644 --- a/src/main/java/ch/progetto152/controller/WaypointController.java +++ b/src/main/java/ch/progetto152/controller/WaypointController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/progetto152/waypoints") +@RequestMapping("/progetto152/waypoint") public class WaypointController { private final WaypointService waypointService; @@ -19,25 +19,29 @@ public class WaypointController { this.waypointService = waypointService; } - @GetMapping("") - public ResponseEntity> getAllWaypoints() { - List waypoint = waypointService.getAllWaypoints(); + @GetMapping("/{location}") + public ResponseEntity> getAllWaypoints(@PathVariable("location") String location) { + List waypoint = waypointService.getAllWaypointsByLocation(location); return new ResponseEntity<>(waypoint, HttpStatus.OK); } - @GetMapping("/{id}") - public ResponseEntity getWaypointById(@PathVariable("id") Long id) { + @GetMapping("/{location}/{id}") + public ResponseEntity getWaypointById(@PathVariable("location") String location, @PathVariable("id") Long id) { WaypointsEntity waypoint = waypointService.getWaypointById(id); if (waypoint != null) { - return new ResponseEntity<>(waypoint, HttpStatus.OK); + if (waypoint.getLocationName().equals(location)) { + return new ResponseEntity<>(waypoint, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } } - @GetMapping("?name={name}") - public ResponseEntity getWaypointByName(@PathVariable("name") String name) { - WaypointsEntity waypoint = waypointService.getWaypointByName(name); + @GetMapping("/id/{id}") + public ResponseEntity getWaypointById(@PathVariable("id") Long id) { + WaypointsEntity waypoint = waypointService.getWaypointById(id); if (waypoint != null) { return new ResponseEntity<>(waypoint, HttpStatus.OK); } else { diff --git a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java index b5db726..56e362f 100644 --- a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java +++ b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java @@ -10,7 +10,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/progetto152/waypointsvisited") +@RequestMapping("/progetto152/waypoint/visited") public class WaypointVisitedController { private final WaypointVisitedService waypointVisitedService; @@ -26,26 +26,6 @@ public class WaypointVisitedController { } @GetMapping("/{id}") - public ResponseEntity getWaypointVisitedById(@PathVariable("id") Long id) { - WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointById(id); - if (waypointVisited != null) { - return new ResponseEntity<>(waypointVisited, HttpStatus.OK); - } else { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - } - - @GetMapping("?userid={id}") - public ResponseEntity getWaypointVisitedByUserId(@PathVariable("id") Long id) { - WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id); - if (waypointVisited != null) { - return new ResponseEntity<>(waypointVisited, HttpStatus.OK); - } else { - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - } - - @GetMapping("?waypointid={id}") public ResponseEntity getWaypointVisitedByWaypointId(@PathVariable("id") Long id) { WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id); if (waypointVisited != null) { @@ -55,6 +35,16 @@ public class WaypointVisitedController { } } + @GetMapping("/user/{id}") + public ResponseEntity getWaypointVisitedByUserId(@PathVariable("id") Long id) { + WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id); + if (waypointVisited != null) { + return new ResponseEntity<>(waypointVisited, HttpStatus.OK); + } else { + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + } + @PostMapping("") public ResponseEntity createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) { WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited); diff --git a/src/main/java/ch/progetto152/entity/WaypointsEntity.java b/src/main/java/ch/progetto152/entity/WaypointsEntity.java index 0358a2c..27f8933 100644 --- a/src/main/java/ch/progetto152/entity/WaypointsEntity.java +++ b/src/main/java/ch/progetto152/entity/WaypointsEntity.java @@ -61,4 +61,8 @@ public class WaypointsEntity { public int hashCode() { return Objects.hash(id, name, lat, lon, description, img, locationName); } + + public String getLocationName() { + return locationName; + } } diff --git a/src/main/java/ch/progetto152/repository/WaypointRepository.java b/src/main/java/ch/progetto152/repository/WaypointRepository.java index 462101c..6ca0e44 100644 --- a/src/main/java/ch/progetto152/repository/WaypointRepository.java +++ b/src/main/java/ch/progetto152/repository/WaypointRepository.java @@ -3,9 +3,13 @@ package ch.progetto152.repository; import ch.progetto152.entity.WaypointsEntity; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; import java.util.Optional; public interface WaypointRepository extends JpaRepository { Optional findWaypointByName(String name); + + List findAllByLocationName(String locationName); + } diff --git a/src/main/java/ch/progetto152/services/WaypointService.java b/src/main/java/ch/progetto152/services/WaypointService.java index b0b4fe8..1b371f2 100644 --- a/src/main/java/ch/progetto152/services/WaypointService.java +++ b/src/main/java/ch/progetto152/services/WaypointService.java @@ -23,13 +23,20 @@ public class WaypointService { return waypointRepository.findAll(); } + public List getAllWaypointsByLocation(String locationName) { + List waypoints = waypointRepository.findAllByLocationName(locationName); + if (waypoints.isEmpty()) { + return null; + }else { + return waypoints; + } + } + public WaypointsEntity getWaypointById(Long id) { return waypointRepository.findById(id).orElse(null); } - public WaypointsEntity getWaypointByName(String name) { - return waypointRepository.findWaypointByName(name).orElse(null); - } + public WaypointsEntity createWaypoint(WaypointsEntity waypoint) { if (errorChecking.checkWaypoint(waypoint)) { diff --git a/src/main/resources/mysql.sql b/src/main/resources/mysql.sql index 0bfef7e..998f076 100644 --- a/src/main/resources/mysql.sql +++ b/src/main/resources/mysql.sql @@ -68,7 +68,11 @@ insert into Waypoints (name, lat, lon, description, img, LocationName) values (' 'Descrizione del punto 3, un grandissimo', 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', 'Lugano'); +insert into Waypoints (id, name, lat, lon, description, img, locationName) values (4, 'Punto 2', 46.123, 8.123, + 'Descrizione del punto 4', + 'https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png', + 'Locarno'); insert into User (name, username, password) values ('Mario Rossi', 'mario.rossi', 'password'); insert into User (name, username, password) values ('Luca Bianchi', 'luca.bianchi', 'password'); -insert into User (name, username, password) values ('Giovanni Verdi', 'giovanni.verdi', 'password'); \ No newline at end of file +insert into User (name, username, password) values ('Giovanni Verdi', 'giovanni.verdi', 'password'); diff --git a/src/main/resources/restApi.txt b/src/main/resources/restApi.txt index 4a5f9cc..28230ec 100644 --- a/src/main/resources/restApi.txt +++ b/src/main/resources/restApi.txt @@ -1,6 +1,7 @@ get: progetto152/location progetto152/location/{location} + progetto152/location/id/{id} progetto152/waypoint/{location} progetto152/waypoint/{location}/{id} maybe: