if a mistake occurs during cache creation, all previously created stations related to that cache get deleted now

This commit is contained in:
Michael 2019-04-05 15:33:31 +02:00
parent c0448bda84
commit 6cf52503bc

View File

@ -151,21 +151,25 @@ public class Controller {
for (Station station : cache.getStationen()) { for (Station station : cache.getStationen()) {
ResponseEntity response = createStation(station); ResponseEntity response = createStation(station);
if (response.getStatusCodeValue() == 400) { if (response.getStatusCodeValue() == 400) {
deleteStationen(cache);
return response; return response;
} }
} }
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) { 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"); return ResponseEntity.status(400).body("cache fields can´t be empty");
} }
for (Cache cache1 : cacheRepository.findAll()) { for (Cache cache1 : cacheRepository.findAll()) {
if (cache1.getName().equals(cache.getName())) { if (cache1.getName().equals(cache.getName())) {
deleteStationen(cache);
return ResponseEntity.status(400).body("name is already taken"); return ResponseEntity.status(400).body("name is already taken");
} }
} }
if (cache.getRankingPoints() < 0) { if (cache.getRankingPoints() < 0) {
deleteStationen(cache);
return ResponseEntity.status(400).body("Ranking points has to be a positive number"); 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)); 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 @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
@RequestMapping("/api/checkAdmin") @RequestMapping("/api/checkAdmin")