Merge branch 'develop' into frontend/timo
This commit is contained in:
commit
47aeec7a7c
@ -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,35 +59,37 @@ 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())) {
|
||||
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()));
|
||||
//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));
|
||||
@ -98,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 =
|
||||
@ -112,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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -128,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;
|
||||
@ -158,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();
|
||||
@ -186,46 +189,47 @@ 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
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@ -234,10 +238,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));
|
||||
@ -261,16 +265,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();
|
||||
@ -289,29 +293,53 @@ 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
|
||||
@RequestMapping("/api/getMyCaches")
|
||||
@ResponseBody
|
||||
String getMyCaches(@RequestParam String token) {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
ArrayList<Bearbeitet> bearbeitetList = new ArrayList<>();
|
||||
ResponseEntity getMyCaches(@RequestParam String token) {
|
||||
try {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
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");
|
||||
}
|
||||
} catch (StringIndexOutOfBoundsException e) {
|
||||
return ResponseEntity.status(400).body("Invalid token");
|
||||
}
|
||||
return new Gson().toJson(bearbeitetList);
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/getRankingList")
|
||||
@ResponseBody
|
||||
String getRankingList() {
|
||||
return new Gson().toJson(userRepository.getRankingList());
|
||||
ResponseEntity getRankingList() {
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList()));
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/getUser")
|
||||
@ResponseBody
|
||||
ResponseEntity getUser(@RequestParam String token) {
|
||||
try {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user