added createCache API and reworked some other methods
This commit is contained in:
parent
73681073dd
commit
38097dad8e
@ -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,17 +134,40 @@ 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 {
|
||||
}
|
||||
|
||||
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;
|
||||
@ -152,42 +175,18 @@ public class Controller {
|
||||
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;
|
||||
return new Gson().toJson(station);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/checkAdmin")
|
||||
@ResponseBody
|
||||
boolean checkAdmin(@RequestParam String token) {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
List<Role> 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<Station> 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<Reward> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user