From 148254e6103947526a0e76460be1a4b197400d43 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Apr 2019 14:51:51 +0200 Subject: [PATCH 1/9] reworked createCache to work with @RequestBody Cache cache --- .../bugageocaching/controller/Controller.java | 142 +++++++----------- 1 file changed, 57 insertions(+), 85 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index a2e93df..cd7a1ca 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -144,52 +144,78 @@ public class Controller { } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/createStation") + @RequestMapping("/api/createCache") @ResponseBody - public ResponseEntity createStation(@RequestParam String description, - @RequestParam String lattitude, - @RequestParam String longitude, - @RequestParam String solution) { + public ResponseEntity createCache(@RequestBody Cache cache) { - if (description.length() == 0 || lattitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) { - return ResponseEntity.status(400).body("At least one Argument was empty"); - } - - double latti; - double longi; - - try { - latti = Double.valueOf(lattitude); - if (latti < -90 || latti > 90) { - return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree"); + for (Station station : cache.getStationen()) { + ResponseEntity response = createStation(station); + if (response.getStatusCodeValue() == 400) { + return response; } - } catch (NumberFormatException e) { - return ResponseEntity.status(400).body("Latitude has to be a decimal"); } - try { - longi = Double.valueOf(longitude); - if (longi < -180 || longi > 180) { - return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees"); + if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) { + return ResponseEntity.status(400).body("cache fields can´t be empty"); + } + + for (Cache cache1 : cacheRepository.findAll()) { + if (cache1.getName().equals(cache.getName())) { + return ResponseEntity.status(400).body("name is already taken"); } - } catch (NumberFormatException e) { - return ResponseEntity.status(400).body("Longitude has to be a decimal number"); } - Station station = new Station(); - station.setDescription(description); - station.setLattitude(latti); - station.setLongitude(longi); - station.setSolution(solution); + if (cache.getRankingPoints() < 0) { + return ResponseEntity.status(400).body("Ranking points has to be a positive number"); + } + + cacheRepository.save(cache); + + for (int i = 0; i + 1 < cache.getStationen().size(); i++) { + StationReihenfolge stationReihenfolge = new StationReihenfolge(); + stationReihenfolge.setCache(cache); + stationReihenfolge.setStation(cache.getStationen().get(i)); + stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1)); + stationReihenfolgeRepository.save(stationReihenfolge); + } + + return ResponseEntity.status(200).body(new Gson().toJson(cache)); + } + + public ResponseEntity createStation(Station station) { + + if (station.getDescription().length() == 0 || Double.valueOf(station.getLattitude()) == null || Double.valueOf(station.getLongitude()) == null || station.getSolution().length() == 0) { + return ResponseEntity.status(400).body("station fields can´t be empty"); + } + + if (station.getLattitude() < -90 || station.getLattitude() > 90) { + return ResponseEntity.status(400).body("Lattitude has to be between -90 and 90 Degree"); + } + + if (station.getLongitude() < -180 || station.getLongitude() > 180) { + return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees"); + } + Random r = new Random(); int low = 100000; int high = 1000000; - int code = r.nextInt(high - low) + low; + int code = 0; + boolean unique = false; + while (!unique) { + code = r.nextInt(high - low) + low; + unique = true; + for (Station station1 : stationRepository.findAll()) { + if (station1.getCode() == code) { + unique = false; + } + } + } + station.setCode(code); stationRepository.save(station); - return ResponseEntity.status(201).body(new Gson().toJson(station)); + return ResponseEntity.status(200).body(new Gson().toJson(station)); } @@ -214,60 +240,6 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(stationRepository.findAll())); } - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose - @RequestMapping("/api/createCache") - @ResponseBody - public ResponseEntity createCache(@RequestParam String description, - @RequestParam String name, - @RequestParam String rankingPoints, - @RequestParam(value = "rewardID", defaultValue = "-1") String rewardID, - @RequestParam List stationen) { - - if (description.length() == 0 || name.length() == 0 || rankingPoints.length() == 0 || stationen.size() == 0) { - return ResponseEntity.status(400).body("Fields can´t be empty"); - } - - for (Cache cache : cacheRepository.findAll()) { - if (cache.getName().equals(name)) { - return ResponseEntity.status(400).body("name is already taken"); - } - } - - int points; - - try { - points = Integer.valueOf(rankingPoints); - if (points < 0) { - return ResponseEntity.status(400).body("Ranking points has to be a positive number"); - } - } catch (NumberFormatException e) { - return ResponseEntity.status(400).body("Ranking points has to be an integer"); - } - - Optional rewardOptional = rewardRepository.findById(Integer.valueOf(rewardID)); - Reward reward = rewardOptional.orElse(null); - - Cache cache = new Cache(); - cache.setDescription(description); - cache.setName(name); - cache.setRankingPoints(Integer.valueOf(rankingPoints)); - cache.setReward(reward); - cache.setStartStation(stationen.get(0)); - cache.setStationen(stationen); - - cacheRepository.save(cache); - - for (int i = 0; i + 1 < stationen.size(); i++) { - StationReihenfolge stationReihenfolge = new StationReihenfolge(); - stationReihenfolge.setCache(cache); - stationReihenfolge.setStation(stationen.get(i)); - stationReihenfolge.setNachfolgeStation(stationen.get(i + 1)); - stationReihenfolgeRepository.save(stationReihenfolge); - } - - return ResponseEntity.status(200).body(new Gson().toJson(cache)); - } - @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/deleteCache") @ResponseBody From c0448bda847c8a073e66e2ff343114f842ac643b Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Apr 2019 15:24:02 +0200 Subject: [PATCH 2/9] removed startStation field from cache --- .../labsw/bugageocaching/controller/Controller.java | 2 +- .../hhn/labsw/bugageocaching/entities/Cache.java | 13 +------------ 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index cd7a1ca..0e94893 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -97,7 +97,7 @@ public class Controller { Cache cache = cacheOptional.get(); bearbeitet.setCache(cache); - Station startStation = cache.getStartStation(); + Station startStation = cache.getStationen().get(0); bearbeitet.setAktuelleStation(startStation); } else { return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID); diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java b/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java index 6846b04..465b2cc 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Cache.java @@ -23,9 +23,6 @@ public class Cache { @ManyToOne private Reward reward; - @ManyToOne - private Station startStation; - public Cache() { } @@ -68,7 +65,7 @@ public class Cache { public void setStationen(List stationen) { this.stationen = stationen; } - + public Reward getReward() { return reward; } @@ -76,12 +73,4 @@ public class Cache { public void setReward(Reward reward) { this.reward = reward; } - - public Station getStartStation() { - return startStation; - } - - public void setStartStation(Station startStation) { - this.startStation = startStation; - } } From 6cf52503bcfdd3add41aeecac00e7f87f1b5cf82 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Apr 2019 15:33:31 +0200 Subject: [PATCH 3/9] if a mistake occurs during cache creation, all previously created stations related to that cache get deleted now --- .../bugageocaching/controller/Controller.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 0e94893..1c17bd0 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -151,21 +151,25 @@ public class Controller { for (Station station : cache.getStationen()) { ResponseEntity response = createStation(station); if (response.getStatusCodeValue() == 400) { + deleteStationen(cache); return response; } } if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) { + deleteStationen(cache); return ResponseEntity.status(400).body("cache fields can´t be empty"); } for (Cache cache1 : cacheRepository.findAll()) { if (cache1.getName().equals(cache.getName())) { + deleteStationen(cache); return ResponseEntity.status(400).body("name is already taken"); } } if (cache.getRankingPoints() < 0) { + deleteStationen(cache); return ResponseEntity.status(400).body("Ranking points has to be a positive number"); } @@ -218,6 +222,16 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(station)); } + public void deleteStationen(Cache cache) { + for (Station station : cache.getStationen()) { + try { + stationRepository.delete(station); + } catch (IllegalArgumentException e) { // station is null + // do nothing + } + } + } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkAdmin") From 5bf05616c82b509aa2284cd266316497753507a7 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Apr 2019 15:43:10 +0200 Subject: [PATCH 4/9] reworked checks to work with double instead of Double --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 1c17bd0..8c5af5c 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -156,7 +156,7 @@ public class Controller { } } - if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || Double.valueOf(cache.getRankingPoints()) == null || cache.getStationen().size() == 0) { + if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) { deleteStationen(cache); return ResponseEntity.status(400).body("cache fields can´t be empty"); } @@ -188,7 +188,7 @@ public class Controller { public ResponseEntity createStation(Station station) { - if (station.getDescription().length() == 0 || Double.valueOf(station.getLattitude()) == null || Double.valueOf(station.getLongitude()) == null || station.getSolution().length() == 0) { + 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"); } From 99d29ffdec717ad232fca226734cf5de8776a02b Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 5 Apr 2019 15:45:53 +0200 Subject: [PATCH 5/9] added comments --- .../java/hhn/labsw/bugageocaching/controller/Controller.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 8c5af5c..f04c688 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -148,6 +148,7 @@ public class Controller { @ResponseBody public ResponseEntity createCache(@RequestBody Cache cache) { + // Stationen werden in die Datenbank eingetragen for (Station station : cache.getStationen()) { ResponseEntity response = createStation(station); if (response.getStatusCodeValue() == 400) { @@ -156,6 +157,7 @@ public class Controller { } } + // Caches werden in die Datenbank eingetragen if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) { deleteStationen(cache); return ResponseEntity.status(400).body("cache fields can´t be empty"); From e86aa6adc2dcac9d52612a8e78b64b104b84ecf8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 7 Apr 2019 14:06:41 +0200 Subject: [PATCH 6/9] removed stationReihenfolge database table, because its not needed --- .../bugageocaching/controller/Controller.java | 17 ------ .../entities/StationReihenfolge.java | 53 ------------------- .../StationReihenfolgeRepository.java | 7 --- 3 files changed, 77 deletions(-) delete mode 100644 src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java delete mode 100644 src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index a0d9afc..6499b71 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -40,9 +40,6 @@ public class Controller { @Autowired UserRepository userRepository; - @Autowired - StationReihenfolgeRepository stationReihenfolgeRepository; - private AtomicLong counter = new AtomicLong(); @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @@ -177,14 +174,6 @@ public class Controller { cacheRepository.save(cache); - for (int i = 0; i + 1 < cache.getStationen().size(); i++) { - StationReihenfolge stationReihenfolge = new StationReihenfolge(); - stationReihenfolge.setCache(cache); - stationReihenfolge.setStation(cache.getStationen().get(i)); - stationReihenfolge.setNachfolgeStation(cache.getStationen().get(i + 1)); - stationReihenfolgeRepository.save(stationReihenfolge); - } - return ResponseEntity.status(200).body(new Gson().toJson(cache)); } @@ -270,12 +259,6 @@ public class Controller { Cache cache = optionalCache.get(); - for (StationReihenfolge stationReihenfolge : stationReihenfolgeRepository.findAll()) { - if (stationReihenfolge.getCache().getId() == cache.getId()) { - stationReihenfolgeRepository.delete(stationReihenfolge); - } - } - for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) { if (bearbeitet.getCache().getId() == cache.getId()) { bearbeitetRepository.delete(bearbeitet); diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java b/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java deleted file mode 100644 index b51ef51..0000000 --- a/src/main/java/hhn/labsw/bugageocaching/entities/StationReihenfolge.java +++ /dev/null @@ -1,53 +0,0 @@ -package hhn.labsw.bugageocaching.entities; - -import javax.persistence.*; - -@Entity -@Table -public class StationReihenfolge { - - @Id - @GeneratedValue - private int id; - - @OneToOne - private Cache cache; - - @OneToOne - private Station station; - - @OneToOne - private Station nachfolgeStation; - - public int getId() { - return id; - } - - public void setId(int id) { - this.id = id; - } - - public Cache getCache() { - return cache; - } - - public void setCache(Cache cache) { - this.cache = cache; - } - - public Station getStation() { - return station; - } - - public void setStation(Station station) { - this.station = station; - } - - public Station getNachfolgeStation() { - return nachfolgeStation; - } - - public void setNachfolgeStation(Station nachfolgeStation) { - this.nachfolgeStation = nachfolgeStation; - } -} diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java deleted file mode 100644 index 8897965..0000000 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/StationReihenfolgeRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package hhn.labsw.bugageocaching.repositories; - -import hhn.labsw.bugageocaching.entities.StationReihenfolge; -import org.springframework.data.repository.CrudRepository; - -public interface StationReihenfolgeRepository extends CrudRepository { -} From eb15f1fe356ea35496c0257d7b7c6dbdc9e0849b Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 8 Apr 2019 14:13:22 +0200 Subject: [PATCH 7/9] Added id to rankinglist --- .../hhn/labsw/bugageocaching/repositories/UserRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java index e0fe553..6ea725e 100644 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java +++ b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java @@ -10,6 +10,6 @@ import java.util.List; public interface UserRepository extends CrudRepository { User findByUsername(String username); - @Query(value = "SELECT u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true) + @Query(value = "SELECT u.id u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true) List getRankingList(); } From 629145b9c682b2688739de66113439daa9779a5e Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 8 Apr 2019 14:14:11 +0200 Subject: [PATCH 8/9] Komma vergessen --- .../hhn/labsw/bugageocaching/repositories/UserRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java index 6ea725e..81cddfe 100644 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java +++ b/src/main/java/hhn/labsw/bugageocaching/repositories/UserRepository.java @@ -10,6 +10,6 @@ import java.util.List; public interface UserRepository extends CrudRepository { User findByUsername(String username); - @Query(value = "SELECT u.id u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true) + @Query(value = "SELECT u.id, u.username, u.ranking_points_sum from user u order by ranking_points_sum DESC", nativeQuery = true) List getRankingList(); } From 5145b203c4cc4daaeb987457c13955c449d53171 Mon Sep 17 00:00:00 2001 From: Maximilian Leopold Date: Mon, 8 Apr 2019 14:44:13 +0200 Subject: [PATCH 9/9] Chaged format of RankingList JSON --- .../bugageocaching/controller/Controller.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 6499b71..4928476 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -1,6 +1,7 @@ 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.*; @@ -10,10 +11,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.bcrypt.BCrypt; import org.springframework.web.bind.annotation.*; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import java.util.Random; +import java.util.*; import java.util.concurrent.atomic.AtomicLong; @RestController @@ -297,7 +295,18 @@ public class Controller { @RequestMapping("/api/getRankingList") @ResponseBody public ResponseEntity getRankingList() { - return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList())); + + List sendBackUsers = new LinkedList<>(); + List rankingUsers = userRepository.getRankingList(); + for (Object[] obj : rankingUsers) { + User u = new User(); + u.setId((int) obj[0]); + u.setUsername((String) obj[1]); + u.setRankingPointsSum((int) obj[2]); + sendBackUsers.add(u); + } + + return ResponseEntity.status(200).body(new Gson().toJson(sendBackUsers)); } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose