Kaynağa Gözat

:beetle: Fix infinite recursion in details Rest API

When you specify id un url.
DricomDragon 2 ay önce
ebeveyn
işleme
888a04dcb7

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

@@ -23,12 +23,16 @@ public class PlayerController {
 
     @GetMapping("/")
     public List<PlayerItem> list() {
-        return repo.findAll().stream().map(PlayerItem::project).toList();
+        return repo.findAll().stream()
+                .map(PlayerItem::project)
+                .toList();
     }
 
     @GetMapping("/{id}")
-    public PlayerEntity get(@PathVariable final long id) {
-        return repo.findById(id).orElseThrow(() -> new RuntimeException("Player not found"));
+    public PlayerItem get(@PathVariable final long id) {
+        return repo.findById(id)
+                .map(PlayerItem::project)
+                .orElseThrow(() -> new RuntimeException("Player not found"));
     }
     
 }

+ 8 - 4
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/SquadController.java

@@ -1,7 +1,7 @@
 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.SquadEntity;
+import eu.jovian_hersemeule.dev.squad.squad_composer.data.projections.SquadDetails;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.projections.SquadItem;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.repos.SquadRepository;
 
@@ -22,12 +22,16 @@ public class SquadController {
 
     @GetMapping("/")
     public List<SquadItem> list() {
-        return repo.findAll().stream().map(SquadItem::project).toList();
+        return repo.findAll().stream()
+                .map(SquadItem::project)
+                .toList();
     }
 
     @GetMapping("/{id}")
-    public SquadEntity get(@PathVariable final long id) {
-        return repo.findById(id).orElseThrow(() -> new RuntimeException("Squad not found"));
+    public SquadDetails get(@PathVariable final long id) {
+        return repo.findById(id)
+                .map(SquadDetails::project)
+                .orElseThrow(() -> new RuntimeException("Squad not found"));
     }
 
 }

+ 71 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/projections/SquadDetails.java

@@ -0,0 +1,71 @@
+package eu.jovian_hersemeule.dev.squad.squad_composer.data.projections;
+
+import java.time.Instant;
+
+import eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa.SquadEntity;
+
+public class SquadDetails {
+
+    private Long id;
+
+    private PlayerItem player;
+
+    private Instant creationDate;
+
+    private String name;
+
+    private String comment;
+
+    private Integer mech1Id;
+
+    private Integer mech2Id;
+
+    private Integer mech3Id;
+
+    public static SquadDetails project(final SquadEntity entity) {
+        return new SquadDetails(entity);
+    }
+
+    private SquadDetails(final SquadEntity squadEntity) {
+        this.id = squadEntity.getId();
+        this.name = squadEntity.getName();
+        this.comment = squadEntity.getComment();
+        this.player = PlayerItem.project(squadEntity.getPlayer());
+        this.creationDate = squadEntity.getCreationDate().toInstant();
+        this.mech1Id = squadEntity.getMech1Id();
+        this.mech2Id = squadEntity.getMech2Id();
+        this.mech3Id = squadEntity.getMech3Id();
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public PlayerItem getPlayer() {
+        return player;
+    }
+
+    public Instant getCreationDate() {
+        return creationDate;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public Integer getMech1Id() {
+        return mech1Id;
+    }
+
+    public Integer getMech2Id() {
+        return mech2Id;
+    }
+
+    public Integer getMech3Id() {
+        return mech3Id;
+    }
+}