More code cleanup
This commit is contained in:
parent
0de405167b
commit
d718446f36
@ -1,7 +1,6 @@
|
|||||||
package hhn.labsw.bugageocaching.controller;
|
package hhn.labsw.bugageocaching.controller;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import hhn.labsw.bugageocaching.entities.*;
|
import hhn.labsw.bugageocaching.entities.*;
|
||||||
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
|
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
|
||||||
import hhn.labsw.bugageocaching.repositories.*;
|
import hhn.labsw.bugageocaching.repositories.*;
|
||||||
@ -9,22 +8,18 @@ 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.lang.reflect.Array;
|
|
||||||
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 static hhn.labsw.bugageocaching.util.CacheConstructionUtil.*;
|
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil;
|
||||||
|
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class Controller {
|
public class Controller {
|
||||||
@ -49,9 +44,8 @@ public class Controller {
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
private AtomicLong counter = new AtomicLong();
|
|
||||||
byte[] key = new byte[64];
|
byte[] key = new byte[64];
|
||||||
|
private AtomicLong counter = new AtomicLong();
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -180,9 +174,6 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@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
|
||||||
@ -199,17 +190,6 @@ public class Controller {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(400).body("JWT Token invalid");
|
return ResponseEntity.status(400).body("JWT Token invalid");
|
||||||
}
|
}
|
||||||
|
|
||||||
/*User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
|
||||||
if (user == null) {
|
|
||||||
return ResponseEntity.status(404).body("User was not found");
|
|
||||||
}
|
|
||||||
for (Role role : user.getRoles()) {
|
|
||||||
if (role.getId() == 0) { // is admin
|
|
||||||
return ResponseEntity.status(200).body("User is Admin");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ResponseEntity.status(401).body("User is no Admin");*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Bis hier
|
//Bis hier
|
||||||
@ -224,32 +204,7 @@ public class Controller {
|
|||||||
@RequestMapping("/api/deleteCache")
|
@RequestMapping("/api/deleteCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity deleteCache(@RequestParam String cacheID) {
|
public ResponseEntity deleteCache(@RequestParam String cacheID) {
|
||||||
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
|
return deleteCacheUtil(cacheID);
|
||||||
if (!optionalCache.isPresent()) {
|
|
||||||
return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID));
|
|
||||||
}
|
|
||||||
|
|
||||||
Cache cache = optionalCache.get();
|
|
||||||
|
|
||||||
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
|
||||||
if (bearbeitet.getCache().getId() == cache.getId()) {
|
|
||||||
bearbeitetRepository.delete(bearbeitet);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<Station> stationen = new ArrayList<>();
|
|
||||||
for (Station station : cache.getStationen()) {
|
|
||||||
stationen.add(stationRepository.findById(station.getId()).get());
|
|
||||||
}
|
|
||||||
|
|
||||||
cacheRepository.delete(cache);
|
|
||||||
|
|
||||||
for (Station station : stationen) {
|
|
||||||
stationRepository.delete(station);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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
|
||||||
|
|||||||
@ -1,15 +1,19 @@
|
|||||||
package hhn.labsw.bugageocaching.util;
|
package hhn.labsw.bugageocaching.util;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import hhn.labsw.bugageocaching.entities.Bearbeitet;
|
||||||
import hhn.labsw.bugageocaching.entities.Cache;
|
import hhn.labsw.bugageocaching.entities.Cache;
|
||||||
import hhn.labsw.bugageocaching.entities.Station;
|
import hhn.labsw.bugageocaching.entities.Station;
|
||||||
import hhn.labsw.bugageocaching.entities.StationReihenfolge;
|
import hhn.labsw.bugageocaching.repositories.BearbeitetRepository;
|
||||||
import hhn.labsw.bugageocaching.repositories.CacheRepository;
|
import hhn.labsw.bugageocaching.repositories.CacheRepository;
|
||||||
import hhn.labsw.bugageocaching.repositories.StationRepository;
|
import hhn.labsw.bugageocaching.repositories.StationRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import javax.persistence.RollbackException;
|
import javax.persistence.RollbackException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
public class CacheConstructionUtil {
|
public class CacheConstructionUtil {
|
||||||
@ -20,6 +24,9 @@ public class CacheConstructionUtil {
|
|||||||
@Autowired
|
@Autowired
|
||||||
static CacheRepository cacheRepository;
|
static CacheRepository cacheRepository;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
static BearbeitetRepository bearbeitetRepository;
|
||||||
|
|
||||||
public static ResponseEntity createCacheUtil(Cache cache){
|
public static ResponseEntity createCacheUtil(Cache cache){
|
||||||
|
|
||||||
// Stationen werden in die Datenbank eingetragen
|
// Stationen werden in die Datenbank eingetragen
|
||||||
@ -112,4 +119,32 @@ public class CacheConstructionUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static ResponseEntity deleteCacheUtil(String cacheID) {
|
||||||
|
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||||
|
if (!optionalCache.isPresent()) {
|
||||||
|
return ResponseEntity.status(404).body(new Gson().toJson("There is no cache with the ID " + cacheID));
|
||||||
|
}
|
||||||
|
|
||||||
|
Cache cache = optionalCache.get();
|
||||||
|
|
||||||
|
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
||||||
|
if (bearbeitet.getCache().getId() == cache.getId()) {
|
||||||
|
bearbeitetRepository.delete(bearbeitet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ArrayList<Station> stationen = new ArrayList<>();
|
||||||
|
for (Station station : cache.getStationen()) {
|
||||||
|
stationen.add(stationRepository.findById(station.getId()).get());
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheRepository.delete(cache);
|
||||||
|
|
||||||
|
for (Station station : stationen) {
|
||||||
|
stationRepository.delete(station);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ResponseEntity.status(200).body(new Gson().toJson(true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user