ohctechv3/.svn/pristine/36/3698b85b77b6b55a3b733c1edcdd2f59fa091b39.svn-base

62 lines
2.1 KiB
Plaintext
Raw Normal View History

2024-10-28 15:03:36 +05:30
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> deviceMaster = deviceMasterRepo.findById(id);
return deviceMaster.orElse(null);
}
@Override
public Page<DeviceMaster> 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<DeviceMaster> 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<DeviceMaster> optionalDevice = deviceMasterRepo.findById(id);
if (optionalDevice.isPresent()) {
deviceMasterRepo.delete(optionalDevice.get());
} else {
throw new RuntimeException("Device not found with ID: " + id);
}
}
}