From 2123a9177ed6076f8f0c0148ad20dc0c836ffe5e Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 28 Mar 2019 20:47:34 +0100 Subject: [PATCH] implemented api for createStation and fixed some bugs in other methods --- .../bugageocaching/controller/Controller.java | 57 ++++++++++++++++++- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 348ad8b..1418cc3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -10,8 +10,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; -import java.time.Clock; import java.util.Optional; +import java.util.Random; import java.util.concurrent.atomic.AtomicLong; @RestController @@ -120,13 +120,64 @@ public class Controller { } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("api/logout") + @RequestMapping("/api/logout") @ResponseBody boolean logout(@RequestParam String token) { User user = userRepository.findByUsername(token.substring(0, token.indexOf("$"))); - user.setToken("abgemeldet"); + user.setToken("offline"); userRepository.save(user); return true; } + + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/createStation") + @ResponseBody + Station 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; + + } + } + }