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

:wrench: More data retrieval option for mechs

DricomDragon преди 7 месеца
родител
ревизия
1ac72f450e

+ 15 - 1
squad-composer/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/controllers/MechController.java

@@ -1,15 +1,18 @@
 package eu.jovian_hersemeule.dev.squad.squad_composer.controllers;
 
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.client.RestClientException;
 
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.MechRowMapper;
 import eu.jovian_hersemeule.dev.squad.squad_composer.data.model.Mech;
+import jakarta.websocket.server.PathParam;
 
 import java.util.List;
 
 import javax.sql.DataSource;
 
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.jdbc.core.JdbcTemplate;
 
@@ -19,13 +22,24 @@ public class MechController {
 
     private final JdbcTemplate jdbcTemplate;
 
+    private final MechRowMapper mapper = new MechRowMapper();
+
     MechController(final DataSource ds) {
         jdbcTemplate = new JdbcTemplate(ds);
     }
 
     @GetMapping("/")
     public List<Mech> list() {
-        return jdbcTemplate.query("SELECT * FROM MECH", new MechRowMapper());
+        return jdbcTemplate.query("SELECT * FROM MECH", mapper);
+    }
+
+    @GetMapping("/{id}")
+    public Mech get(@PathVariable final int id) {
+        final List<Mech> ans = jdbcTemplate.query("SELECT * FROM mech WHERE id = ?", mapper, id);
+        if (ans.isEmpty()) {
+            return null;
+        }
+        return ans.get(0);
     }
     
 }

+ 5 - 3
squad-composer/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/MechRowMapper.java

@@ -14,9 +14,11 @@ public class MechRowMapper implements RowMapper<Mech> {
     @Override
     @Nullable
     public Mech mapRow(final @NonNull ResultSet rs, int rowNum) throws SQLException {
-        final Mech obj = new Mech();
-        obj.setName(rs.getString("name"));
-        return obj;
+        return new Mech(
+            rs.getInt("id"),
+            rs.getString("name"),
+            rs.getString("class"),
+            rs.getInt("resistance"));
     }
 
 }

+ 3 - 11
squad-composer/src/main/java/eu/jovian_hersemeule/dev/squad/squad_composer/data/model/Mech.java

@@ -1,13 +1,5 @@
 package eu.jovian_hersemeule.dev.squad.squad_composer.data.model;
 
-public class Mech {
-    String name;
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-}
+public record Mech(int id, String name, String kind, Integer health) {
+    // Nothing
+}