PlantService.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.example.plantsforyou.plant;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. import java.util.List;
  5. import java.util.Optional;
  6. @Service
  7. public class PlantService {
  8. private final PlantRepository plantRepository;
  9. @Autowired
  10. public PlantService(PlantRepository plantRepository) {
  11. this.plantRepository = plantRepository;
  12. }
  13. public List<Plant> getAllPlants(){
  14. return plantRepository.findAll();
  15. }
  16. public Optional<Plant> findPlantById(Long plantID){ return plantRepository.findById(plantID); }
  17. public void addPlant(Plant plant){
  18. plantRepository.save(plant);
  19. }
  20. public void delete(Long plantID) {
  21. plantRepository.deleteById(plantID);
  22. }
  23. public void updatePrice(Long plantID, Double price){
  24. Optional<Plant> plant = plantRepository.findById(plantID);
  25. if(plant.isPresent()){
  26. Plant toUpdate = plant.get();
  27. toUpdate.setPrice(price);
  28. plantRepository.save(toUpdate);
  29. }
  30. }
  31. public void updateInStock(Long plantID, boolean inStock){
  32. Optional<Plant> plant = plantRepository.findById(plantID);
  33. if(plant.isPresent()){
  34. Plant toUpdate = plant.get();
  35. toUpdate.setInStock(inStock);
  36. plantRepository.save(toUpdate);
  37. }
  38. }
  39. }