diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 25c4db2..21ee6b3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -163,6 +163,100 @@ public class Controller { } } + /** + * Checkt, ob die eingescannte Station die Nachfolgestation der zuletzt + * besuchten Stationen des aktuell durchgeführten Caches ist + */ + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/checkStation") + @ResponseBody + public ResponseEntity checkStation(@RequestParam String token, + @RequestParam String cacheID, + @RequestParam String stationID, + @RequestParam String durchgefuehrterCacheID) { + try { + Claims claims = Jwts.parser() //Parse JWT + .setSigningKey(key) + .parseClaimsJws(token).getBody(); + + User user = userRepository.findByUsername(claims.getSubject()); + if (user == null) { + return ResponseEntity.status(404).body("User was not found"); + } + + Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); + Cache cache; + if (cacheOptional.isPresent()) { + cache = cacheOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); + } + + Optional durchgefuehrterCacheIDOptional = cacheRepository.findById(Integer.valueOf(durchgefuehrterCacheID)); + Cache durchgefuehrterCache; + if (durchgefuehrterCacheIDOptional.isPresent()) { + durchgefuehrterCache = cacheOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Cache " + durchgefuehrterCacheID); + } + + Optional stationOptional = stationRepository.findById(Integer.valueOf(stationID)); + Station station; + if (stationOptional.isPresent()) { + station = stationOptional.get(); + } else { + return ResponseEntity.status(404).body("Couldnt find Station " + stationID); + } + + if (cache != durchgefuehrterCache) { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + + Bearbeitet bearbeitet; + if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { + bearbeitet = bearbeitetRepository.findByUserAndCache(user, cache); + } else { + return ResponseEntity.status(400).body("The user has not started this cache yet"); + } + + Station aktuelleStation = bearbeitet.getAktuelleStation(); + + if (!cache.getStationen().contains(station)) { + return ResponseEntity.status(400).body("The scanned station isnt a part of the cache"); + } + + int i = 0; + for (Station station1 : cache.getStationen()) { + if (station1.equals(station)) { + break; + } + i++; + } + + if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { + bearbeitet.setAktuelleStation(station); + if (i == cache.getStationen().size() - 1) { // letze Station erreicht + Optional cacheAccesDefinitionOptional = + cacheAccesDefinitionRepository.findById(1); // abgeschlossen + if (cacheAccesDefinitionOptional.isPresent()) { + CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + } else { + return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 1); + } + } + return ResponseEntity.status(200).body(new Gson().toJson(station)); + } else { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + } + + } catch (ExpiredJwtException e) { + return ResponseEntity.status(400).body("JWT Token expired"); + } catch (Exception e) { + return ResponseEntity.status(400).body("JWT Token invalid"); + } + } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/createCache") @ResponseBody