reworked createCache to work with @RequestBody Cache cache
This commit is contained in:
parent
67a2defe41
commit
148254e610
@ -144,52 +144,78 @@ public class Controller {
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/createStation")
|
||||
@RequestMapping("/api/createCache")
|
||||
@ResponseBody
|
||||
public ResponseEntity createStation(@RequestParam String description,
|
||||
@RequestParam String lattitude,
|
||||
@RequestParam String longitude,
|
||||
@RequestParam String solution) {
|
||||
public ResponseEntity createCache(@RequestBody Cache cache) {
|
||||
|
||||
if (description.length() == 0 || lattitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) {
|
||||
return ResponseEntity.status(400).body("At least one Argument was empty");
|
||||
}
|
||||
|
||||
double latti;
|
||||
double longi;
|
||||
|
||||
try {
|
||||
latti = Double.valueOf(lattitude);
|
||||
if (latti < -90 || latti > 90) {
|
||||
return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree");
|
||||
for (Station station : cache.getStationen()) {
|
||||
ResponseEntity response = createStation(station);
|
||||
if (response.getStatusCodeValue() == 400) {
|
||||
return response;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.status(400).body("Latitude has to be a decimal");
|
||||
}
|
||||
|
||||
try {
|
||||
longi = Double.valueOf(longitude);
|
||||
if (longi < -180 || longi > 180) {
|
||||
return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees");
|
||||
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) {
|
||||
return ResponseEntity.status(400).body("cache fields can´t be empty");
|
||||
}
|
||||
|
||||
for (Cache cache1 : cacheRepository.findAll()) {
|
||||
if (cache1.getName().equals(cache.getName())) {
|
||||
return ResponseEntity.status(400).body("name is already taken");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.status(400).body("Longitude has to be a decimal number");
|
||||
}
|
||||
|
||||
Station station = new Station();
|
||||
station.setDescription(description);
|
||||
station.setLattitude(latti);
|
||||
station.setLongitude(longi);
|
||||
station.setSolution(solution);
|
||||
if (cache.getRankingPoints() < 0) {
|
||||
return ResponseEntity.status(400).body("Ranking points has to be a positive number");
|
||||
}
|
||||
|
||||
cacheRepository.save(cache);
|
||||
|
||||
for (int i = 0; i + 1 < cache.getStationen().size(); i++) {
|
||||
StationReihenfolge stationReihenfolge = new StationReihenfolge();
|
||||
stationReihenfolge.setCache(cache);
|
||||
stationReihenfolge.setStation(cache.getStationen().get(i));
|
||||
stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1));
|
||||
stationReihenfolgeRepository.save(stationReihenfolge);
|
||||
}
|
||||
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||
}
|
||||
|
||||
public ResponseEntity createStation(Station station) {
|
||||
|
||||
if (station.getDescription().length() == 0 || Double.valueOf(station.getLattitude()) == null || Double.valueOf(station.getLongitude()) == null || station.getSolution().length() == 0) {
|
||||
return ResponseEntity.status(400).body("station fields can´t be empty");
|
||||
}
|
||||
|
||||
if (station.getLattitude() < -90 || station.getLattitude() > 90) {
|
||||
return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree");
|
||||
}
|
||||
|
||||
if (station.getLongitude() < -180 || station.getLongitude() > 180) {
|
||||
return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees");
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
int low = 100000;
|
||||
int high = 1000000;
|
||||
int code = r.nextInt(high - low) + low;
|
||||
int code = 0;
|
||||
boolean unique = false;
|
||||
while (!unique) {
|
||||
code = r.nextInt(high - low) + low;
|
||||
unique = true;
|
||||
for (Station station1 : stationRepository.findAll()) {
|
||||
if (station1.getCode() == code) {
|
||||
unique = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
station.setCode(code);
|
||||
|
||||
stationRepository.save(station);
|
||||
|
||||
return ResponseEntity.status(201).body(new Gson().toJson(station));
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(station));
|
||||
}
|
||||
|
||||
|
||||
@ -214,60 +240,6 @@ public class Controller {
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(stationRepository.findAll()));
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/createCache")
|
||||
@ResponseBody
|
||||
public ResponseEntity createCache(@RequestParam String description,
|
||||
@RequestParam String name,
|
||||
@RequestParam String rankingPoints,
|
||||
@RequestParam(value = "rewardID", defaultValue = "-1") String rewardID,
|
||||
@RequestParam List<Station> stationen) {
|
||||
|
||||
if (description.length() == 0 || name.length() == 0 || rankingPoints.length() == 0 || stationen.size() == 0) {
|
||||
return ResponseEntity.status(400).body("Fields can´t be empty");
|
||||
}
|
||||
|
||||
for (Cache cache : cacheRepository.findAll()) {
|
||||
if (cache.getName().equals(name)) {
|
||||
return ResponseEntity.status(400).body("name is already taken");
|
||||
}
|
||||
}
|
||||
|
||||
int points;
|
||||
|
||||
try {
|
||||
points = Integer.valueOf(rankingPoints);
|
||||
if (points < 0) {
|
||||
return ResponseEntity.status(400).body("Ranking points has to be a positive number");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
return ResponseEntity.status(400).body("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));
|
||||
stationReihenfolgeRepository.save(stationReihenfolge);
|
||||
}
|
||||
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/deleteCache")
|
||||
@ResponseBody
|
||||
|
||||
Loading…
Reference in New Issue
Block a user