changes to editCache

This commit is contained in:
Michael 2019-05-14 11:11:31 +02:00
parent 0d2ac9af37
commit ea7a02e9f6

View File

@ -8,6 +8,7 @@ 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.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -86,12 +87,12 @@ public class CacheConstructionUtil {
return ResponseEntity.status(400).body("station fields can´t be empty"); return ResponseEntity.status(400).body("station fields can´t be empty");
} }
if (station.getLongitude() < 9 || station.getLongitude() > 10) { if (station.getLattitude() < 9 || station.getLattitude() > 10) {
return ResponseEntity.status(400).body("Lattitude has to be between 9 and 10 degrees"); return ResponseEntity.status(400).body("Lattitude has to be between 9 and 10 degrees");
} }
if (station.getLattitude() < 49 || station.getLattitude() > 50) { if (station.getLongitude() < 49 || station.getLongitude() > 50) {
return ResponseEntity.status(400).body("Lattitude has to be in the range of 49 to 50 degrees"); return ResponseEntity.status(400).body("Longitude has to be in the range of 49 to 50 degrees");
} }
Random r = new Random(); Random r = new Random();
@ -124,21 +125,28 @@ public class CacheConstructionUtil {
public static ResponseEntity editCacheUtil(Cache cache) { public static ResponseEntity editCacheUtil(Cache cache) {
Optional<Cache> oldCacheOptional = cacheRepository.findById(cache.getId()); Optional<Cache> oldCacheOptional = cacheRepository.findById(cache.getId());
Cache oldCache; Cache oldCache;
List<Station> oldStationen;
if (oldCacheOptional.isPresent()) { if (oldCacheOptional.isPresent()) {
oldCache = oldCacheOptional.get(); oldCache = oldCacheOptional.get();
oldStationen = new ArrayList<>(oldCache.getStationen());
} else { } else {
return ResponseEntity.status(404).body("There isnt a cache with the id " + cache.getId()); return ResponseEntity.status(404).body("There isnt a cache with the id " + cache.getId());
} }
ResponseEntity response; ResponseEntity response = new ResponseEntity(HttpStatus.ACCEPTED);
List<Station> newCreatedStationList = new ArrayList<Station>(); List<Station> newCreatedStationList = new ArrayList<Station>();
for (Station station : cache.getStationen()) { for (Station station : cache.getStationen()) {
boolean stationIsNew = true;
// wenn Station schon vorher vorhanden war, wird nur nach validen Daten geprüft // wenn Station schon vorher vorhanden war, wird nur nach validen Daten geprüft
if (oldCache.getStationen().contains(station)) { for (Station oldStation : oldCache.getStationen()) {
response = checkStationUtil(station); if (oldStation.getId() == station.getId()) {
response = createStationUtil(station);
stationIsNew = false;
break;
}
} }
// wenn Station neu hinzugefügt wurde, wird die Station zusätzlich in die Datenbank gespeichert // wenn Station neu hinzugefügt wurde, wird die Station zusätzlich in die Datenbank gespeichert
else { if (stationIsNew) {
response = createStationUtil(station); response = createStationUtil(station);
if (response.getStatusCodeValue() == 200) { if (response.getStatusCodeValue() == 200) {
newCreatedStationList.add(station); newCreatedStationList.add(station);
@ -146,6 +154,10 @@ public class CacheConstructionUtil {
} }
if (response.getStatusCodeValue() == 400) { if (response.getStatusCodeValue() == 400) {
// neu erzeugte Stationen werden gelöscht, falls es einen Fehler gibt // neu erzeugte Stationen werden gelöscht, falls es einen Fehler gibt
// alle veränderten Stationen werden resettet
for (Station oldStation : oldStationen) {
stationRepository.save(oldStation);
}
deleteNewCreatedStationsUtil(newCreatedStationList); deleteNewCreatedStationsUtil(newCreatedStationList);
return response; return response;
} }
@ -164,7 +176,7 @@ public class CacheConstructionUtil {
for (Cache cache1 : cacheRepository.findAll()) { for (Cache cache1 : cacheRepository.findAll()) {
if (cache1.getName().equals(cache.getName())) { if (cache1.getName().equals(cache.getName())) {
if (cache1 != oldCache) { if (cache1.getId() != oldCache.getId()) {
deleteNewCreatedStationsUtil(newCreatedStationList); deleteNewCreatedStationsUtil(newCreatedStationList);
return ResponseEntity.status(400).body("name is already taken"); return ResponseEntity.status(400).body("name is already taken");
} }
@ -178,12 +190,13 @@ public class CacheConstructionUtil {
cacheRepository.save(cache); cacheRepository.save(cache);
for (Station station : oldCache.getStationen()) { // nicht gebrauchte Stationen müssen noch gelöscht werden
// wenn Station entfernt wurde, wird diese auch aus der Datenbank gelöscht // for (Station oldStation : oldCache.getStationen()) {
if (!cache.getStationen().contains(station)) { // // wenn Station entfernt wurde, wird diese auch aus der Datenbank gelöscht
stationRepository.delete(station); // if (!cache.getStationen().contains(station)) {
} // stationRepository.delete(station);
} // }
// }
return ResponseEntity.status(200).body(new Gson().toJson(cache)); return ResponseEntity.status(200).body(new Gson().toJson(cache));
} }