Added error checking for post calls
This commit is contained in:
@@ -48,6 +48,9 @@ public class LocationController {
|
||||
@PostMapping("")
|
||||
public ResponseEntity<LocationEntity> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,9 @@ public class UserController {
|
||||
@PostMapping("")
|
||||
public ResponseEntity<UserEntity> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -48,6 +48,9 @@ public class WaypointController {
|
||||
@PostMapping("")
|
||||
public ResponseEntity<WaypointsEntity> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -58,6 +58,9 @@ public class WaypointVisitedController {
|
||||
@PostMapping("")
|
||||
public ResponseEntity<WaypointsVisitedEntity> 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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
44
src/main/java/ch/progetto152/utility/ErrorChecking.java
Normal file
44
src/main/java/ch/progetto152/utility/ErrorChecking.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user