ohctechv3/.svn/pristine/27/2748c3a205b0f3a8be7fede3ae7b06e288e14827.svn-base

68 lines
2.4 KiB
Plaintext
Raw Permalink Normal View History

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