diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 95b376a..f2451f3 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -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 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 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 diff --git a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java index d7594c7..308e263 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/CacheConstructionUtil.java @@ -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 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 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)); + } }