|
16 | 16 |
|
17 | 17 | import com.grouptwelve.grouptwelveBE.model.FavoriteTeam; |
18 | 18 | import com.grouptwelve.grouptwelveBE.model.Game; |
| 19 | +import com.grouptwelve.grouptwelveBE.model.Player; |
19 | 20 | import com.grouptwelve.grouptwelveBE.model.User; |
20 | 21 | import com.grouptwelve.grouptwelveBE.repository.FavoriteTeamRepository; |
21 | 22 | import com.grouptwelve.grouptwelveBE.repository.GameRepository; |
| 23 | +import com.grouptwelve.grouptwelveBE.repository.PlayerRepository; |
22 | 24 | import com.grouptwelve.grouptwelveBE.repository.UserRepository; |
23 | 25 |
|
24 | 26 |
|
|
29 | 31 | public class Controller { |
30 | 32 | @Autowired |
31 | 33 | private UserRepository userRepository; |
| 34 | + |
32 | 35 | @Autowired |
33 | 36 | private FavoriteTeamRepository favoriteTeamRepository; |
34 | 37 |
|
35 | 38 | @Autowired |
36 | 39 | private GameRepository gameRepository; |
37 | 40 |
|
| 41 | + @Autowired |
| 42 | + private PlayerRepository playerRepository; |
| 43 | + |
38 | 44 |
|
39 | 45 | @GetMapping("/") |
40 | 46 | public String root() { |
@@ -111,84 +117,84 @@ public List<Game> listGames(@RequestParam(required = false) String status) { |
111 | 117 | } |
112 | 118 |
|
113 | 119 | // GET (2): one by id |
114 | | - @GetMapping("/games/{id}") |
115 | | - public Game getGame(@PathVariable("id") Long id) { |
116 | | - return gameRepository.findById(id).orElse(null); |
117 | | - } |
118 | | - |
119 | | - // PUT (1): update core fields (like the game info - status or teams) |
120 | | - @PutMapping("/games/{id}") |
121 | | - public Game updateGame(@PathVariable("id") Long id, @RequestBody Game u) { |
122 | | - return gameRepository.findById(id).map(g -> { |
123 | | - if (u.getLeague() != null) g.setLeague(u.getLeague()); |
124 | | - if (u.getHomeTeam() != null) g.setHomeTeam(u.getHomeTeam()); |
125 | | - if (u.getAwayTeam() != null) g.setAwayTeam(u.getAwayTeam()); |
126 | | - if (u.getStartTime() != null) g.setStartTime(u.getStartTime()); |
127 | | - if (u.getStatus() != null) g.setStatus(u.getStatus()); |
128 | | - if (u.getOddsHome() != null) g.setOddsHome(u.getOddsHome()); |
129 | | - if (u.getOddsAway() != null) g.setOddsAway(u.getOddsAway()); |
130 | | - return gameRepository.save(g); |
131 | | - }).orElse(null); |
132 | | - } |
133 | | - |
134 | | - // PUT (2): update odds only |
135 | | - @PutMapping("/games/{id}/odds") |
136 | | - public Game updateOdds(@PathVariable("id") Long id, @RequestBody Game u) { |
137 | | - return gameRepository.findById(id).map(g -> { |
138 | | - if (u.getOddsHome() != null) g.setOddsHome(u.getOddsHome()); |
139 | | - if (u.getOddsAway() != null) g.setOddsAway(u.getOddsAway()); |
140 | | - return gameRepository.save(g); |
141 | | - }).orElse(null); |
142 | | - } |
143 | | - |
144 | | - // DELETE (1): delete one |
145 | | - @DeleteMapping("/games/{id}") |
146 | | - public void deleteGame(@PathVariable("id") Long id) { |
147 | | - gameRepository.deleteById(id); |
148 | | - } |
149 | | - |
150 | | - // DELETE (2): bulk delete by status |
151 | | - @DeleteMapping("/games") |
152 | | - public long deleteGamesByStatus(@RequestParam String status) { |
153 | | - return gameRepository.deleteByStatus(status); |
154 | | - } |
155 | 120 |
|
156 | | - |
157 | | - // end of games table |
158 | | - |
159 | | - |
160 | | - @DeleteMapping("/users/{id}") // DELETE 1 |
161 | | - public String deleteUser(@PathVariable("id") Long id) { |
162 | | - if (userRepository.existsById(id)) { |
163 | | - userRepository.deleteById(id); |
164 | | - return "User with id " + id + " deleted."; |
165 | | - } else { |
166 | | - return "User with id " + id + " not found."; |
167 | | - } |
| 121 | + // Player (8 routes) |
| 122 | + // GET /players - lists all players (optional filters: team, position) |
| 123 | + @GetMapping("/players") |
| 124 | + public List<Player> listPlayers(@RequestParam(required = false) String team, |
| 125 | + @RequestParam(required = false) String position) { |
| 126 | + boolean noTeam = (team == null || team.isBlank()); |
| 127 | + boolean noPos = (position == null || position.isBlank()); |
| 128 | + if (noTeam && noPos) return playerRepository.findAll(); |
| 129 | + if (!noTeam && noPos) return playerRepository.findByTeam(team); |
| 130 | + if (noTeam && !noPos) return playerRepository.findByPosition(position); |
| 131 | + // both provided: filter team result by position |
| 132 | + List<Player> byTeam = playerRepository.findByTeam(team); |
| 133 | + return byTeam.stream().filter(p -> p.getPosition() != null && position.equalsIgnoreCase(p.getPosition())).toList(); |
| 134 | + } |
| 135 | + |
| 136 | + // GET /players/{id} - get player by id |
| 137 | + @GetMapping("/players/{id}") |
| 138 | + public Player getPlayer(@PathVariable("id") Long id) { |
| 139 | + return playerRepository.findById(id).orElse(null); |
| 140 | + } |
| 141 | + |
| 142 | + // GET /players/position/{position} - list players by position |
| 143 | + @GetMapping("/players/position/{position}") |
| 144 | + public List<Player> listPlayersByPosition(@PathVariable("position") String position) { |
| 145 | + return playerRepository.findByPosition(position); |
| 146 | + } |
| 147 | + |
| 148 | + // GET /players/team/{team} - list players by team |
| 149 | + @GetMapping("/players/team/{team}") |
| 150 | + public List<Player> listPlayersByTeam(@PathVariable("team") String team) { |
| 151 | + return playerRepository.findByTeam(team); |
| 152 | + } |
| 153 | + |
| 154 | + // GET /players/search?q=... - search by name or team (case-insensitive contains) |
| 155 | + @GetMapping("/players/search") |
| 156 | + public List<Player> searchPlayers(@RequestParam("q") String q) { |
| 157 | + if (q == null || q.isBlank()) return List.of(); |
| 158 | + String qLower = q.toLowerCase(); |
| 159 | + return playerRepository.findAll().stream() |
| 160 | + .filter(p -> (p.getName() != null && p.getName().toLowerCase().contains(qLower)) |
| 161 | + || (p.getTeam() != null && p.getTeam().toLowerCase().contains(qLower))) |
| 162 | + .toList(); |
| 163 | + } |
| 164 | + |
| 165 | + // POST /players - create new player |
| 166 | + @PostMapping("/players") |
| 167 | + public Player createPlayer(@RequestBody Player p) { |
| 168 | + return playerRepository.save(p); |
| 169 | + } |
| 170 | + |
| 171 | + // PUT /players/{id} - update full player record (partial-update semantics) |
| 172 | + @PutMapping("/players/{id}") |
| 173 | + public Player updatePlayer(@PathVariable("id") Long id, @RequestBody Player u) { |
| 174 | + return playerRepository.findById(id).map(existing -> { |
| 175 | + if (u.getName() != null) existing.setName(u.getName()); |
| 176 | + if (u.getTeam() != null) existing.setTeam(u.getTeam()); |
| 177 | + if (u.getPosition() != null) existing.setPosition(u.getPosition()); |
| 178 | + if (u.getJerseyNumber() != 0) existing.setJerseyNumber(u.getJerseyNumber()); |
| 179 | + if (u.getTouchdowns() != 0) existing.setTouchdowns(u.getTouchdowns()); |
| 180 | + if (u.getTotalYards() != 0) existing.setTotalYards(u.getTotalYards()); |
| 181 | + if (u.getGamesPlayed() != 0) existing.setGamesPlayed(u.getGamesPlayed()); |
| 182 | + if (u.getFieldGoals() != 0) existing.setFieldGoals(u.getFieldGoals()); |
| 183 | + if (u.getCompletionRate() != 0.0) existing.setCompletionRate(u.getCompletionRate()); |
| 184 | + return playerRepository.save(existing); |
| 185 | + }).orElse(null); |
168 | 186 | } |
169 | 187 |
|
170 | | - @DeleteMapping("/users/email/{email}") // DELETE 2 |
171 | | - public String deleteUserByEmail(@PathVariable("email") String email) { |
172 | | - if (userRepository.existsByEmail(email)) { |
173 | | - userRepository.deleteByEmail(email); |
174 | | - return "User with email " + email + " deleted."; |
| 188 | + // DELETE /players/{id} - delete player by ID |
| 189 | + @DeleteMapping("/players/{id}") |
| 190 | + public String deletePlayer(@PathVariable("id") Long id) { |
| 191 | + if (playerRepository.existsById(id)) { |
| 192 | + playerRepository.deleteById(id); |
| 193 | + return "Player with id " + id + " deleted."; |
175 | 194 | } else { |
176 | | - return "User with email " + email + " not found."; |
| 195 | + return "Player with id " + id + " not found."; |
177 | 196 | } |
178 | 197 | } |
179 | | - // User CRUD operations END |
180 | | - |
181 | | - // FavoriteTeam CRUD operations BEGIN |
182 | | - @GetMapping("/favoriteteams") // GET 1 |
183 | | - public List<FavoriteTeam> getAllFavoriteTeams() { |
184 | | - return favoriteTeamRepository.findAll(); |
185 | | - } |
186 | | - |
187 | | - @GetMapping("/favoriteteams/user/{userId}") // GET 2 |
188 | | - public List<FavoriteTeam> getFavoriteTeamsByUserId(@PathVariable("userId") Long userId) { |
189 | | - return favoriteTeamRepository.findByUserId(userId); |
190 | | - } |
191 | | - |
192 | 198 | @GetMapping("/favoriteteams/user/{userId}/team/{teamId}") // GET 3 |
193 | 199 | public FavoriteTeam getFavoriteTeamByUserIdAndTeamId(@PathVariable("userId") Long userId, @PathVariable("teamId") Long teamId) { |
194 | 200 | return favoriteTeamRepository.findByUserIdAndTeamId(userId, teamId).orElse(null); |
@@ -234,4 +240,5 @@ public String deleteFavoriteTeamById(@PathVariable("id") Long id) { |
234 | 240 | } |
235 | 241 | } |
236 | 242 | // FavoriteTeam CRUD operations END |
| 243 | + |
237 | 244 | } |
0 commit comments