Prechádzať zdrojové kódy

Create management endpoint for plants

Blazej 4 rokov pred
rodič
commit
5f56a18764

+ 11 - 4
src/main/java/com/example/plantsforyou/plant/Plant.java

@@ -6,7 +6,6 @@ import com.example.plantsforyou.plant.enums.PlantTypeOfLight;
 import lombok.Getter;
 import lombok.Getter;
 import lombok.NoArgsConstructor;
 import lombok.NoArgsConstructor;
 import lombok.Setter;
 import lombok.Setter;
-import org.springframework.context.annotation.Description;
 
 
 import javax.persistence.*;
 import javax.persistence.*;
 
 
@@ -29,20 +28,28 @@ public class Plant {
     private PlantCareDifficulty difficulty;
     private PlantCareDifficulty difficulty;
     @Enumerated(EnumType.STRING)
     @Enumerated(EnumType.STRING)
     private PlantSize size;
     private PlantSize size;
-    boolean in_stock;
+    boolean inStock;
 
 
     public Plant(String name,
     public Plant(String name,
                  double price,
                  double price,
                  String description,
                  String description,
                  PlantTypeOfLight typeOfLight,
                  PlantTypeOfLight typeOfLight,
                  PlantCareDifficulty difficulty,
                  PlantCareDifficulty difficulty,
-                 PlantSize size, boolean in_stock) {
+                 PlantSize size, boolean inStock) {
         this.name = name;
         this.name = name;
         this.price = price;
         this.price = price;
         this.description = description;
         this.description = description;
         this.typeOfLight = typeOfLight;
         this.typeOfLight = typeOfLight;
         this.difficulty = difficulty;
         this.difficulty = difficulty;
         this.size = size;
         this.size = size;
-        this.in_stock = in_stock;
+        this.inStock = inStock;
+    }
+
+    public void setPrice(double price) {
+        this.price = price;
+    }
+
+    public void setInStock(boolean inStock) {
+        this.inStock = inStock;
     }
     }
 }
 }

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

@@ -19,7 +19,7 @@ public class PlantController {
 
 
     @GetMapping
     @GetMapping
     public List<Plant> getPlants(){
     public List<Plant> getPlants(){
-        return plantService.getPlants();
+        return plantService.getAllPlants();
     }
     }
 
 
 }
 }

+ 48 - 0
src/main/java/com/example/plantsforyou/plant/PlantManagementController.java

@@ -0,0 +1,48 @@
+package com.example.plantsforyou.plant;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.List;
+
+@RestController
+@RequestMapping(path = "management/api/v1/plants")
+public class PlantManagementController {
+    private final PlantService plantService;
+
+    @Autowired
+    public PlantManagementController(PlantService plantService) {
+        this.plantService = plantService;
+    }
+
+    @GetMapping
+    public List<Plant> getAllPlants(){
+        return plantService.getAllPlants();
+    }
+
+    @PostMapping
+    public ResponseEntity addNewPlant(@RequestBody Plant plant){
+        if(plantService.getAllPlants().contains(plant))
+            return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE);
+        plantService.addPlant(plant);
+        return new ResponseEntity(HttpStatus.OK);
+    }
+
+    @DeleteMapping(path = "{plantID}")
+    public void deletePlant(@PathVariable("plantID") Long plantID){
+        plantService.delete(plantID);
+    }
+
+    //Nie jestem pewien czy to dobry sposób na przekazanie wartości do update (RequestBody)
+    @PutMapping(path = "{plantID}/inStock")
+    public void updatePlantInStock(@PathVariable("plantID") Long plantID, @RequestBody boolean inStock){
+        plantService.updateInStock(plantID, inStock);
+    }
+
+    @PutMapping(path = "{plantID}/price")
+    public void updatePlantPrice(@PathVariable("plantID") Long plantID, @RequestBody Double price){
+        plantService.updatePrice(plantID, price);
+    }
+}

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

@@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.util.List;
 import java.util.List;
+import java.util.Optional;
 
 
 @Service
 @Service
 public class PlantService {
 public class PlantService {
@@ -14,10 +15,31 @@ public class PlantService {
         this.plantRepository = plantRepository;
         this.plantRepository = plantRepository;
     }
     }
 
 
-    public List<Plant> getPlants(){
+    public List<Plant> getAllPlants(){
         return plantRepository.findAll();
         return plantRepository.findAll();
     }
     }
     public void addPlant(Plant plant){
     public void addPlant(Plant plant){
         plantRepository.save(plant);
         plantRepository.save(plant);
     }
     }
+
+    public void delete(Long plantID) {
+        plantRepository.deleteById(plantID);
+    }
+    public void updatePrice(Long plantID, Double price){
+        Optional<Plant> plant = plantRepository.findById(plantID);
+        if(plant.isPresent()){
+            Plant toUpdate = plant.get();
+            toUpdate.setPrice(price);
+            plantRepository.save(toUpdate);
+        }
+    }
+    public void updateInStock(Long plantID, boolean inStock){
+        Optional<Plant> plant = plantRepository.findById(plantID);
+        if(plant.isPresent()){
+            Plant toUpdate = plant.get();
+            toUpdate.setInStock(inStock);
+            plantRepository.save(toUpdate);
+        }
+    }
+
 }
 }