58 lines
1.7 KiB
Plaintext
58 lines
1.7 KiB
Plaintext
package com.healthcare.ohctech.service.impl;
|
|
|
|
import com.healthcare.ohctech.dto.PlantDto;
|
|
import com.healthcare.ohctech.entity.Plant;
|
|
import com.healthcare.ohctech.repository.PlantRepo;
|
|
import com.healthcare.ohctech.service.PlantService;
|
|
import org.springframework.data.domain.Page;
|
|
import org.springframework.data.domain.Pageable;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Optional;
|
|
|
|
@Service
|
|
public class PlantServiceImpl implements PlantService {
|
|
private final PlantRepo plantRepo;
|
|
|
|
public PlantServiceImpl(PlantRepo plantRepo) {
|
|
this.plantRepo = plantRepo;
|
|
}
|
|
|
|
public Plant getPlantById(Long plantId) {
|
|
Optional<Plant> plant = plantRepo.findById(plantId);
|
|
return plant.orElse(null);
|
|
}
|
|
|
|
public Page<Plant> getAllPlants(Pageable pageable) {
|
|
return plantRepo.findAll(pageable);
|
|
}
|
|
|
|
public void addPlant(PlantDto plantDto) {
|
|
Plant plant = new Plant();
|
|
plant.setPlantName(plantDto.plantName());
|
|
|
|
plantRepo.save(plant);
|
|
}
|
|
|
|
public Plant updatePlant(PlantDto plantDto) {
|
|
Optional<Plant> optionalPlant = plantRepo.findById(plantDto.id());
|
|
if (optionalPlant.isPresent()) {
|
|
Plant plant = optionalPlant.get();
|
|
plant.setPlantName(plantDto.plantName());
|
|
|
|
return plantRepo.save(plant);
|
|
} else {
|
|
throw new RuntimeException("Plant not found with ID: " + plantDto.id());
|
|
}
|
|
}
|
|
|
|
public void deletePlant(Long plantId) {
|
|
Optional<Plant> optionalPlant = plantRepo.findById(plantId);
|
|
if (optionalPlant.isPresent()) {
|
|
plantRepo.delete(optionalPlant.get());
|
|
} else {
|
|
throw new RuntimeException("Plant not found with ID: " + plantId);
|
|
}
|
|
}
|
|
}
|