package com.healthcare.ohctech.service.impl; import com.healthcare.ohctech.dto.HealthAdviceDto; import com.healthcare.ohctech.entity.HealthAdvice; import com.healthcare.ohctech.repository.HealthAdviceRepo; import com.healthcare.ohctech.service.HealthAdviceService; 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 HealthAdviceServiceImpl implements HealthAdviceService { @Autowired private final HealthAdviceRepo healthAdviceRepo; public HealthAdviceServiceImpl(HealthAdviceRepo healthAdviceRepo) { this.healthAdviceRepo = healthAdviceRepo; } @Override public HealthAdvice getHealthAdviceById(Long id) { Optional healthAdvice = healthAdviceRepo.findById(id); return healthAdvice.orElse(null); } @Override public Page getAllHealthAdvices(Pageable pageable) { return healthAdviceRepo.findAll(pageable); } @Override public void addHealthAdvice(HealthAdviceDto healthAdviceDto) { HealthAdvice healthAdvice = new HealthAdvice(); healthAdvice.setHealthAdviceName(healthAdviceDto.healthAdviceName()); healthAdvice.setHealthAdviceCat(healthAdviceDto.healthAdviceCat()); healthAdviceRepo.save(healthAdvice); } @Override public HealthAdvice updateHealthAdvice(HealthAdviceDto healthAdviceDto) { Optional optionalHealthAdvice = healthAdviceRepo.findById(healthAdviceDto.id()); if (optionalHealthAdvice.isPresent()) { HealthAdvice healthAdvice = optionalHealthAdvice.get(); healthAdvice.setHealthAdviceName(healthAdviceDto.healthAdviceName()); healthAdvice.setHealthAdviceCat(healthAdviceDto.healthAdviceCat()); return healthAdviceRepo.save(healthAdvice); } else { throw new RuntimeException("HealthAdvice not found with ID: " + healthAdviceDto.id()); } } @Override public void deleteHealthAdvice(Long id) { Optional optionalHealthAdvice = healthAdviceRepo.findById(id); if (optionalHealthAdvice.isPresent()) { healthAdviceRepo.delete(optionalHealthAdvice.get()); } else { throw new RuntimeException("HealthAdvice not found with ID: " + id); } } }