reworked methods to return a http response

This commit is contained in:
Michael 2019-04-04 15:03:01 +02:00
parent cd3508090b
commit 072a1f286d

View File

@ -67,7 +67,6 @@ public class Controller {
if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) {
String token = user.getUsername() + BCrypt.hashpw(String.valueOf(System.currentTimeMillis() + counter.incrementAndGet()), BCrypt.gensalt());
System.out.println(token);
String hashedToken = BCrypt.hashpw(token, BCrypt.gensalt());
userRepository.findByUsername(user.getUsername()).setToken(hashedToken);
userRepository.save(userRepository.findByUsername(user.getUsername()));
@ -201,26 +200,26 @@ public class Controller {
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
@RequestMapping("/api/getAllStations")
@ResponseBody
String getAllStations() {
return new Gson().toJson(stationRepository.findAll());
ResponseEntity getAllStations() {
return ResponseEntity.status(200).body(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 {
ResponseEntity 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");
return ResponseEntity.status(400).body("Fields can´t be empty");
}
for (Cache cache : cacheRepository.findAll()) {
if (cache.getName().equals(name)) {
throw new IllegalParameterException("name is already taken");
return ResponseEntity.status(400).body("name is already taken");
}
}
@ -229,10 +228,10 @@ public class Controller {
try {
points = Integer.valueOf(rankingPoints);
if (points < 0) {
throw new IllegalParameterException("Ranking points has to be a positive number");
return ResponseEntity.status(400).body("Ranking points has to be a positive number");
}
} catch (NumberFormatException e) {
throw new NumberFormatException("Ranking points has to be an integer");
return ResponseEntity.status(400).body("Ranking points has to be an integer");
}
Optional<Reward> rewardOptional = rewardRepository.findById(Integer.valueOf(rewardID));
@ -256,16 +255,16 @@ public class Controller {
stationReihenfolgeRepository.save(stationReihenfolge);
}
return new Gson().toJson(cache);
return ResponseEntity.status(200).body(new Gson().toJson(cache));
}
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
@RequestMapping("/api/deleteCache")
@ResponseBody
boolean deleteCache(@RequestParam String cacheID) throws IllegalParameterException {
ResponseEntity deleteCache(@RequestParam String cacheID) {
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
if (!optionalCache.isPresent()) {
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID));
}
Cache cache = optionalCache.get();
@ -284,7 +283,7 @@ public class Controller {
cacheRepository.delete(cache);
return true;
return ResponseEntity.status(200).body(new Gson().toJson(true));
}
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
@ -293,16 +292,20 @@ public class Controller {
ResponseEntity getMyCaches(@RequestParam String token) {
try {
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
ArrayList<Bearbeitet> bearbeitetList = new ArrayList<>();
if (user != null) {
ArrayList<Bearbeitet> bearbeitetList = new ArrayList<>();
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
if (bearbeitet.getUser().getId() == user.getId()) {
bearbeitetList.add(bearbeitet);
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
if (bearbeitet.getUser().getId() == user.getId()) {
bearbeitetList.add(bearbeitet);
}
}
return ResponseEntity.status(200).body(new Gson().toJson(bearbeitetList));
} else {
return ResponseEntity.status(404).body("User was not found in the database");
}
return ResponseEntity.status(200).body(new Gson().toJson(bearbeitetList));
} catch (Exception e) {
return ResponseEntity.status(404).body("User was not found in the database");
} catch (StringIndexOutOfBoundsException e) {
return ResponseEntity.status(400).body("Invalid token");
}
}
@ -319,9 +322,13 @@ public class Controller {
ResponseEntity getUser(@RequestParam String token) {
try {
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
return ResponseEntity.status(200).body(new Gson().toJson(user));
} catch (Exception e) {
return ResponseEntity.status(404).body("User was not found in the database");
if (user != null) {
return ResponseEntity.status(200).body(new Gson().toJson(user));
} else {
return ResponseEntity.status(404).body("User was not found in the database");
}
} catch (StringIndexOutOfBoundsException e) {
return ResponseEntity.status(400).body("Invalid token");
}
}
}