From 0f856b1ec2bed6fc55a0490806a615a746bda87f Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 13 Apr 2019 19:44:47 +0200 Subject: [PATCH] =?UTF-8?q?first=20implementation=20for=20checkStation(API?= =?UTF-8?q?=20method=20for=20cache=20durchf=C3=BChren)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bugageocaching/controller/Controller.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 0bd9fc0..5951034 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -164,6 +164,81 @@ public class Controller { } } + @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(); + + int i = 0; + for (Station station1 : cache.getStationen()) { + if (station1.equals(station)) { + break; + } + i++; + } + + if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { + return ResponseEntity.status(200).body("OK"); + } 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