diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 528af69..332a903 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -16,7 +16,6 @@ import javax.annotation.PostConstruct; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -import java.util.Optional; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.createCacheUtil; import static hhn.labsw.bugageocaching.util.CacheConstructionUtil.deleteCacheUtil; @@ -64,88 +63,88 @@ public class Controller { return ResponseEntity.status(200).body(new Gson().toJson(cacheRepository.findAll())); } - // alte startCache-methode -// @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose -// @RequestMapping("/api/startCache") -// @ResponseBody -// public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token, -// @RequestParam String cacheID) { -// -// if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben) -// -// Bearbeitet bearbeitet = new Bearbeitet(); -// -// -// //---------------------- -// //Verify token -// ResponseEntity tokenVerification = VerificationUtil.verifyToken(token); -// -// //Error in token verification -// if (tokenVerification.getStatusCodeValue() != 200) { -// return tokenVerification; -// } -// -// Claims claims = (Claims) tokenVerification.getBody(); -// -// ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); -// -// if (getUser.getStatusCodeValue() != 200) { -// return getUser; -// } -// -// User user = (User) getUser.getBody(); -// -// bearbeitet.setUser(user); -// -// //---------------------- -// //Get Cache -// ResponseEntity getCache = FinderUtil.findCacheById(cacheID); -// -// if (getCache.getStatusCodeValue() != 200) { -// return getCache; -// } -// -// Cache cache = (Cache) getCache.getBody(); -// //---------------------- -// -// if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { -// Bearbeitet bearbeitet1 = bearbeitetRepository.findByUserAndCache(user, cache); -// return ResponseEntity.status(200).body(bearbeitet1); -// } -// -// bearbeitet.setCache(cache); -// -// Station startStation = cache.getStationen().get(0); -// bearbeitet.setAktuelleStation(startStation); -// -// //Get CacheAccesDefinition -// ResponseEntity getCacheAccesDefinition = FinderUtil.findCacheAccesDefinitionById("0"); -// -// if (getCacheAccesDefinition.getStatusCodeValue() != 200) { -// return getCacheAccesDefinition; -// } -// -// CacheAccesDefinition cacheAccesDefinition = (CacheAccesDefinition) getCacheAccesDefinition.getBody(); -// //---------------------- -// bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); -// -// bearbeitetRepository.save(bearbeitet); -// -// return ResponseEntity.status(201).body(new Gson().toJson(bearbeitet)); -// -// } else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben) -// -// ResponseEntity getCache = FinderUtil.findCacheById(cacheID); -// -// if (getCache.getStatusCodeValue() != 200) { -// return getCache; -// } -// -// Cache cache = (Cache) getCache.getBody(); -// -// return ResponseEntity.status(200).body(new Gson().toJson(cache)); -// } -// } + + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/startCache") + @ResponseBody + public ResponseEntity startCache(@RequestParam(value = "token", defaultValue = "-1") String token, + @RequestParam String cacheID) { + + if (!token.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben) + + Bearbeitet bearbeitet = new Bearbeitet(); + + + //---------------------- + //Verify token + ResponseEntity tokenVerification = VerificationUtil.verifyToken(token); + + //Error in token verification + if (tokenVerification.getStatusCodeValue() != 200) { + return tokenVerification; + } + + Claims claims = (Claims) tokenVerification.getBody(); + + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; + } + + User user = (User) getUser.getBody(); + + bearbeitet.setUser(user); + + //---------------------- + //Get Cache + ResponseEntity getCache = FinderUtil.findCacheById(cacheID); + + if (getCache.getStatusCodeValue() != 200) { + return getCache; + } + + Cache cache = (Cache) getCache.getBody(); + //---------------------- + + if (bearbeitetRepository.findByUserAndCache(user, cache) != null) { + Bearbeitet bearbeitet1 = bearbeitetRepository.findByUserAndCache(user, cache); + return ResponseEntity.status(200).body(bearbeitet1); + } + + bearbeitet.setCache(cache); + + Station startStation = cache.getStationen().get(0); + bearbeitet.setAktuelleStation(startStation); + + //Get CacheAccesDefinition + ResponseEntity getCacheAccesDefinition = FinderUtil.findCacheAccesDefinitionById("0"); + + if (getCacheAccesDefinition.getStatusCodeValue() != 200) { + return getCacheAccesDefinition; + } + + CacheAccesDefinition cacheAccesDefinition = (CacheAccesDefinition) getCacheAccesDefinition.getBody(); + //---------------------- + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + + //bearbeitetRepository.save(bearbeitet); + + return ResponseEntity.status(201).body(new Gson().toJson(bearbeitet)); + + } else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben) + + ResponseEntity getCache = FinderUtil.findCacheById(cacheID); + + if (getCache.getStatusCodeValue() != 200) { + return getCache; + } + + Cache cache = (Cache) getCache.getBody(); + + return ResponseEntity.status(200).body(new Gson().toJson(cache)); + } + } @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose @RequestMapping("/api/checkStation") @@ -208,8 +207,11 @@ public class Controller { Station station = (Station) getStation.getBody(); //---------------------- - if (cache != durchgefuehrterCache) { - return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + System.out.println(cache.getName()); + System.out.println(durchgefuehrterCache.getName()); + + if (!cacheID.equals(durchgefuehrterCacheID)) { + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station (Name)"); } if (!cache.getStationen().contains(station)) { @@ -223,6 +225,7 @@ public class Controller { if (getBearbeitet.getStatusCodeValue() != 200) { if (cache.getStationen().get(0).equals(station)) { // start Cache + System.out.println("Startstation"); Bearbeitet bearbeitet = new Bearbeitet(); bearbeitet.setUser(user); bearbeitet.setCache(cache); @@ -259,7 +262,7 @@ public class Controller { int i = cache.getStationen().indexOf(station); if (i == 0) { - return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station (i==0)"); } if (cache.getStationen().get(i - 1).equals(aktuelleStation)) { @@ -277,13 +280,8 @@ public class Controller { //---------------------- bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); //Get User_Info - ResponseEntity getUser_Info = FinderUtil.findUser_InfoByID(String.valueOf(user.getId())); - - if (getUser_Info.getStatusCodeValue() != 200) { - return getUser_Info; - } - - User_Info user_info = (User_Info) getUser_Info.getBody(); + System.out.println(String.valueOf(user.getId())); + User_Info user_info = user_infoRepository.findUser_InfoByUser(user); //---------------------- user_info.setRankingPointsSum(user_info.getRankingPointsSum() + cache.getRankingPoints()); user_infoRepository.save(user_info); @@ -291,7 +289,7 @@ public class Controller { bearbeitetRepository.save(bearbeitet); return ResponseEntity.status(200).body(new Gson().toJson(bearbeitet)); } else { - return ResponseEntity.status(400).body("The scanned station isn´t the correct following station"); + return ResponseEntity.status(400).body("The scanned station isn´t the correct following station(nicht letzte)"); } } @@ -467,13 +465,7 @@ public class Controller { User user = (User) getUser.getBody(); //Get User_Info - ResponseEntity getUser_Info = FinderUtil.findUser_InfoByID(String.valueOf(user.getId())); - - if (getUser_Info.getStatusCodeValue() != 200) { - return getUser_Info; - } - - User_Info user_info = (User_Info) getUser_Info.getBody(); + User_Info user_info = user_infoRepository.findUser_InfoByUser(user); //---------------------- if (user_info.getTeam() != null) { return ResponseEntity.status(400).body("You already have a team"); @@ -520,13 +512,7 @@ public class Controller { User user = (User) getUser.getBody(); //Get User_Info - ResponseEntity getUser_Info = FinderUtil.findUser_InfoByID(String.valueOf(user.getId())); - - if (getUser_Info.getStatusCodeValue() != 200) { - return getUser_Info; - } - - User_Info user_info = (User_Info) getUser_Info.getBody(); + User_Info user_info = user_infoRepository.findUser_InfoByUser(user); //---------------------- if (user_info.getTeam() != null) { return ResponseEntity.status(400).body("You already have a team"); @@ -587,13 +573,7 @@ public class Controller { User user = (User) getUser.getBody(); //Get User_Info - ResponseEntity getUser_Info = FinderUtil.findUser_InfoByID(String.valueOf(user.getId())); - - if (getUser_Info.getStatusCodeValue() != 200) { - return getUser_Info; - } - - User_Info user_info = (User_Info) getUser_Info.getBody(); + User_Info user_info = user_infoRepository.findUser_InfoByUser(user); //---------------------- if (user_info.getTeam() == null) { return ResponseEntity.status(400).body("You aren´t in any team"); diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java index 74ff0a3..c74c428 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_Info.java @@ -1,7 +1,9 @@ package hhn.labsw.bugageocaching.entities; +import org.hibernate.validator.constraints.UniqueElements; import org.springframework.context.annotation.Primary; +import javax.annotation.Generated; import javax.persistence.*; import java.io.Serializable; @@ -15,6 +17,9 @@ public class User_Info implements Serializable { private User user; @Id + @GeneratedValue + private int id; + private int rankingPointsSum; @ManyToOne @@ -24,6 +29,14 @@ public class User_Info implements Serializable { public User_Info() { } + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + public User getUser() { return user; } @@ -47,4 +60,5 @@ public class User_Info implements Serializable { public void setTeam(Team team) { this.team = team; } + } diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java b/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java index 4d63e41..2c04860 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/User_InfoID.java @@ -4,5 +4,5 @@ import java.io.Serializable; public class User_InfoID implements Serializable { private int user; - private int rankingPointsSum; + private int id; } diff --git a/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java b/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java index a96b12d..309cc41 100644 --- a/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java +++ b/src/main/java/hhn/labsw/bugageocaching/repositories/User_InfoRepository.java @@ -1,8 +1,13 @@ package hhn.labsw.bugageocaching.repositories; +import hhn.labsw.bugageocaching.entities.User; import hhn.labsw.bugageocaching.entities.User_Info; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; -public interface User_InfoRepository extends CrudRepository { +import java.util.List; +import java.util.Optional; +public interface User_InfoRepository extends CrudRepository { + User_Info findUser_InfoByUser(User user); } diff --git a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java index 98b7b6b..06fce97 100644 --- a/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java +++ b/src/main/java/hhn/labsw/bugageocaching/util/FinderUtil.java @@ -10,6 +10,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; +import java.util.List; import java.util.Optional; @Component @@ -31,12 +32,10 @@ public class FinderUtil { static User_InfoRepository user_infoRepository; - static TeamInviteRepository teamInviteRepository; - @Autowired public void setCacheRepository(CacheRepository cacheRepository) { - FinderUtil.cacheRepository= cacheRepository; + FinderUtil.cacheRepository = cacheRepository; } @Autowired @@ -74,11 +73,6 @@ public class FinderUtil { FinderUtil.user_infoRepository = user_infoRepository; } - @Autowired - public void setTeamInviteRepository(TeamInviteRepository teamInviteRepository) { - FinderUtil.teamInviteRepository = teamInviteRepository; - } - public static ResponseEntity findCacheById(String cacheID) { @@ -111,6 +105,16 @@ public class FinderUtil { return ResponseEntity.status(404).body("The user has not started this cache yet"); } + public static ResponseEntity findUserByUsername(String username) { + + User user = userRepository.findByUsername(username); + if (user != null) { + return ResponseEntity.status(200).body(user); + } + + return ResponseEntity.status(404).body("Couldnt find user with username " + username); + } + public static ResponseEntity findCacheAccesDefinitionById(String cacheAccesDefinitionID) { Optional cacheAccesDefinitionOptional = cacheAccesDefinitionRepository.findById(Integer.valueOf(cacheAccesDefinitionID)); if (cacheAccesDefinitionOptional.isPresent()) { @@ -120,15 +124,6 @@ public class FinderUtil { } } - public static ResponseEntity findUser_InfoByID(String infoID) { - Optional user_InfoOptional = user_infoRepository.findById(Integer.valueOf(infoID)); - if (user_InfoOptional.isPresent()) { - return ResponseEntity.status(200).body(user_InfoOptional.get()); - } else { - return ResponseEntity.status(404).body("Couldnt find User_Info " + infoID); - } - } - public static ResponseEntity findUserFromClaim(Claims claims) { try { @@ -158,13 +153,4 @@ public class FinderUtil { } } - public static ResponseEntity findTeamInviteByID(String teamInviteID) { - Optional teamInviteOptional = teamInviteRepository.findById(Integer.valueOf(teamInviteID)); - if (teamInviteOptional.isPresent()) { - return ResponseEntity.status(200).body(teamInviteOptional.get()); - } else { - return ResponseEntity.status(404).body("Couldnt find User_Info " + teamInviteID); - } - } - - } +}