diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 7c19fbf..a4935d9 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -97,7 +97,7 @@ public class Controller { } Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(0); // bearbeitet + cacheAccesDefinitionRepository.findById(0); // bearbeitet if (cacheAccesDefinitionOptional.isPresent()) { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); @@ -126,7 +126,7 @@ public class Controller { boolean logout(@RequestParam String token) { User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - user.setToken("offline"); + user.setToken(null); userRepository.save(user); return true; } @@ -134,51 +134,51 @@ public class Controller { @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/createStation") @ResponseBody - Station createStation(@RequestParam String description, + String createStation(@RequestParam String description, @RequestParam String lattitude, @RequestParam String longitude, @RequestParam String solution) throws IllegalParameterException { if (description.length() == 0 || lattitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) { throw new IllegalParameterException("Fields can´t be empty"); - } else { - - Station station = new Station(); - station.setDescription(description); - station.setSolution(solution); - Random r = new Random(); - int low = 100000; - int high = 1000000; - int code = r.nextInt(high - low) + low; - station.setCode(code); - - try { - double latti = Double.valueOf(lattitude); - if (-90 <= latti && latti <= 90) { - station.setLattitude(latti); - } else { - throw new IllegalParameterException("Lattitude has to be in the range of -90 to 90 degrees"); - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Lattitude hast to be a decimal number"); - } - - try { - double longi = Double.valueOf(longitude); - if (-180 <= longi && longi <= 180) { - station.setLongitude(longi); - } else { - throw new IllegalParameterException("Longitude has to be in the range of -180 to 180 degrees"); - } - } catch (NumberFormatException e) { - throw new NumberFormatException("Longitude hast to be a decimal number"); - } - - stationRepository.save(station); - - return station; - } + + double latti; + double longi; + + try { + latti = Double.valueOf(lattitude); + if (latti < -90 || latti > 90) { + throw new IllegalParameterException("Lattitude has to be in the range of -90 to 90 degrees"); + } + } catch (NumberFormatException e) { + throw new NumberFormatException("Lattitude has to be a decimal number"); + } + + try { + longi = Double.valueOf(longitude); + if (longi < -180 || longi > 180) { + throw new IllegalParameterException("Longitude has to be in the range of -180 to 180 degrees"); + } + } catch (NumberFormatException e) { + throw new NumberFormatException("Longitude has to be a decimal number"); + } + + Station station = new Station(); + station.setDescription(description); + station.setLattitude(latti); + station.setLongitude(longi); + station.setSolution(solution); + Random r = new Random(); + int low = 100000; + int high = 1000000; + int code = r.nextInt(high - low) + low; + station.setCode(code); + + stationRepository.save(station); + + return new Gson().toJson(station); + } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -186,8 +186,7 @@ public class Controller { @ResponseBody boolean checkAdmin(@RequestParam String token) { User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - List roles = user.getRoles(); - for (Role role : roles) { + for (Role role : user.getRoles()) { if (role.getId() == 0) { // is admin return true; } @@ -202,4 +201,57 @@ public class Controller { return new Gson().toJson(stationRepository.findAll()); } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/createCache") + @ResponseBody + String createCache(@RequestParam String description, + @RequestParam String name, + @RequestParam String rankingPoints, + @RequestParam(value = "rewardID", defaultValue = "-1") String rewardID, + @RequestParam List stationen) throws IllegalParameterException { + + if (description.length() == 0 || name.length() == 0 || rankingPoints.length() == 0 || stationen.size() == 0) { + throw new IllegalParameterException("Fields can´t be empty"); + } + + for (Cache cache : cacheRepository.findAll()) { + if (cache.getName().equals(name)) { + throw new IllegalParameterException("name is already taken"); + } + } + + int points; + + try { + points = Integer.valueOf(rankingPoints); + if (points < 0) { + throw new IllegalParameterException("Ranking points has to be a positive number"); + } + } catch (NumberFormatException e) { + throw new NumberFormatException("Ranking points has to be an integer"); + } + + Optional rewardOptional = rewardRepository.findById(Integer.valueOf(rewardID)); + Reward reward = rewardOptional.orElse(null); + + Cache cache = new Cache(); + cache.setDescription(description); + cache.setName(name); + cache.setRankingPoints(Integer.valueOf(rankingPoints)); + cache.setReward(reward); + cache.setStartStation(stationen.get(0)); + cache.setStationen(stationen); + + cacheRepository.save(cache); + + for(int i = 0; i + 1 < stationen.size(); i++) { + StationReihenfolge stationReihenfolge = new StationReihenfolge(); + stationReihenfolge.setCache(cache); + stationReihenfolge.setStation(stationen.get(i)); + stationReihenfolge.setNachfolgeStation(stationen.get(i+1)); + } + + return new Gson().toJson(cache); + } } +