first implementation of editCache
This commit is contained in:
parent
e64b607276
commit
c1dcba7c04
@ -6,6 +6,7 @@ import hhn.labsw.bugageocaching.helper.POI;
|
||||
import hhn.labsw.bugageocaching.helper.RankingListHelper;
|
||||
import hhn.labsw.bugageocaching.helper.TeamRankingListHelper;
|
||||
import hhn.labsw.bugageocaching.repositories.*;
|
||||
import hhn.labsw.bugageocaching.util.CacheConstructionUtil;
|
||||
import hhn.labsw.bugageocaching.util.FinderUtil;
|
||||
import hhn.labsw.bugageocaching.util.VerificationUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
@ -24,6 +25,7 @@ import java.util.List;
|
||||
|
||||
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil;
|
||||
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil;
|
||||
import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.editCacheUtil;
|
||||
import static hhn.labsw.bugageocaching.util.VerificationUtil.fetchPublicKey;
|
||||
|
||||
@RestController
|
||||
@ -57,7 +59,7 @@ public class Controller {
|
||||
TeamInviteRepository teamInviteRepository;
|
||||
|
||||
@PostConstruct
|
||||
public void init(){
|
||||
public void init() {
|
||||
fetchPublicKey();
|
||||
}
|
||||
|
||||
@ -333,28 +335,30 @@ public class Controller {
|
||||
@CrossOrigin(origins = "*", allowedHeaders = "*") // only for dev purpose
|
||||
@RequestMapping(value = "/api/editCache", method = RequestMethod.PUT, produces = "application/json")
|
||||
@ResponseBody
|
||||
public ResponseEntity editCache(@RequestBody Cache newCache){
|
||||
public ResponseEntity editCache(@RequestBody Cache newCache) {
|
||||
|
||||
//----------------------
|
||||
//Get Cache
|
||||
ResponseEntity getCache = FinderUtil.findCacheById(newCache.getId() + "");
|
||||
// //----------------------
|
||||
// //Get Cache
|
||||
// ResponseEntity getCache = FinderUtil.findCacheById(newCache.getId() + "");
|
||||
//
|
||||
// if (getCache.getStatusCodeValue() != 200) {
|
||||
// return getCache;
|
||||
// }
|
||||
//
|
||||
// Cache oldCache = (Cache) getCache.getBody();
|
||||
// //----------------------
|
||||
//
|
||||
// oldCache.setDescription(newCache.getDescription());
|
||||
// oldCache.setName(newCache.getName());
|
||||
// oldCache.setRankingPoints(newCache.getRankingPoints());
|
||||
// oldCache.setReward(newCache.getReward());
|
||||
// oldCache.setStationen(newCache.getStationen());
|
||||
//
|
||||
// cacheRepository.save(oldCache);
|
||||
//
|
||||
// return ResponseEntity.status(200).body("Cache edited");
|
||||
|
||||
if (getCache.getStatusCodeValue() != 200) {
|
||||
return getCache;
|
||||
}
|
||||
|
||||
Cache oldCache = (Cache) getCache.getBody();
|
||||
//----------------------
|
||||
|
||||
oldCache.setDescription(newCache.getDescription());
|
||||
oldCache.setName(newCache.getName());
|
||||
oldCache.setRankingPoints(newCache.getRankingPoints());
|
||||
oldCache.setReward(newCache.getReward());
|
||||
oldCache.setStationen(newCache.getStationen());
|
||||
|
||||
cacheRepository.save(oldCache);
|
||||
|
||||
return ResponseEntity.status(200).body("Cache edited");
|
||||
return editCacheUtil(newCache);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Checks if the given User has an admin role")
|
||||
@ -823,7 +827,7 @@ public class Controller {
|
||||
|
||||
List<TeamInvite> teamInvitesList = teamInviteRepository.findByUser(user);
|
||||
|
||||
for (TeamInvite tmp : teamInvitesList){
|
||||
for (TeamInvite tmp : teamInvitesList) {
|
||||
tmp.setUser(null);
|
||||
}
|
||||
|
||||
|
||||
@ -13,6 +13,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.persistence.RollbackException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@ -120,6 +121,87 @@ public class CacheConstructionUtil {
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(station));
|
||||
}
|
||||
|
||||
public static ResponseEntity editCacheUtil(Cache cache) {
|
||||
Optional<Cache> oldCacheOptional = cacheRepository.findById(cache.getId());
|
||||
Cache oldCache;
|
||||
if (oldCacheOptional.isPresent()) {
|
||||
oldCache = oldCacheOptional.get();
|
||||
} else {
|
||||
return ResponseEntity.status(404).body("There isnt a cache with the id " + cache.getId());
|
||||
}
|
||||
|
||||
ResponseEntity response;
|
||||
List<Station> newCreatedStationList = new ArrayList<Station>();
|
||||
for (Station station : cache.getStationen()) {
|
||||
// wenn Station schon vorher vorhanden war, wird nur nach validen Daten geprüft
|
||||
if (oldCache.getStationen().contains(station)) {
|
||||
response = checkStationUtil(station);
|
||||
}
|
||||
// wenn Station neu hinzugefügt wurde, wird die Station zusätzlich in die Datenbank gespeichert
|
||||
else {
|
||||
response = createStationUtil(station);
|
||||
if (response.getStatusCodeValue() == 200) {
|
||||
newCreatedStationList.add(station);
|
||||
}
|
||||
}
|
||||
if (response.getStatusCodeValue() == 400) {
|
||||
// neu erzeugte Stationen werden gelöscht, falls es einen Fehler gibt
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
// überprüft den Cache nach validen Daten
|
||||
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0) {
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return ResponseEntity.status(400).body("cache fields can´t be empty");
|
||||
}
|
||||
|
||||
if (cache.getStationen().size() < 2) {
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return ResponseEntity.status(400).body("a cache needs atleast 2 stations");
|
||||
}
|
||||
|
||||
for (Cache cache1 : cacheRepository.findAll()) {
|
||||
if (cache1.getName().equals(cache.getName())) {
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return ResponseEntity.status(400).body("name is already taken");
|
||||
}
|
||||
}
|
||||
|
||||
if (cache.getRankingPoints() < 0) {
|
||||
deleteNewCreatedStationsUtil(newCreatedStationList);
|
||||
return ResponseEntity.status(400).body("Ranking points has to be a positive number");
|
||||
}
|
||||
|
||||
cacheRepository.save(cache);
|
||||
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||
}
|
||||
|
||||
public static ResponseEntity checkStationUtil(Station station) {
|
||||
if (station.getDescription().length() == 0 || station.getLattitude() == 0.0 || station.getLongitude() == 0.0 /*|| station.getSolution().length() == 0*/) {
|
||||
return ResponseEntity.status(400).body("station fields can´t be empty");
|
||||
}
|
||||
|
||||
if (station.getLattitude() < 9 || station.getLattitude() > 10) {
|
||||
return ResponseEntity.status(400).body("Lattitude has to be between 9 and 10 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");
|
||||
}
|
||||
|
||||
return ResponseEntity.status(200).body(new Gson().toJson(station));
|
||||
}
|
||||
|
||||
public static void deleteStationenUtil(Cache cache) {
|
||||
for (Station station : cache.getStationen()) {
|
||||
try {
|
||||
@ -130,6 +212,16 @@ public class CacheConstructionUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static void deleteNewCreatedStationsUtil(List<Station> newCreatedStationList) {
|
||||
for (Station station : newCreatedStationList) {
|
||||
try {
|
||||
stationRepository.delete(station);
|
||||
} catch (IllegalArgumentException e) { // station is null
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static ResponseEntity deleteCacheUtil(String cacheID) {
|
||||
Optional<Cache> optionalCache = cacheRepository.findById(Integer.valueOf(cacheID));
|
||||
if (!optionalCache.isPresent()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user