Quellcode durchsuchen

:tada: Add squads endpoint

DricomDragon vor 6 Monaten
Ursprung
Commit
5a16da0351

+ 32 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/SquadController.java

@@ -0,0 +1,32 @@
+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.repos.SquadRepository;
+
+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;
+
+@RestController
+@RequestMapping("/squads")
+public class SquadController {
+
+    final private SquadRepository repo;
+
+    SquadController(final SquadRepository squadRepository) {
+        this.repo = squadRepository;
+    }
+
+    @GetMapping("/")
+    public List<SquadEntity> list() {
+        return repo.findAll();
+    }
+
+    @GetMapping("/{id}")
+    public SquadEntity get(@PathVariable final long id) {
+        return repo.findById(id).orElseThrow(() -> new RuntimeException("Squad not found"));
+    }
+
+}

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

@@ -14,8 +14,6 @@ public class PlayerEntity {
     @Column(name = "name", nullable = false, length = 50)
     private String name;
 
-    // Getters and Setters
-
     public Long getId() {
         return id;
     }

+ 89 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/jpa/SquadEntity.java

@@ -0,0 +1,89 @@
+package eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa;
+
+import jakarta.persistence.*;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "squad")
+public class SquadEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private Long id;
+
+    @ManyToOne
+    @JoinColumn(name = "player_id", insertable = false, updatable = false)
+    private PlayerEntity player;
+
+    @Column(name = "creation_date", nullable = false)
+    private Timestamp creationDate;
+
+    @Column(name = "name", nullable = false, length = 50)
+    private String name;
+
+    @Column(name = "comment")
+    private String comment;
+
+    @Column(name = "mech_1_id")
+    private Integer mech1Id;
+
+    @Column(name = "mech_2_id")
+    private Integer mech2Id;
+
+    @Column(name = "mech_3_id")
+    private Integer mech3Id;
+
+    public Long getId() {
+        return id;
+    }
+
+    public PlayerEntity getPlayer() {
+        return player;
+    }
+
+    public Timestamp getCreationDate() {
+        return creationDate;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public String getComment() {
+        return comment;
+    }
+
+    public void setComment(String comment) {
+        this.comment = comment;
+    }
+
+    public Integer getMech1Id() {
+        return mech1Id;
+    }
+
+    public void setMech1Id(Integer mech1Id) {
+        this.mech1Id = mech1Id;
+    }
+
+    public Integer getMech2Id() {
+        return mech2Id;
+    }
+
+    public void setMech2Id(Integer mech2Id) {
+        this.mech2Id = mech2Id;
+    }
+
+    public Integer getMech3Id() {
+        return mech3Id;
+    }
+
+    public void setMech3Id(Integer mech3Id) {
+        this.mech3Id = mech3Id;
+    }
+
+}

+ 10 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/repos/SquadRepository.java

@@ -0,0 +1,10 @@
+package eu.jovian_hersemeule.dev.squad.squad_composer.data.repos;
+
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.stereotype.Repository;
+
+import eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa.SquadEntity;
+
+@Repository
+public interface SquadRepository extends JpaRepository<SquadEntity, Long> {
+}

+ 1 - 1
dev-back/src/main/resources/db/migration/V1_0_1__Create_squad_table.sql

@@ -1,7 +1,7 @@
 CREATE TABLE squad (
   id SERIAL PRIMARY KEY,
   player_id INTEGER NOT NULL,
-  creation_date TIMESTAMP NOT NULL,
+  creation_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
   name VARCHAR(50) NOT NULL,
   comment TEXT,
   FOREIGN KEY (player_id) REFERENCES player(id),