From a8b0b1bbb4ba621f081fd0123b811295202e5025 Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 25 Apr 2019 12:40:56 +0200 Subject: [PATCH] added createTeam --- .../bugageocaching/controller/Controller.java | 51 ++++++++++++++++++- .../labsw/bugageocaching/entities/Team.java | 9 ---- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index ac8fd0e..076e4e4 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -48,7 +48,6 @@ public class Controller { @Autowired User_InfoRepository user_infoRepository; - @PostConstruct public void init() { fetchPublicKey(); @@ -424,6 +423,56 @@ public class Controller { } } + @CrossOrigin(origins = "http://localhost:8081") // only for dev purpose + @RequestMapping("/api/createTeam") + @ResponseBody + public ResponseEntity createTeam(@RequestParam String token, + @RequestParam String name) { + ResponseEntity verifyToken = VerificationUtil.verifyToken(token); + + if (verifyToken.getStatusCodeValue() != 200) { + return verifyToken; + } + + Claims claims = (Claims) verifyToken.getBody(); + + ResponseEntity getUser = FinderUtil.findUserFromClaim(claims); + + if (getUser.getStatusCodeValue() != 200) { + return getUser; + } + + 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(); + //---------------------- + if (user_info.getTeam() != null) { + return ResponseEntity.status(400).body("You already have a team"); + } + + for (Team team : teamRepository.findAll()) { + if (team.getName().equals(name)) { + return ResponseEntity.status(400).body("The teamname is already taken"); + } + } + + Team team = new Team(); + team.setName(name); + teamRepository.save(team); + + user_info.setTeam(team); + user_infoRepository.save(user_info); + + return ResponseEntity.status(200).body(new Gson().toJson(team)); + } + @RequestMapping("/api/hello") public ResponseEntity hello(@RequestParam String name) { return ResponseEntity.status(200).body(userRepository.getRankingPlaceFromUser(name)); diff --git a/src/main/java/hhn/labsw/bugageocaching/entities/Team.java b/src/main/java/hhn/labsw/bugageocaching/entities/Team.java index d4dd2c2..935929f 100644 --- a/src/main/java/hhn/labsw/bugageocaching/entities/Team.java +++ b/src/main/java/hhn/labsw/bugageocaching/entities/Team.java @@ -13,7 +13,6 @@ public class Team { private int id; private String name; - private int rankingPoints; public int getId() { return id; @@ -30,12 +29,4 @@ public class Team { public void setName(String name) { this.name = name; } - - public int getRankingPoints() { - return rankingPoints; - } - - public void setRankingPoints(int rankingPoints) { - this.rankingPoints = rankingPoints; - } }