Added error checking for post calls
This commit is contained in:
@@ -48,6 +48,9 @@ public class LocationController {
|
|||||||
@PostMapping("")
|
@PostMapping("")
|
||||||
public ResponseEntity<LocationEntity> createLocation(@RequestBody LocationEntity location) {
|
public ResponseEntity<LocationEntity> createLocation(@RequestBody LocationEntity location) {
|
||||||
LocationEntity createdLocation = locationService.createLocation(location);
|
LocationEntity createdLocation = locationService.createLocation(location);
|
||||||
|
if(createdLocation == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
return new ResponseEntity<>(createdLocation, HttpStatus.CREATED);
|
return new ResponseEntity<>(createdLocation, HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ public class UserController {
|
|||||||
@PostMapping("")
|
@PostMapping("")
|
||||||
public ResponseEntity<UserEntity> createUser(@RequestBody UserEntity user) {
|
public ResponseEntity<UserEntity> createUser(@RequestBody UserEntity user) {
|
||||||
UserEntity createdUser = userService.createUser(user);
|
UserEntity createdUser = userService.createUser(user);
|
||||||
|
if(createdUser == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
|
return new ResponseEntity<>(createdUser, HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,9 @@ public class WaypointController {
|
|||||||
@PostMapping("")
|
@PostMapping("")
|
||||||
public ResponseEntity<WaypointsEntity> createWaypoint(@RequestBody WaypointsEntity waypoint) {
|
public ResponseEntity<WaypointsEntity> createWaypoint(@RequestBody WaypointsEntity waypoint) {
|
||||||
WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint);
|
WaypointsEntity createdWaypoint = waypointService.createWaypoint(waypoint);
|
||||||
|
if (createdWaypoint == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
|
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,9 @@ public class WaypointVisitedController {
|
|||||||
@PostMapping("")
|
@PostMapping("")
|
||||||
public ResponseEntity<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) {
|
public ResponseEntity<WaypointsVisitedEntity> createWaypointVisited(@RequestBody WaypointsVisitedEntity waypointVisited) {
|
||||||
WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited);
|
WaypointsVisitedEntity createdWaypoint = waypointVisitedService.createWaypoint(waypointVisited);
|
||||||
|
if(createdWaypoint == null) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
|
return new ResponseEntity<>(createdWaypoint, HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ch.progetto152.services;
|
|||||||
|
|
||||||
import ch.progetto152.entity.LocationEntity;
|
import ch.progetto152.entity.LocationEntity;
|
||||||
import ch.progetto152.repository.LocationRepository;
|
import ch.progetto152.repository.LocationRepository;
|
||||||
|
import ch.progetto152.utility.ErrorChecking;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -10,6 +11,7 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class LocationService {
|
public class LocationService {
|
||||||
|
|
||||||
|
private ErrorChecking errorChecking = new ErrorChecking();
|
||||||
private final LocationRepository locationRepository;
|
private final LocationRepository locationRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -30,7 +32,11 @@ public class LocationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public LocationEntity createLocation(LocationEntity Location) {
|
public LocationEntity createLocation(LocationEntity Location) {
|
||||||
|
if(errorChecking.checkLocation(Location)) {
|
||||||
return locationRepository.save(Location);
|
return locationRepository.save(Location);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public LocationEntity updateLocation(Long id, LocationEntity Location) {
|
public LocationEntity updateLocation(Long id, LocationEntity Location) {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package ch.progetto152.services;
|
|||||||
|
|
||||||
import ch.progetto152.entity.UserEntity;
|
import ch.progetto152.entity.UserEntity;
|
||||||
import ch.progetto152.repository.UserRepository;
|
import ch.progetto152.repository.UserRepository;
|
||||||
|
import ch.progetto152.utility.ErrorChecking;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -11,6 +12,7 @@ import java.util.List;
|
|||||||
@Service
|
@Service
|
||||||
public class UserService {
|
public class UserService {
|
||||||
|
|
||||||
|
ErrorChecking errorChecking = new ErrorChecking();
|
||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -35,7 +37,11 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity createUser(UserEntity user) {
|
public UserEntity createUser(UserEntity user) {
|
||||||
|
if(errorChecking.checkUser(user)) {
|
||||||
return userRepository.save(user);
|
return userRepository.save(user);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public UserEntity updateUser(Long id, UserEntity user) {
|
public UserEntity updateUser(Long id, UserEntity user) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ch.progetto152.services;
|
|||||||
|
|
||||||
import ch.progetto152.entity.WaypointsEntity;
|
import ch.progetto152.entity.WaypointsEntity;
|
||||||
import ch.progetto152.repository.WaypointRepository;
|
import ch.progetto152.repository.WaypointRepository;
|
||||||
|
import ch.progetto152.utility.ErrorChecking;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ import java.util.List;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class WaypointService {
|
public class WaypointService {
|
||||||
|
|
||||||
|
ErrorChecking errorChecking = new ErrorChecking();
|
||||||
private final WaypointRepository waypointRepository;
|
private final WaypointRepository waypointRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -29,8 +32,11 @@ public class WaypointService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public WaypointsEntity createWaypoint(WaypointsEntity waypoint) {
|
public WaypointsEntity createWaypoint(WaypointsEntity waypoint) {
|
||||||
|
if (errorChecking.checkWaypoint(waypoint)) {
|
||||||
return waypointRepository.save(waypoint);
|
return waypointRepository.save(waypoint);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) {
|
public WaypointsEntity updateWaypoint(Long id, WaypointsEntity waypoint) {
|
||||||
WaypointsEntity waypoint1 = getWaypointById(id);
|
WaypointsEntity waypoint1 = getWaypointById(id);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package ch.progetto152.services;
|
|||||||
|
|
||||||
import ch.progetto152.entity.WaypointsVisitedEntity;
|
import ch.progetto152.entity.WaypointsVisitedEntity;
|
||||||
import ch.progetto152.repository.WaypointVisitedRepository;
|
import ch.progetto152.repository.WaypointVisitedRepository;
|
||||||
|
import ch.progetto152.utility.ErrorChecking;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -9,6 +10,8 @@ import java.util.List;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class WaypointVisitedService {
|
public class WaypointVisitedService {
|
||||||
|
|
||||||
|
ErrorChecking errorChecking = new ErrorChecking();
|
||||||
private final WaypointVisitedRepository waypointVisitedRepository;
|
private final WaypointVisitedRepository waypointVisitedRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -33,8 +36,11 @@ public class WaypointVisitedService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public WaypointsVisitedEntity createWaypoint(WaypointsVisitedEntity waypoint) {
|
public WaypointsVisitedEntity createWaypoint(WaypointsVisitedEntity waypoint) {
|
||||||
|
if (errorChecking.checkWaypointVisited(waypoint)) {
|
||||||
return waypointVisitedRepository.save(waypoint);
|
return waypointVisitedRepository.save(waypoint);
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public WaypointsVisitedEntity updateWaypoint(Long id, WaypointsVisitedEntity waypoint) {
|
public WaypointsVisitedEntity updateWaypoint(Long id, WaypointsVisitedEntity waypoint) {
|
||||||
WaypointsVisitedEntity waypoint1 = getWaypointById(id);
|
WaypointsVisitedEntity waypoint1 = getWaypointById(id);
|
||||||
|
|||||||
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