diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 29ed66c..5e31a36 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -39,6 +39,9 @@ public class Controller { @Autowired UserRepository userRepository; + @Autowired + StationReihenfolgeRepository stationReihenfolgeRepository; + private AtomicLong counter = new AtomicLong(); @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -249,9 +252,39 @@ public class Controller { stationReihenfolge.setCache(cache); stationReihenfolge.setStation(stationen.get(i)); stationReihenfolge.setNachfolgeStation(stationen.get(i + 1)); + stationReihenfolgeRepository.save(stationReihenfolge); } return new Gson().toJson(cache); } + + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/deleteCache") + @ResponseBody + boolean deleteCache(@RequestParam String cacheID) throws IllegalParameterException { + Optional optionalCache = cacheRepository.findById(Integer.valueOf(cacheID)); + if (!optionalCache.isPresent()) { + throw new IllegalParameterException("There is no cache with the ID " + cacheID); + } + + Cache cache = optionalCache.get(); + + for (StationReihenfolge stationReihenfolge : stationReihenfolgeRepository.findAll()) { + if (stationReihenfolge.getCache().getId() == cache.getId()) { + stationReihenfolgeRepository.delete(stationReihenfolge); + } + } + + for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { + if (bearbeitet.getCache().getId() == cache.getId()) { + bearbeitetRepository.delete(bearbeitet); + } + } + + cacheRepository.delete(cache); + + return true; + } + }