浏览代码

:tada: Import spring data and jpa to create /players endpoint

DricomDragon 6 月之前
父节点
当前提交
35a0d6d0fe

+ 4 - 0
dev-back/pom.xml

@@ -39,6 +39,10 @@
             <artifactId>spring-jdbc</artifactId>
         </dependency>
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+        </dependency>
+        <dependency>
             <groupId>com.zaxxer</groupId>
             <artifactId>HikariCP</artifactId>
         </dependency>

+ 32 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/PlayerController.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.PlayerEntity;
+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;
+
+@RestController
+@RequestMapping("/players")
+public class PlayerController {
+
+    final private PlayerRepository repo;
+
+    PlayerController(final PlayerRepository playerRepository) {
+        this.repo = playerRepository;
+    }
+
+    @GetMapping("/")
+    public List<PlayerEntity> list() {
+        return repo.findAll();
+    }
+
+    @GetMapping("/{id}")
+    public PlayerEntity get(@PathVariable final long id) {
+        return repo.findById(id).orElseThrow(() -> new RuntimeException("Player not found"));
+    }
+    
+}

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

@@ -0,0 +1,34 @@
+package eu.jovian_hersemeule.dev.squad.squad_composer.data.jpa;
+
+import jakarta.persistence.*;
+
+@Entity
+@Table(name = "player")
+public class PlayerEntity {
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id")
+    private Long id;
+
+    @Column(name = "name", nullable = false, length = 50)
+    private String name;
+
+    // Getters and Setters
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+}

+ 10 - 0
dev-back/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/repos/PlayerRepository.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.PlayerEntity;
+
+@Repository
+public interface PlayerRepository extends JpaRepository<PlayerEntity, Long> {
+}