diff --git a/src/main/java/ch/progetto152/controller/LocationController.java b/src/main/java/ch/progetto152/controller/LocationController.java index 7a2cdfb..fd56a15 100644 --- a/src/main/java/ch/progetto152/controller/LocationController.java +++ b/src/main/java/ch/progetto152/controller/LocationController.java @@ -12,6 +12,8 @@ import java.util.List; @RestController @RequestMapping("/progetto152/location") public class LocationController { + + // Inject the LocationService private final LocationService locationService; @Autowired @@ -19,12 +21,14 @@ public class LocationController { this.locationService = locationService; } + // Handle GET request to get all locations @GetMapping("") public ResponseEntity> getAllLocations() { List Locations = locationService.getAllLocations(); return new ResponseEntity<>(Locations, HttpStatus.OK); } + // Handle GET request to get a specific location by name @GetMapping("/{name}") public ResponseEntity getLocationByName(@PathVariable("name") String name) { LocationEntity location = locationService.getLocationByName(name); @@ -35,15 +39,17 @@ public class LocationController { } } + // Handle POST request to create a new location @PostMapping("") public ResponseEntity createLocation(@RequestBody LocationEntity location) { LocationEntity createdLocation = locationService.createLocation(location); - if(createdLocation == null) { + if (createdLocation == null) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } return new ResponseEntity<>(createdLocation, HttpStatus.CREATED); } + // Handle PUT request to update an existing location @PutMapping("/{name}") public ResponseEntity updateLocation(@PathVariable("name") String name, @RequestBody LocationEntity location) { LocationEntity location1 = locationService.updateLocation(name, location); @@ -54,6 +60,7 @@ public class LocationController { } } + // Handle DELETE request to delete an existing location by name @DeleteMapping("/{name}") public ResponseEntity deleteLocation(@PathVariable("name") String name) { boolean deleted = locationService.deleteLocation(name); diff --git a/src/main/java/ch/progetto152/controller/UserController.java b/src/main/java/ch/progetto152/controller/UserController.java index 1dabf64..f0f6f6a 100644 --- a/src/main/java/ch/progetto152/controller/UserController.java +++ b/src/main/java/ch/progetto152/controller/UserController.java @@ -14,6 +14,8 @@ import java.util.List; @RequestMapping("/progetto152/user") public class UserController { + + // Inject the UserService private final UserService userService; @Autowired @@ -21,12 +23,14 @@ public class UserController { this.userService = userService; } + // Handle GET request to get all users @GetMapping("") public ResponseEntity> getAllUsers() { List users = userService.getAllUsers(); return new ResponseEntity<>(users, HttpStatus.OK); } + // Handle GET request to get user by id @GetMapping("/id/{id}") public ResponseEntity getUserById(@PathVariable("id") Long id) { UserEntity user = userService.getUserById(id); @@ -37,6 +41,7 @@ public class UserController { } } + // Handle GET request to get user by username @GetMapping("/{username}") public ResponseEntity getUserByUsername(@PathVariable("username") String username) { UserEntity user = userService.getUserByUsername(username); @@ -47,16 +52,18 @@ public class UserController { } } + // Handle POST request to create a new user @PostMapping("") public ResponseEntity createUser(@RequestBody UserEntity user) { UserEntity createdUser = userService.createUser(user); - if(createdUser == null) { + if (createdUser == null) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } createdUser.setAdmin(0); return new ResponseEntity<>(createdUser, HttpStatus.CREATED); } + // Handle PUT request to update an existing user @PutMapping("/{id}") public ResponseEntity updateUser(@PathVariable("id") Long id, @RequestBody UserEntity user) { UserEntity updatedUser = userService.updateUser(id, user); @@ -67,6 +74,7 @@ public class UserController { } } + // Handle DELETE request to delete an existing user @DeleteMapping("/{id}") public ResponseEntity deleteUser(@PathVariable("id") Long id) { boolean deleted = userService.deleteUser(id); diff --git a/src/main/java/ch/progetto152/controller/WaypointController.java b/src/main/java/ch/progetto152/controller/WaypointController.java index 527205a..e4df318 100644 --- a/src/main/java/ch/progetto152/controller/WaypointController.java +++ b/src/main/java/ch/progetto152/controller/WaypointController.java @@ -12,6 +12,8 @@ import java.util.List; @RestController @RequestMapping("/progetto152/waypoint") public class WaypointController { + + // Iject the WaypointService instance private final WaypointService waypointService; @Autowired @@ -19,12 +21,14 @@ public class WaypointController { this.waypointService = waypointService; } + // Handle the GET request to get all waypoints @GetMapping("") public ResponseEntity> getAllWaypoints() { List waypoint = waypointService.getAllWaypoints(); return new ResponseEntity<>(waypoint, HttpStatus.OK); } + // Handle the GET request to get all waypoints by location @GetMapping("/{location}") public ResponseEntity> getAllWaypoints(@PathVariable("location") String location) { System.out.println(waypointService.getAllWaypointsByLocation(location)); @@ -32,6 +36,8 @@ public class WaypointController { return new ResponseEntity<>(waypoint, HttpStatus.OK); } + + // Handle the GET request to get a specific waypoint by id @GetMapping("/{location}/{id}") public ResponseEntity getWaypoint(@PathVariable("location") String location, @PathVariable("id") Long id) { WaypointsEntity waypoint = waypointService.getWaypointById(id); @@ -46,6 +52,7 @@ public class WaypointController { } } + // Handle the GET request to get a specific waypoint by id @GetMapping("/id/{id}") public ResponseEntity getWaypointById(@PathVariable("id") Long id) { WaypointsEntity waypoint = waypointService.getWaypointById(id); @@ -56,6 +63,7 @@ public class WaypointController { } } + // Handle the POST request to create a new waypoint @PostMapping("") public ResponseEntity createWaypoint(@RequestBody WaypointsEntity waypoint) { WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint); @@ -65,6 +73,7 @@ public class WaypointController { return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); } + // Handle the PUT request to update an existing waypoint @PutMapping("/{id}") public ResponseEntity updateWaypoint(@PathVariable("id") Long id, @RequestBody WaypointsEntity waypoint) { WaypointsEntity updatedWaypoint = waypointService.updateWaypoint(id, waypoint); @@ -75,6 +84,7 @@ public class WaypointController { } } + // Handle the DELETE request to delete an existing waypoint @DeleteMapping("/{id}") public ResponseEntity deleteWaypoint(@PathVariable("id") Long id) { boolean deleted = waypointService.deleteWaypoint(id); diff --git a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java index b4358bb..a048979 100644 --- a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java +++ b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java @@ -14,6 +14,8 @@ import java.util.List; @RestController @RequestMapping("/progetto152/waypoint/visited") public class WaypointVisitedController { + + // Iject the WaypointVisitedService and UserService private final WaypointVisitedService waypointVisitedService; private final UserService userService; @@ -24,14 +26,14 @@ public class WaypointVisitedController { this.userService = userService; } + // Handle GET request to get all waypoints visited @GetMapping("") public ResponseEntity> getAllWaypointsVisited() { List waypointVisited = waypointVisitedService.getAllWaypointsVisited(); return new ResponseEntity<>(waypointVisited, HttpStatus.OK); } - - + // Handle GET request to get all waypoints visited by user @GetMapping("/{id}") public ResponseEntity getWaypointVisitedByWaypointId(@PathVariable("id") Long id) { WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByWaypointId(id); @@ -42,6 +44,7 @@ public class WaypointVisitedController { } } + // Handle GET request to get all waypoints visited by user @GetMapping("/{user}/{id}") public ResponseEntity getWaypointVisitedByWaypointIdAndUserId(@PathVariable("user") String username, @PathVariable("id") Long waypointId) { UserEntity user = userService.getUserByUsername(username); @@ -55,6 +58,7 @@ public class WaypointVisitedController { } } + // Handle GET request to get all waypoints visited by user @GetMapping("/user/{id}") public ResponseEntity getWaypointVisitedByUserId(@PathVariable("id") Long id) { WaypointsVisitedEntity waypointVisited = waypointVisitedService.getWaypointsVisitedByUserId(id); @@ -65,6 +69,7 @@ public class WaypointVisitedController { } } + // Handle POST request to create a new waypoint visited @PostMapping("") public ResponseEntity createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) { WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited); @@ -74,6 +79,7 @@ public class WaypointVisitedController { return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); } + // Handle PUT request to update a waypoint visited @PutMapping("/{id}") public ResponseEntity updateWaypointVisited(@PathVariable("id") Long id, @RequestBody WaypointsVisitedEntity waypointVisited) { WaypointsVisitedEntity updatedWaypoint = waypointVisitedService.updateWaypoint(id, waypointVisited); @@ -84,6 +90,7 @@ public class WaypointVisitedController { } } + // Handle DELETE request to delete a waypoint visited @DeleteMapping("/{id}") public ResponseEntity deleteWaypointVisited(@PathVariable("id") Long id) { boolean deleted = waypointVisitedService.deleteWaypoint(id); diff --git a/src/main/java/ch/progetto152/services/LocationService.java b/src/main/java/ch/progetto152/services/LocationService.java index 8b02b0b..53d4613 100644 --- a/src/main/java/ch/progetto152/services/LocationService.java +++ b/src/main/java/ch/progetto152/services/LocationService.java @@ -11,7 +11,7 @@ import java.util.List; @Service public class LocationService { - private ErrorChecking errorChecking = new ErrorChecking(); + private final ErrorChecking errorChecking = new ErrorChecking(); private final LocationRepository locationRepository; @Autowired diff --git a/src/main/java/ch/progetto152/services/WaypointVisitedService.java b/src/main/java/ch/progetto152/services/WaypointVisitedService.java index 35f0dc7..0a85d41 100644 --- a/src/main/java/ch/progetto152/services/WaypointVisitedService.java +++ b/src/main/java/ch/progetto152/services/WaypointVisitedService.java @@ -1,6 +1,5 @@ package ch.progetto152.services; -import ch.progetto152.entity.UserEntity; import ch.progetto152.entity.WaypointsVisitedEntity; import ch.progetto152.repository.WaypointVisitedRepository; import ch.progetto152.utility.ErrorChecking; @@ -63,13 +62,8 @@ public class WaypointVisitedService { return true; } - public Boolean getWaypointsVisitedByWaypointIdAndUserId(Long waypointId, Long userId) { WaypointsVisitedEntity waypointsVisited = waypointVisitedRepository.findWaypointsVisitedEntitiesByUserIdAndWaypointId(userId, waypointId).orElse(null); - if (waypointsVisited != null) { - return true; - } else { - return false; - } + return waypointsVisited != null; } } diff --git a/src/main/java/ch/progetto152/utility/ErrorChecking.java b/src/main/java/ch/progetto152/utility/ErrorChecking.java index 1993d19..cd8ce2f 100644 --- a/src/main/java/ch/progetto152/utility/ErrorChecking.java +++ b/src/main/java/ch/progetto152/utility/ErrorChecking.java @@ -6,6 +6,8 @@ import ch.progetto152.entity.UserEntity; import ch.progetto152.entity.WaypointsEntity; import ch.progetto152.entity.WaypointsVisitedEntity; + +// This class is used to check if the data received from the client is valid public class ErrorChecking { public boolean checkUser(UserEntity user){