Started reorganising backend code
This commit is contained in:
parent
14049730a4
commit
fe0dd478cd
@ -9,20 +9,17 @@ import io.jsonwebtoken.Claims;
|
|||||||
import io.jsonwebtoken.ExpiredJwtException;
|
import io.jsonwebtoken.ExpiredJwtException;
|
||||||
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.Jwts;
|
||||||
import io.jsonwebtoken.SignatureAlgorithm;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
||||||
import io.jsonwebtoken.security.Keys;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import javax.xml.bind.DatatypeConverter;
|
|
||||||
import java.security.Key;
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.*;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class Controller {
|
public class Controller {
|
||||||
@ -103,14 +100,6 @@ public class Controller {
|
|||||||
return ResponseEntity.status(200).body(token);
|
return ResponseEntity.status(200).body(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*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 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.status(200).body(token);
|
|
||||||
}*/
|
|
||||||
return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten");
|
return ResponseEntity.status(400).body("Es ist ein Fehler aufgetreten");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -176,113 +165,15 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Eigentlich brauchen wir mit JWT keine Logout Methode mehr.
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
|
||||||
@RequestMapping("/api/logout")
|
|
||||||
@ResponseBody
|
|
||||||
public 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 ResponseEntity.status(404).body("User was not found");
|
|
||||||
}
|
|
||||||
user.setToken(null);
|
|
||||||
userRepository.save(user);*/
|
|
||||||
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/createCache")
|
@RequestMapping("/api/createCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity createCache(@RequestBody Cache cache) {
|
public ResponseEntity createCache(@RequestBody Cache cache) {
|
||||||
|
return createCacheUtil(cache);
|
||||||
// Stationen werden in die Datenbank eingetragen
|
|
||||||
for (Station station : cache.getStationen()) {
|
|
||||||
ResponseEntity response = createStation(station);
|
|
||||||
if (response.getStatusCodeValue() == 400) {
|
|
||||||
deleteStationen(cache);
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Caches werden in die Datenbank eingetragen
|
|
||||||
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) {
|
|
||||||
deleteStationen(cache);
|
|
||||||
return ResponseEntity.status(400).body("cache fields can´t be empty");
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Cache cache1 : cacheRepository.findAll()) {
|
|
||||||
if (cache1.getName().equals(cache.getName())) {
|
|
||||||
deleteStationen(cache);
|
|
||||||
return ResponseEntity.status(400).body("name is already taken");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cache.getRankingPoints() < 0) {
|
|
||||||
deleteStationen(cache);
|
|
||||||
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 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 || 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 = 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(200).body(new Gson().toJson(station));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteStationen(Cache cache) {
|
|
||||||
for (Station station : cache.getStationen()) {
|
|
||||||
try {
|
|
||||||
stationRepository.delete(station);
|
|
||||||
} catch (IllegalArgumentException e) { // station is null
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.entities;
|
|||||||
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table
|
@Table
|
||||||
@ -11,10 +12,20 @@ public class Station {
|
|||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private int id;
|
private int id;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private double longitude;
|
private double longitude;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private double lattitude;
|
private double lattitude;
|
||||||
|
|
||||||
|
@Column(unique = true)
|
||||||
|
@NotNull
|
||||||
private int code;
|
private int code;
|
||||||
|
|
||||||
|
@NotNull
|
||||||
private String solution;
|
private String solution;
|
||||||
|
|
||||||
public Station() {
|
public Station() {
|
||||||
|
|||||||
@ -0,0 +1,115 @@
|
|||||||
|
package hhn.labsw.bugageocaching.util;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import hhn.labsw.bugageocaching.entities.Cache;
|
||||||
|
import hhn.labsw.bugageocaching.entities.Station;
|
||||||
|
import hhn.labsw.bugageocaching.entities.StationReihenfolge;
|
||||||
|
import hhn.labsw.bugageocaching.repositories.CacheRepository;
|
||||||
|
import hhn.labsw.bugageocaching.repositories.StationRepository;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import javax.persistence.RollbackException;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
public class CacheConstructionUtil {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
static StationRepository stationRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
static CacheRepository cacheRepository;
|
||||||
|
|
||||||
|
public static ResponseEntity createCacheUtil(Cache cache){
|
||||||
|
|
||||||
|
// Stationen werden in die Datenbank eingetragen
|
||||||
|
for (Station station : cache.getStationen()) {
|
||||||
|
ResponseEntity response = createStationUtil(station);
|
||||||
|
if (response.getStatusCodeValue() == 400) {
|
||||||
|
deleteStationenUtil(cache);
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Caches werden in die Datenbank eingetragen
|
||||||
|
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) {
|
||||||
|
deleteStationenUtil(cache);
|
||||||
|
return ResponseEntity.status(400).body("cache fields can´t be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Cache cache1 : cacheRepository.findAll()) {
|
||||||
|
if (cache1.getName().equals(cache.getName())) {
|
||||||
|
deleteStationenUtil(cache);
|
||||||
|
return ResponseEntity.status(400).body("name is already taken");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cache.getRankingPoints() < 0) {
|
||||||
|
deleteStationenUtil(cache);
|
||||||
|
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 static ResponseEntity createStationUtil(Station station) {
|
||||||
|
|
||||||
|
if (station.getDescription().length() == 0 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 || 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 = 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
station.setCode(code);
|
||||||
|
}catch (RollbackException e){
|
||||||
|
return ResponseEntity.status(400).body("There was an error generating the unique code");
|
||||||
|
}
|
||||||
|
|
||||||
|
stationRepository.save(station);
|
||||||
|
|
||||||
|
return ResponseEntity.status(200).body(new Gson().toJson(station));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteStationenUtil(Cache cache) {
|
||||||
|
for (Station station : cache.getStationen()) {
|
||||||
|
try {
|
||||||
|
stationRepository.delete(station);
|
||||||
|
} catch (IllegalArgumentException e) { // station is null
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user