From 9b6cfc4fe2bd9ec9b0829c238c3a463bc8ef633e Mon Sep 17 00:00:00 2001 From: grata Date: Thu, 27 Apr 2023 16:12:49 +0200 Subject: [PATCH] Added error checking for post calls --- .../controller/LocationController.java | 3 ++ .../controller/UserController.java | 3 ++ .../controller/WaypointController.java | 3 ++ .../controller/WaypointVisitedController.java | 3 ++ .../progetto152/services/LocationService.java | 8 +++- .../ch/progetto152/services/UserService.java | 8 +++- .../progetto152/services/WaypointService.java | 8 +++- .../services/WaypointVisitedService.java | 8 +++- .../ch/progetto152/utility/ErrorChecking.java | 44 +++++++++++++++++++ 9 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ch/progetto152/utility/ErrorChecking.java diff --git a/src/main/java/ch/progetto152/controller/LocationController.java b/src/main/java/ch/progetto152/controller/LocationController.java index 48d5a8e..1578336 100644 --- a/src/main/java/ch/progetto152/controller/LocationController.java +++ b/src/main/java/ch/progetto152/controller/LocationController.java @@ -48,6 +48,9 @@ public class LocationController { @PostMapping("") public ResponseEntity createLocation(@RequestBody LocationEntity location) { LocationEntity createdLocation = locationService.createLocation(location); + if(createdLocation == null) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } return new ResponseEntity<>(createdLocation, HttpStatus.CREATED); } diff --git a/src/main/java/ch/progetto152/controller/UserController.java b/src/main/java/ch/progetto152/controller/UserController.java index af0d154..8b09aa5 100644 --- a/src/main/java/ch/progetto152/controller/UserController.java +++ b/src/main/java/ch/progetto152/controller/UserController.java @@ -60,6 +60,9 @@ public class UserController { @PostMapping("") public ResponseEntity createUser(@RequestBody UserEntity user) { UserEntity createdUser = userService.createUser(user); + if(createdUser == null) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } return new ResponseEntity<>(createdUser, HttpStatus.CREATED); } diff --git a/src/main/java/ch/progetto152/controller/WaypointController.java b/src/main/java/ch/progetto152/controller/WaypointController.java index 6d6df7c..e3bf172 100644 --- a/src/main/java/ch/progetto152/controller/WaypointController.java +++ b/src/main/java/ch/progetto152/controller/WaypointController.java @@ -48,6 +48,9 @@ public class WaypointController { @PostMapping("") public ResponseEntity createWaypoint(@RequestBody WaypointsEntity waypoint) { WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint); + if (createdWaypoint == null) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); } diff --git a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java index 551e9e3..b5db726 100644 --- a/src/main/java/ch/progetto152/controller/WaypointVisitedController.java +++ b/src/main/java/ch/progetto152/controller/WaypointVisitedController.java @@ -58,6 +58,9 @@ public class WaypointVisitedController { @PostMapping("") public ResponseEntity createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) { WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited); + if(createdWaypoint == null) { + return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); + } return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED); } diff --git a/src/main/java/ch/progetto152/services/LocationService.java b/src/main/java/ch/progetto152/services/LocationService.java index 96cae85..edd14a8 100644 --- a/src/main/java/ch/progetto152/services/LocationService.java +++ b/src/main/java/ch/progetto152/services/LocationService.java @@ -2,6 +2,7 @@ package ch.progetto152.services; import ch.progetto152.entity.LocationEntity; import ch.progetto152.repository.LocationRepository; +import ch.progetto152.utility.ErrorChecking; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -10,6 +11,7 @@ import java.util.List; @Service public class LocationService { + private ErrorChecking errorChecking = new ErrorChecking(); private final LocationRepository locationRepository; @Autowired @@ -30,7 +32,11 @@ public class LocationService { } public LocationEntity createLocation(LocationEntity Location) { - return locationRepository.save(Location); + if(errorChecking.checkLocation(Location)) { + return locationRepository.save(Location); + } else { + return null; + } } public LocationEntity updateLocation(Long id, LocationEntity Location) { diff --git a/src/main/java/ch/progetto152/services/UserService.java b/src/main/java/ch/progetto152/services/UserService.java index 7b8bd17..74d2802 100644 --- a/src/main/java/ch/progetto152/services/UserService.java +++ b/src/main/java/ch/progetto152/services/UserService.java @@ -3,6 +3,7 @@ package ch.progetto152.services; import ch.progetto152.entity.UserEntity; import ch.progetto152.repository.UserRepository; +import ch.progetto152.utility.ErrorChecking; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -11,6 +12,7 @@ import java.util.List; @Service public class UserService { + ErrorChecking errorChecking = new ErrorChecking(); private final UserRepository userRepository; @Autowired @@ -35,7 +37,11 @@ public class UserService { } public UserEntity createUser(UserEntity user) { - return userRepository.save(user); + if(errorChecking.checkUser(user)) { + return userRepository.save(user); + } else { + return null; + } } public UserEntity updateUser(Long id, UserEntity user) { diff --git a/src/main/java/ch/progetto152/services/WaypointService.java b/src/main/java/ch/progetto152/services/WaypointService.java index 99b182b..b0b4fe8 100644 --- a/src/main/java/ch/progetto152/services/WaypointService.java +++ b/src/main/java/ch/progetto152/services/WaypointService.java @@ -2,6 +2,7 @@ package ch.progetto152.services; import ch.progetto152.entity.WaypointsEntity; import ch.progetto152.repository.WaypointRepository; +import ch.progetto152.utility.ErrorChecking; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,6 +10,8 @@ import java.util.List; @Service public class WaypointService { + + ErrorChecking errorChecking = new ErrorChecking(); private final WaypointRepository waypointRepository; @Autowired @@ -29,7 +32,10 @@ public class WaypointService { } public WaypointsEntity createWaypoint(WaypointsEntity waypoint) { - return waypointRepository.save(waypoint); + if (errorChecking.checkWaypoint(waypoint)) { + return waypointRepository.save(waypoint); + } + return null; } public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) { diff --git a/src/main/java/ch/progetto152/services/WaypointVisitedService.java b/src/main/java/ch/progetto152/services/WaypointVisitedService.java index 6a5d028..0f5aced 100644 --- a/src/main/java/ch/progetto152/services/WaypointVisitedService.java +++ b/src/main/java/ch/progetto152/services/WaypointVisitedService.java @@ -2,6 +2,7 @@ package ch.progetto152.services; import ch.progetto152.entity.WaypointsVisitedEntity; import ch.progetto152.repository.WaypointVisitedRepository; +import ch.progetto152.utility.ErrorChecking; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,6 +10,8 @@ import java.util.List; @Service public class WaypointVisitedService { + + ErrorChecking errorChecking = new ErrorChecking(); private final WaypointVisitedRepository waypointVisitedRepository; @Autowired @@ -33,7 +36,10 @@ public class WaypointVisitedService { } public WaypointsVisitedEntity createWaypoint(WaypointsVisitedEntity waypoint) { - return waypointVisitedRepository.save(waypoint); + if (errorChecking.checkWaypointVisited(waypoint)) { + return waypointVisitedRepository.save(waypoint); + } + return null; } public WaypointsVisitedEntity updateWaypoint(Long id, WaypointsVisitedEntity waypoint) { diff --git a/src/main/java/ch/progetto152/utility/ErrorChecking.java b/src/main/java/ch/progetto152/utility/ErrorChecking.java new file mode 100644 index 0000000..bddacd2 --- /dev/null +++ b/src/main/java/ch/progetto152/utility/ErrorChecking.java @@ -0,0 +1,44 @@ +package ch.progetto152.utility; + + +import ch.progetto152.entity.LocationEntity; +import ch.progetto152.entity.UserEntity; +import ch.progetto152.entity.WaypointsEntity; +import ch.progetto152.entity.WaypointsVisitedEntity; + +public class ErrorChecking { + + public boolean checkUser(UserEntity user){ + return user != null && + isNotEmpty(user.getName()) && + isNotEmpty(user.getUsername()) && + isNotEmpty(user.getPassword()); + } + + public boolean checkLocation(LocationEntity location){ + return location != null && + isNotEmpty(location.getLocation()) && + isNotEmpty(location.getRegion()) && + isNotEmpty(String.valueOf(location.getLat())) && + isNotEmpty(String.valueOf(location.getLon())); + } + + public boolean checkWaypoint(WaypointsEntity waypoint){ + return waypoint != null && + isNotEmpty(waypoint.getLocationName()) && + isNotEmpty(waypoint.getDescription()) && + isNotEmpty(waypoint.getImg()) && + isNotEmpty(String.valueOf(waypoint.getLat())) && + isNotEmpty(String.valueOf(waypoint.getLon())); + } + + public boolean checkWaypointVisited(WaypointsVisitedEntity waypointsVisited){ + return waypointsVisited != null && + isNotEmpty(String.valueOf(waypointsVisited.getWaypointId())) && + isNotEmpty(String.valueOf(waypointsVisited.getUserId())); + } + + public boolean isNotEmpty(String string){ + return string != null && !string.trim().isEmpty(); + } +}