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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/allCaches")
|
@RequestMapping("/api/allCaches")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String getAllCaches() {
|
public ResponseEntity getAllCaches() {
|
||||||
return new Gson().toJson(cacheRepository.findAll());
|
return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@ -59,35 +59,37 @@ public class Controller {
|
|||||||
if (user.getUsername() == null || user.getPassword() == null) {
|
if (user.getUsername() == null || user.getPassword() == null) {
|
||||||
System.out.println(user.getUsername());
|
System.out.println(user.getUsername());
|
||||||
System.out.println(user.getPassword());
|
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) {
|
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())) {
|
if (BCrypt.checkpw(user.getPassword(), userRepository.findByUsername(user.getUsername()).getPassword())) {
|
||||||
String token = user.getUsername() + BCrypt.hashpw(String.valueOf(System.currentTimeMillis() + counter.incrementAndGet()), BCrypt.gensalt());
|
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());
|
String hashedToken = BCrypt.hashpw(token, BCrypt.gensalt());
|
||||||
userRepository.findByUsername(user.getUsername()).setToken(hashedToken);
|
userRepository.findByUsername(user.getUsername()).setToken(hashedToken);
|
||||||
userRepository.save(userRepository.findByUsername(user.getUsername()));
|
userRepository.save(userRepository.findByUsername(user.getUsername()));
|
||||||
//return ResponseEntity.ok(new Gson().toJson(token));
|
//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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/startCache")
|
@RequestMapping("/api/startCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public String startCache(@RequestParam(value = "token", defaultValue = "-1") String token,
|
public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token,
|
||||||
@RequestParam String cacheID) throws IllegalParameterException {
|
@RequestParam String cacheID) throws IllegalParameterException {
|
||||||
|
|
||||||
if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben)
|
if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben)
|
||||||
|
|
||||||
Bearbeitet bearbeitet = new Bearbeitet();
|
Bearbeitet bearbeitet = new Bearbeitet();
|
||||||
|
|
||||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||||
|
if (user == null) {
|
||||||
|
return ResponseEntity.status(404).body("User was not found");
|
||||||
|
}
|
||||||
bearbeitet.setUser(user);
|
bearbeitet.setUser(user);
|
||||||
|
|
||||||
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||||
@ -98,7 +100,7 @@ public class Controller {
|
|||||||
Station startStation = cache.getStartStation();
|
Station startStation = cache.getStartStation();
|
||||||
bearbeitet.setAktuelleStation(startStation);
|
bearbeitet.setAktuelleStation(startStation);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalParameterException("There is no cache with the ID " + cacheID);
|
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
|
Optional<CacheAccesDefinition> cacheAccesDefinitionOptional =
|
||||||
@ -112,15 +114,15 @@ public class Controller {
|
|||||||
|
|
||||||
bearbeitetRepository.save(bearbeitet);
|
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)
|
} else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben)
|
||||||
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
Optional<Cache> cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||||
if (cacheOptional.isPresent()) {
|
if (cacheOptional.isPresent()) {
|
||||||
Cache cache = cacheOptional.get();
|
Cache cache = cacheOptional.get();
|
||||||
return new Gson().toJson(cache);
|
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||||
} else {
|
} 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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/logout")
|
@RequestMapping("/api/logout")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
boolean logout(@RequestParam String token) {
|
ResponseEntity logout(@RequestParam String token) {
|
||||||
// System.out.println("logout");
|
// System.out.println("logout");
|
||||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||||
// System.out.println(token);
|
// System.out.println(token);
|
||||||
// System.out.println(user.getToken());
|
// System.out.println(user.getToken());
|
||||||
if (user == null || user.getToken().isEmpty() )
|
if (user == null || user.getToken().isEmpty()) {
|
||||||
return false;
|
return ResponseEntity.status(404).body("User was not found");
|
||||||
|
}
|
||||||
user.setToken(null);
|
user.setToken(null);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
return true;
|
return ResponseEntity.status(200).body("Token was deleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/createStation")
|
@RequestMapping("/api/createStation")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
String createStation(@RequestParam String description,
|
ResponseEntity createStation(@RequestParam String description,
|
||||||
@RequestParam String lattitude,
|
@RequestParam String lattitude,
|
||||||
@RequestParam String longitude,
|
@RequestParam String longitude,
|
||||||
@RequestParam String solution) throws IllegalParameterException {
|
@RequestParam String solution) throws IllegalParameterException {
|
||||||
|
|
||||||
if (description.length() == 0 || lattitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) {
|
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;
|
double latti;
|
||||||
@ -158,19 +161,19 @@ public class Controller {
|
|||||||
try {
|
try {
|
||||||
latti = Double.valueOf(lattitude);
|
latti = Double.valueOf(lattitude);
|
||||||
if (latti < -90 || latti > 90) {
|
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) {
|
} 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 {
|
try {
|
||||||
longi = Double.valueOf(longitude);
|
longi = Double.valueOf(longitude);
|
||||||
if (longi < -180 || longi > 180) {
|
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) {
|
} 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();
|
Station station = new Station();
|
||||||
@ -186,46 +189,47 @@ public class Controller {
|
|||||||
|
|
||||||
stationRepository.save(station);
|
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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/checkAdmin")
|
@RequestMapping("/api/checkAdmin")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
boolean checkAdmin(@RequestParam String token) {
|
ResponseEntity checkAdmin(@RequestParam String token) {
|
||||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||||
for (Role role : user.getRoles()) {
|
for (Role role : user.getRoles()) {
|
||||||
if (role.getId() == 0) { // is admin
|
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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/getAllStations")
|
@RequestMapping("/api/getAllStations")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
String getAllStations() {
|
ResponseEntity getAllStations() {
|
||||||
return new Gson().toJson(stationRepository.findAll());
|
return ResponseEntity.status(200).body(new Gson().toJson(stationRepository.findAll()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/createCache")
|
@RequestMapping("/api/createCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
String createCache(@RequestParam String description,
|
ResponseEntity createCache(@RequestParam String description,
|
||||||
@RequestParam String name,
|
@RequestParam String name,
|
||||||
@RequestParam String rankingPoints,
|
@RequestParam String rankingPoints,
|
||||||
@RequestParam(value = "rewardID", defaultValue = "-1") String rewardID,
|
@RequestParam(value = "rewardID", defaultValue = "-1") String rewardID,
|
||||||
@RequestParam List<Station> stationen) throws IllegalParameterException {
|
@RequestParam List<Station> stationen) throws IllegalParameterException {
|
||||||
|
|
||||||
if (description.length() == 0 || name.length() == 0 || rankingPoints.length() == 0 || stationen.size() == 0) {
|
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()) {
|
for (Cache cache : cacheRepository.findAll()) {
|
||||||
if (cache.getName().equals(name)) {
|
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 {
|
try {
|
||||||
points = Integer.valueOf(rankingPoints);
|
points = Integer.valueOf(rankingPoints);
|
||||||
if (points < 0) {
|
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) {
|
} 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));
|
Optional<Reward> rewardOptional = rewardRepository.findById(Integer.valueOf(rewardID));
|
||||||
@ -261,16 +265,16 @@ public class Controller {
|
|||||||
stationReihenfolgeRepository.save(stationReihenfolge);
|
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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/deleteCache")
|
@RequestMapping("/api/deleteCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
boolean deleteCache(@RequestParam String cacheID) throws IllegalParameterException {
|
ResponseEntity deleteCache(@RequestParam String cacheID) {
|
||||||
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
|
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||||
if (!optionalCache.isPresent()) {
|
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();
|
Cache cache = optionalCache.get();
|
||||||
@ -289,29 +293,53 @@ public class Controller {
|
|||||||
|
|
||||||
cacheRepository.delete(cache);
|
cacheRepository.delete(cache);
|
||||||
|
|
||||||
return true;
|
return ResponseEntity.status(200).body(new Gson().toJson(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/getMyCaches")
|
@RequestMapping("/api/getMyCaches")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
String getMyCaches(@RequestParam String token) {
|
ResponseEntity getMyCaches(@RequestParam String token) {
|
||||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
try {
|
||||||
ArrayList<Bearbeitet> bearbeitetList = new ArrayList<>();
|
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||||
|
if (user != null) {
|
||||||
|
ArrayList<Bearbeitet> bearbeitetList = new ArrayList<>();
|
||||||
|
|
||||||
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
||||||
if (bearbeitet.getUser().getId() == user.getId()) {
|
if (bearbeitet.getUser().getId() == user.getId()) {
|
||||||
bearbeitetList.add(bearbeitet);
|
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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/getRankingList")
|
@RequestMapping("/api/getRankingList")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
String getRankingList() {
|
ResponseEntity getRankingList() {
|
||||||
return new Gson().toJson(userRepository.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