package com.healthcare.ohctech.service.impl; import com.healthcare.ohctech.dto.DeviceDto; import com.healthcare.ohctech.entity.DeviceMaster; import com.healthcare.ohctech.repository.DeviceMasterRepo; import com.healthcare.ohctech.service.DeviceMasterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.Optional; @Service public class DeviceMasterServiceImpl implements DeviceMasterService { @Autowired private DeviceMasterRepo deviceMasterRepo; @Override public DeviceMaster getDeviceById(Long id) { Optional deviceMaster = deviceMasterRepo.findById(id); return deviceMaster.orElse(null); } @Override public Page getAllDevices(Pageable pageable) { return deviceMasterRepo.findAll(pageable); } @Override public DeviceMaster addDevice(DeviceDto deviceDto) { DeviceMaster deviceMaster = new DeviceMaster(); deviceMaster.setDeviceName(deviceDto.deviceName()); deviceMaster.setIsActive(deviceDto.isActive()); return deviceMasterRepo.save(deviceMaster); } @Override public DeviceMaster updateDevice(DeviceDto deviceDto) { Optional optionalDevice = deviceMasterRepo.findById(deviceDto.id()); if (optionalDevice.isPresent()) { DeviceMaster deviceMaster = optionalDevice.get(); deviceMaster.setDeviceName(deviceDto.deviceName()); deviceMaster.setIsActive(deviceDto.isActive()); return deviceMasterRepo.save(deviceMaster); } else { throw new RuntimeException("Device not found with ID: " + deviceDto.id()); } } @Override public void deleteDevice(Long id) { Optional optionalDevice = deviceMasterRepo.findById(id); if (optionalDevice.isPresent()) { deviceMasterRepo.delete(optionalDevice.get()); } else { throw new RuntimeException("Device not found with ID: " + id); } } }