Explorar o código

:tada: Add an endpoint to see squads of a player

DricomDragon hai 2 meses
pai
achega
a8dc56967c

+ 11 - 3
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/PlayerController.java

@@ -1,8 +1,8 @@
 package eu.jovian_hersemeule.dev.squad.squad_composer.controllers;
 
 import org.springframework.web.bind.annotation.RestController;
-import eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa.PlayerEntity;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.projections.PlayerItem;
+import eu.jovian_hersemeule.dev.squad.squad_composer.data.projections.SquadItem;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.repos.PlayerRepository;
 
 import java.util.List;
@@ -22,17 +22,25 @@ public class PlayerController {
     }
 
     @GetMapping("/")
-    public List<PlayerItem> list() {
+    public List<PlayerItem> listPlayers() {
         return repo.findAll().stream()
                 .map(PlayerItem::project)
                 .toList();
     }
 
     @GetMapping("/{id}")
-    public PlayerItem get(@PathVariable final long id) {
+    public PlayerItem getASpecificPlayer(@PathVariable final long id) {
         return repo.findById(id)
                 .map(PlayerItem::project)
                 .orElseThrow(() -> new RuntimeException("Player not found"));
     }
+
+    @GetMapping("/{id}/squads")
+    public List<SquadItem> getSquadsOfPlayer(@PathVariable final long id) {
+        return repo.findById(id).stream()
+                .flatMap(player -> player.getSquads().stream())
+                .map(SquadItem::project)
+                .toList();
+    }
     
 }