changes to editCache
This commit is contained in:
parent
0d2ac9af37
commit
ea7a02e9f6
@ -8,6 +8,7 @@ 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.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@ -86,12 +87,12 @@ public class CacheConstructionUtil {
|
||||
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");
|
||||
}
|
||||
|
||||
if (station.getLattitude() < 49 || station.getLattitude() > 50) {
|
||||
return ResponseEntity.status(400).body("Lattitude has to be in the range of 49 to 50 degrees");
|
||||
if (station.getLongitude() < 49 || station.getLongitude() > 50) {
|
||||
return ResponseEntity.status(400).body("Longitude has to be in the range of 49 to 50 degrees");
|
||||
}
|
||||
|
||||
Random r = new Random();
|
||||
@ -124,21 +125,28 @@ public class CacheConstructionUtil {
|
||||
public static ResponseEntity editCacheUtil(Cache cache) {
|
||||
Optional<Cache> oldCacheOptional = cacheRepository.findById(cache.getId());
|
||||
Cache oldCache;
|
||||
List<Station> oldStationen;
|
||||
if (oldCacheOptional.isPresent()) {
|
||||
oldCache = oldCacheOptional.get();
|
||||
oldStationen = new ArrayList<>(oldCache.getStationen());
|
||||
} else {
|
||||
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>();
|
||||
for (Station station : cache.getStationen()) {
|
||||
boolean stationIsNew = true;
|
||||
// wenn Station schon vorher vorhanden war, wird nur nach validen Daten geprüft
|
||||
if (oldCache.getStationen().contains(station)) {
|
||||
response = checkStationUtil(station);
|
||||
for (Station oldStation : oldCache.getStationen()) {
|
||||
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
|
||||
else {
|
||||
if (stationIsNew) {
|
||||
response = createStationUtil(station);
|
||||
if (response.getStatusCodeValue() == 200) {
|
||||
newCreatedStationList.add(station);
|
||||
@ -146,6 +154,10 @@ public class CacheConstructionUtil {
|
||||
}
|
||||
if (response.getStatusCodeValue() == 400) {
|
||||
// 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);
|
||||
return response;
|
||||
}
|
||||
@ -164,7 +176,7 @@ public class CacheConstructionUtil {
|
||||
|
||||
for (Cache cache1 : cacheRepository.findAll()) {
|
||||
if (cache1.getName().equals(cache.getName())) {
|
||||
if (cache1 != oldCache) {
|
||||
if (cache1.getId() != oldCache.getId()) {
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return ResponseEntity.status(400).body("name is already taken");
|
||||
}
|
||||
@ -178,12 +190,13 @@ public class CacheConstructionUtil {
|
||||
|
||||
cacheRepository.save(cache);
|
||||
|
||||
for (Station station : oldCache.getStationen()) {
|
||||
// wenn Station entfernt wurde, wird diese auch aus der Datenbank gelöscht
|
||||
if (!cache.getStationen().contains(station)) {
|
||||
stationRepository.delete(station);
|
||||
}
|
||||
}
|
||||
// nicht gebrauchte Stationen müssen noch gelöscht werden
|
||||
// for (Station oldStation : oldCache.getStationen()) {
|
||||
// // wenn Station entfernt wurde, wird diese auch aus der Datenbank gelöscht
|
||||
// if (!cache.getStationen().contains(station)) {
|
||||
// stationRepository.delete(station);
|
||||
// }
|
||||
// }
|
||||
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user