Преглед на файлове

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

DricomDragon преди 2 месеца
родител
ревизия
a8dc56967c
променени са 1 файла, в които са добавени 11 реда и са изтрити 3 реда
  1. 11 3
      dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/PlayerController.java

+ 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();
+    }
     
 }