implemented api for createStation and fixed some bugs in other methods

This commit is contained in:
Michael 2019-03-28 20:47:34 +01:00
parent 0f6ca8a9d9
commit 2123a9177e

View File

@ -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;
}
}
}