Browse Source

:beetle: Fix infinite recursion in Rest API

Player -> Squad -> Player -> Squad -> Player
DricomDragon 6 months ago
parent
commit
d1283fb004

+ 4 - 2
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/PlayerController.java

@@ -2,9 +2,11 @@ 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.repos.PlayerRepository;
 
 import java.util.List;
+
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
@@ -20,8 +22,8 @@ public class PlayerController {
     }
 
     @GetMapping("/")
-    public List<PlayerEntity> list() {
-        return repo.findAll();
+    public List<PlayerItem> list() {
+        return repo.findAll().stream().map(PlayerItem::project).toList();
     }
 
     @GetMapping("/{id}")

+ 3 - 2
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/SquadController.java

@@ -2,6 +2,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.SquadItem;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.repos.SquadRepository;
 
 import java.util.List;
@@ -20,8 +21,8 @@ public class SquadController {
     }
 
     @GetMapping("/")
-    public List<SquadEntity> list() {
-        return repo.findAll();
+    public List<SquadItem> list() {
+        return repo.findAll().stream().map(SquadItem::project).toList();
     }
 
     @GetMapping("/{id}")

+ 10 - 4
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/jpa/PlayerEntity.java

@@ -1,5 +1,7 @@
 package eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa;
 
+import java.util.List;
+
 import jakarta.persistence.*;
 
 @Entity
@@ -14,14 +16,13 @@ public class PlayerEntity {
     @Column(name = "name", nullable = false, length = 50)
     private String name;
 
+    @OneToMany(mappedBy = "player")
+    private List<SquadEntity> squads;
+
     public Long getId() {
         return id;
     }
 
-    public void setId(Long id) {
-        this.id = id;
-    }
-
     public String getName() {
         return name;
     }
@@ -29,4 +30,9 @@ public class PlayerEntity {
     public void setName(String name) {
         this.name = name;
     }
+
+    public List<SquadEntity> getSquads() {
+        return squads;
+    }
+
 }

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

@@ -0,0 +1,29 @@
+package eu.jovian_hersemeule.dev.squad.squad_composer.data.projections;
+
+import eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa.PlayerEntity;
+
+/**
+ * Projection qui ne retourne pas beaucoup d'infos.
+ */
+public class PlayerItem {
+
+    private final long id;
+    private final String name;
+
+    public static PlayerItem project(final PlayerEntity entity) {
+        return new PlayerItem(entity);
+    }
+
+    private PlayerItem(final PlayerEntity entity) {
+        this.id = entity.getId();
+        this.name = entity.getName();
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public String getName() {
+        return name;
+    }
+}

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

@@ -0,0 +1,64 @@
+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 SquadItem {
+
+    private Long id;
+
+    private Long playerId;
+
+    private Instant creationDate;
+
+    private String name;
+
+    private Integer mech1Id;
+
+    private Integer mech2Id;
+
+    private Integer mech3Id;
+
+    public static SquadItem project(final SquadEntity entity) {
+        return new SquadItem(entity);
+    }
+
+    private SquadItem(final SquadEntity entity) {
+        this.id = entity.getId();
+        this.name = entity.getName();
+        this.playerId = entity.getPlayer().getId();
+        this.creationDate = entity.getCreationDate().toInstant();
+        this.mech1Id = entity.getMech1Id();
+        this.mech2Id = entity.getMech2Id();
+        this.mech3Id = entity.getMech3Id();
+    }
+
+    public Long getId() {
+        return id;
+    }
+
+    public Long getPlayerId() {
+        return playerId;
+    }
+
+    public Instant getCreationDate() {
+        return creationDate;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public Integer getMech1Id() {
+        return mech1Id;
+    }
+
+    public Integer getMech2Id() {
+        return mech2Id;
+    }
+
+    public Integer getMech3Id() {
+        return mech3Id;
+    }
+}