diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 0e94893..1c17bd0 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -151,21 +151,25 @@ public class Controller { for (Station station : cache.getStationen()) { ResponseEntity response = createStation(station); if (response.getStatusCodeValue() == 400) { + deleteStationen(cache); return response; } } if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) { + deleteStationen(cache); return ResponseEntity.status(400).body("cache fields can“t be empty"); } for (Cache cache1 : cacheRepository.findAll()) { if (cache1.getName().equals(cache.getName())) { + deleteStationen(cache); return ResponseEntity.status(400).body("name is already taken"); } } if (cache.getRankingPoints() < 0) { + deleteStationen(cache); return ResponseEntity.status(400).body("Ranking points has to be a positive number"); } @@ -218,6 +222,16 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(station)); } + public void deleteStationen(Cache cache) { + for (Station station : cache.getStationen()) { + try { + stationRepository.delete(station); + } catch (IllegalArgumentException e) { // station is null + // do nothing + } + } + } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin")