Added Repsoneentities for Api Request
This commit is contained in:
parent
cc8c337833
commit
ebd05f2be9
@ -48,8 +48,8 @@ public class Controller {
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/allCaches")
|
||||
@ResponseBody
|
||||
public String getAllCaches() {
|
||||
return new Gson().toJson(cacheRepository.findAll());
|
||||
public ResponseEntity getAllCaches() {
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll()));
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@ -59,10 +59,10 @@ public class Controller {
|
||||
if (user.getUsername() == null || user.getPassword() == null) {
|
||||
System.out.println(user.getUsername());
|
||||
System.out.println(user.getPassword());
|
||||
return ResponseEntity.status(401).body(null);
|
||||
return ResponseEntity.status(400).body("Username or password cant be null");
|
||||
}
|
||||
if (userRepository.findByUsername(user.getUsername()) == null) {
|
||||
return ResponseEntity.status(401).body(null);
|
||||
return ResponseEntity.status(404).body("User was not found");
|
||||
}
|
||||
|
||||
if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) {
|
||||
@ -71,22 +71,25 @@ public class Controller {
|
||||
userRepository.findByUsername(user.getUsername()).setToken(hashedToken);
|
||||
userRepository.save(userRepository.findByUsername(user.getUsername()));
|
||||
//return ResponseEntity.ok(new Gson().toJson(token));
|
||||
return ResponseEntity.ok(token);
|
||||
return ResponseEntity.status(200).body(token);
|
||||
}
|
||||
return ResponseEntity.status(HttpStatus.BAD_GATEWAY).body(null);
|
||||
return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten");
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/startCache")
|
||||
@ResponseBody
|
||||
public String startCache(@RequestParam(value = "token", defaultValue = "-1") String token,
|
||||
@RequestParam String cacheID) throws IllegalParameterException {
|
||||
public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token,
|
||||
@RequestParam String cacheID) throws IllegalParameterException {
|
||||
|
||||
if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben)
|
||||
|
||||
Bearbeitet bearbeitet = new Bearbeitet();
|
||||
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
if (user == null) {
|
||||
return ResponseEntity.status(404).body("User was not found");
|
||||
}
|
||||
bearbeitet.setUser(user);
|
||||
|
||||
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||
@ -97,7 +100,7 @@ public class Controller {
|
||||
Station startStation = cache.getStartStation();
|
||||
bearbeitet.setAktuelleStation(startStation);
|
||||
} else {
|
||||
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
|
||||
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
|
||||
}
|
||||
|
||||
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
|
||||
@ -111,15 +114,15 @@ public class Controller {
|
||||
|
||||
bearbeitetRepository.save(bearbeitet);
|
||||
|
||||
return new Gson().toJson(bearbeitet);
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet));
|
||||
|
||||
} else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben)
|
||||
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||
if (cacheOptional.isPresent()) {
|
||||
Cache cache = cacheOptional.get();
|
||||
return new Gson().toJson(cache);
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||
} else {
|
||||
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
|
||||
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -127,28 +130,29 @@ public class Controller {
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/logout")
|
||||
@ResponseBody
|
||||
boolean logout(@RequestParam String token) {
|
||||
ResponseEntity logout(@RequestParam String token) {
|
||||
// System.out.println("logout");
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
// System.out.println(token);
|
||||
// System.out.println(user.getToken());
|
||||
if (user == null || user.getToken().isEmpty() )
|
||||
return false;
|
||||
if (user == null || user.getToken().isEmpty()) {
|
||||
return ResponseEntity.status(404).body("User was not found");
|
||||
}
|
||||
user.setToken(null);
|
||||
userRepository.save(user);
|
||||
return true;
|
||||
return ResponseEntity.status(200).body("Token was deleted");
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/createStation")
|
||||
@ResponseBody
|
||||
String createStation(@RequestParam String description,
|
||||
@RequestParam String lattitude,
|
||||
@RequestParam String longitude,
|
||||
@RequestParam String solution) throws IllegalParameterException {
|
||||
ResponseEntity 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");
|
||||
return ResponseEntity.status(400).body("At least one Argument was empty");
|
||||
}
|
||||
|
||||
double latti;
|
||||
@ -157,19 +161,19 @@ public class Controller {
|
||||
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");
|
||||
return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree");
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
throw new NumberFormatException("Lattitude has to be a decimal number");
|
||||
return ResponseEntity.status(400).body("Latitude has to be a decimal");
|
||||
}
|
||||
|
||||
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");
|
||||
return ResponseEntity.status(400).body("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");
|
||||
return ResponseEntity.status(400).body("Longitude has to be a decimal number");
|
||||
}
|
||||
|
||||
Station station = new Station();
|
||||
@ -185,23 +189,24 @@ public class Controller {
|
||||
|
||||
stationRepository.save(station);
|
||||
|
||||
return new Gson().toJson(station);
|
||||
|
||||
return ResponseEntity.status(201).body(new Gson().toJson(station));
|
||||
}
|
||||
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/checkAdmin")
|
||||
@ResponseBody
|
||||
boolean checkAdmin(@RequestParam String token) {
|
||||
ResponseEntity checkAdmin(@RequestParam String token) {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
for (Role role : user.getRoles()) {
|
||||
if (role.getId() == 0) { // is admin
|
||||
return true;
|
||||
return ResponseEntity.status(200).body("User is Admin");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return ResponseEntity.status(401).body("User is no Admin");
|
||||
}
|
||||
|
||||
//Bis hier
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/getAllStations")
|
||||
@ResponseBody
|
||||
|
||||
Loading…
Reference in New Issue
Block a user