More code cleanup

This commit is contained in:
Maximilian Leopold 2019-04-13 14:35:08 +02:00
parent 0de405167b
commit d718446f36
2 changed files with 40 additions and 50 deletions

View File

@ -1,7 +1,6 @@
package hhn.labsw.bugageocaching.controller;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import hhn.labsw.bugageocaching.entities.*;
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
import hhn.labsw.bugageocaching.repositories.*;
@ -9,22 +8,18 @@ import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.web.bind.annotation.*;
import javax.annotation.PostConstruct;
import javax.xml.bind.DatatypeConverter;
import java.lang.reflect.Array;
import java.security.Key;
import java.security.SecureRandom;
import java.util.*;
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
public class Controller {
@ -49,9 +44,8 @@ public class Controller {
@Autowired
UserRepository userRepository;
private AtomicLong counter = new AtomicLong();
byte[] key = new byte[64];
private AtomicLong counter = new AtomicLong();
@PostConstruct
public void init() {
@ -180,9 +174,6 @@ public class Controller {
}
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
@RequestMapping("/api/checkAdmin")
@ResponseBody
@ -199,17 +190,6 @@ public class Controller {
} catch (Exception e) {
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
@ -224,32 +204,7 @@ public class Controller {
@RequestMapping("/api/deleteCache")
@ResponseBody
public ResponseEntity deleteCache(@RequestParam 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));
return deleteCacheUtil(cacheID);
}
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose

View File

@ -1,15 +1,19 @@
package hhn.labsw.bugageocaching.util;
import com.google.gson.Gson;
import hhn.labsw.bugageocaching.entities.Bearbeitet;
import hhn.labsw.bugageocaching.entities.Cache;
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.StationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestParam;
import javax.persistence.RollbackException;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Random;
public class CacheConstructionUtil {
@ -20,6 +24,9 @@ public class CacheConstructionUtil {
@Autowired
static CacheRepository cacheRepository;
@Autowired
static BearbeitetRepository bearbeitetRepository;
public static ResponseEntity createCacheUtil(Cache cache){
// 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));
}
}