Merge branch 'develop' into frontend/timo
This commit is contained in:
commit
57e66dcc45
@ -1,6 +1,7 @@
|
|||||||
package hhn.labsw.bugageocaching.controller;
|
package hhn.labsw.bugageocaching.controller;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.GsonBuilder;
|
||||||
import hhn.labsw.bugageocaching.entities.*;
|
import hhn.labsw.bugageocaching.entities.*;
|
||||||
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
|
import hhn.labsw.bugageocaching.exceptions.IllegalParameterException;
|
||||||
import hhn.labsw.bugageocaching.repositories.*;
|
import hhn.labsw.bugageocaching.repositories.*;
|
||||||
@ -10,10 +11,7 @@ import org.springframework.http.ResponseEntity;
|
|||||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.concurrent.atomic.AtomicLong;
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@ -40,9 +38,6 @@ public class Controller {
|
|||||||
@Autowired
|
@Autowired
|
||||||
UserRepository userRepository;
|
UserRepository userRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
StationReihenfolgeRepository stationReihenfolgeRepository;
|
|
||||||
|
|
||||||
private AtomicLong counter = new AtomicLong();
|
private AtomicLong counter = new AtomicLong();
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@ -97,7 +92,7 @@ public class Controller {
|
|||||||
Cache cache = cacheOptional.get();
|
Cache cache = cacheOptional.get();
|
||||||
bearbeitet.setCache(cache);
|
bearbeitet.setCache(cache);
|
||||||
|
|
||||||
Station startStation = cache.getStartStation();
|
Station startStation = cache.getStationen().get(0);
|
||||||
bearbeitet.setAktuelleStation(startStation);
|
bearbeitet.setAktuelleStation(startStation);
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
|
return ResponseEntity.status(404).body("Couldnt find Cache " + cacheID);
|
||||||
@ -110,7 +105,7 @@ public class Controller {
|
|||||||
bearbeitet.setCacheAccesDefinition(cacheAccesDefinition);
|
bearbeitet.setCacheAccesDefinition(cacheAccesDefinition);
|
||||||
} else {
|
} else {
|
||||||
return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 0);
|
return ResponseEntity.status(404).body("There is no cacheAccesDefinition with the ID " + 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
bearbeitetRepository.save(bearbeitet);
|
bearbeitetRepository.save(bearbeitet);
|
||||||
|
|
||||||
@ -144,52 +139,86 @@ public class Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/createStation")
|
@RequestMapping("/api/createCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity createStation(@RequestParam String description,
|
public ResponseEntity createCache(@RequestBody Cache cache) {
|
||||||
@RequestParam String latitude,
|
|
||||||
@RequestParam String longitude,
|
|
||||||
@RequestParam String solution) {
|
|
||||||
|
|
||||||
if (description.length() == 0 || latitude.length() == 0 || longitude.length() == 0 || solution.length() == 0) {
|
// Stationen werden in die Datenbank eingetragen
|
||||||
return ResponseEntity.status(400).body("At least one Argument was empty");
|
for (Station station : cache.getStationen()) {
|
||||||
}
|
ResponseEntity response = createStation(station);
|
||||||
|
if (response.getStatusCodeValue() == 400) {
|
||||||
double latti;
|
deleteStationen(cache);
|
||||||
double longi;
|
return response;
|
||||||
|
|
||||||
try {
|
|
||||||
latti = Double.valueOf(latitude);
|
|
||||||
if (latti < -90 || latti > 90) {
|
|
||||||
return ResponseEntity.status(400).body("Latitude has to be between -90 and 90 Degree");
|
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return ResponseEntity.status(400).body("Latitude has to be a decimal");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Caches werden in die Datenbank eingetragen
|
||||||
longi = Double.valueOf(longitude);
|
if (cache.getDescription().length() == 0 || cache.getName().length() == 0 || cache.getRankingPoints() == 0.0 || cache.getStationen().size() == 0) {
|
||||||
if (longi < -180 || longi > 180) {
|
deleteStationen(cache);
|
||||||
return ResponseEntity.status(400).body("Longitude has to be in the range of -180 to 180 degrees");
|
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");
|
||||||
}
|
}
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
return ResponseEntity.status(400).body("Longitude has to be a decimal number");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Station station = new Station();
|
if (cache.getRankingPoints() < 0) {
|
||||||
station.setDescription(description);
|
deleteStationen(cache);
|
||||||
station.setLattitude(latti);
|
return ResponseEntity.status(400).body("Ranking points has to be a positive number");
|
||||||
station.setLongitude(longi);
|
}
|
||||||
station.setSolution(solution);
|
|
||||||
|
cacheRepository.save(cache);
|
||||||
|
|
||||||
|
return ResponseEntity.status(200).body(new Gson().toJson(cache));
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResponseEntity createStation(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() < -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();
|
Random r = new Random();
|
||||||
int low = 100000;
|
int low = 100000;
|
||||||
int high = 1000000;
|
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);
|
station.setCode(code);
|
||||||
|
|
||||||
stationRepository.save(station);
|
stationRepository.save(station);
|
||||||
|
|
||||||
return ResponseEntity.status(201).body(new Gson().toJson(station));
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -198,7 +227,7 @@ public class Controller {
|
|||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity checkAdmin(@RequestParam String token) {
|
public ResponseEntity checkAdmin(@RequestParam String token) {
|
||||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||||
if(user == null){
|
if (user == null) {
|
||||||
return ResponseEntity.status(404).body("User was not found");
|
return ResponseEntity.status(404).body("User was not found");
|
||||||
}
|
}
|
||||||
for (Role role : user.getRoles()) {
|
for (Role role : user.getRoles()) {
|
||||||
@ -217,60 +246,6 @@ public class Controller {
|
|||||||
return ResponseEntity.status(200).body(new Gson().toJson(stationRepository.findAll()));
|
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<Station> 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<Reward> 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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
@RequestMapping("/api/deleteCache")
|
@RequestMapping("/api/deleteCache")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@ -282,12 +257,6 @@ public class Controller {
|
|||||||
|
|
||||||
Cache cache = optionalCache.get();
|
Cache cache = optionalCache.get();
|
||||||
|
|
||||||
for (StationReihenfolge stationReihenfolge : stationReihenfolgeRepository.findAll()) {
|
|
||||||
if (stationReihenfolge.getCache().getId() == cache.getId()) {
|
|
||||||
stationReihenfolgeRepository.delete(stationReihenfolge);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
for (Bearbeitet bearbeitet : bearbeitetRepository.findAll()) {
|
||||||
if (bearbeitet.getCache().getId() == cache.getId()) {
|
if (bearbeitet.getCache().getId() == cache.getId()) {
|
||||||
bearbeitetRepository.delete(bearbeitet);
|
bearbeitetRepository.delete(bearbeitet);
|
||||||
@ -326,7 +295,18 @@ public class Controller {
|
|||||||
@RequestMapping("/api/getRankingList")
|
@RequestMapping("/api/getRankingList")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity getRankingList() {
|
public ResponseEntity getRankingList() {
|
||||||
return ResponseEntity.status(200).body(new Gson().toJson(userRepository.getRankingList()));
|
|
||||||
|
List<User> sendBackUsers = new LinkedList<>();
|
||||||
|
List<Object[]> 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
|
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||||
|
|||||||
@ -23,9 +23,6 @@ public class Cache {
|
|||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Reward reward;
|
private Reward reward;
|
||||||
|
|
||||||
@ManyToOne
|
|
||||||
private Station startStation;
|
|
||||||
|
|
||||||
public Cache() {
|
public Cache() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +65,7 @@ public class Cache {
|
|||||||
public void setStationen(List<Station> stationen) {
|
public void setStationen(List<Station> stationen) {
|
||||||
this.stationen = stationen;
|
this.stationen = stationen;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Reward getReward() {
|
public Reward getReward() {
|
||||||
return reward;
|
return reward;
|
||||||
}
|
}
|
||||||
@ -76,12 +73,4 @@ public class Cache {
|
|||||||
public void setReward(Reward reward) {
|
public void setReward(Reward reward) {
|
||||||
this.reward = reward;
|
this.reward = reward;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Station getStartStation() {
|
|
||||||
return startStation;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStartStation(Station startStation) {
|
|
||||||
this.startStation = startStation;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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<StationReihenfolge, Integer> {
|
|
||||||
}
|
|
||||||
@ -10,6 +10,6 @@ import java.util.List;
|
|||||||
public interface UserRepository extends CrudRepository<User, Integer> {
|
public interface UserRepository extends CrudRepository<User, Integer> {
|
||||||
User findByUsername(String username);
|
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<Object[]> getRankingList();
|
List<Object[]> getRankingList();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user