Explorar el Código

Add PlantService and PlantController with /api/v1/plants for showing saved plants

Blazej hace 4 años
padre
commit
b8ff8a55a8

+ 25 - 0
src/main/java/com/example/plantsforyou/plant/PlantController.java

@@ -0,0 +1,25 @@
+package com.example.plantsforyou.plant;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+@RestController
+@RequestMapping(path = "api/v1/plants")
+public class PlantController {
+    private final PlantService plantService;
+
+    @Autowired
+    public PlantController(PlantService plantService) {
+        this.plantService = plantService;
+    }
+
+    @GetMapping
+    public List<Plant> getPlants(){
+        return plantService.getPlants();
+    }
+
+}

+ 23 - 0
src/main/java/com/example/plantsforyou/plant/PlantService.java

@@ -0,0 +1,23 @@
+package com.example.plantsforyou.plant;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+
+import java.util.List;
+
+@Service
+public class PlantService {
+    private final PlantRepository plantRepository;
+
+    @Autowired
+    public PlantService(PlantRepository plantRepository) {
+        this.plantRepository = plantRepository;
+    }
+
+    public List<Plant> getPlants(){
+        return plantRepository.findAll();
+    }
+    public void addPlant(Plant plant){
+        plantRepository.save(plant);
+    }
+}