ohctechv3/.svn/pristine/a4/a4c85315a7749fc9539668c5d891dc4e12580ac3.svn-base

94 lines
3.7 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.AbnormalityDto;
import com.healthcare.ohctech.entity.Abnormality;
import com.healthcare.ohctech.repository.AbnormalityRepo;
import com.healthcare.ohctech.repository.TrainingMasterRepo;
import com.healthcare.ohctech.service.AbnormalityService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class AbnormalityServiceImpl implements AbnormalityService {
private final AbnormalityRepo abnormalityRepo;
private final TrainingMasterRepo trainingMasterRepo;
public AbnormalityServiceImpl(AbnormalityRepo abnormalityRepo, TrainingMasterRepo trainingMasterRepo) {
this.abnormalityRepo = abnormalityRepo;
this.trainingMasterRepo = trainingMasterRepo;
}
@Override
public Abnormality getAbnormalityById(Long abnormalityId) {
return abnormalityRepo.findById(abnormalityId)
.orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId));
}
@Override
public Page<Abnormality> getAllAbnormalities(Pageable pageable) {
return abnormalityRepo.findAll(pageable);
}
@Override
public void addAbnormality(List<AbnormalityDto> abnormalityDtos, Long userId) {
List<Abnormality> abnormalities = new ArrayList<>();
for (AbnormalityDto dto : abnormalityDtos) {
validateWellnessProgramIds(dto.wellnessProgramIds());
Abnormality abnormality = convertToEntity(new Abnormality(), dto);
abnormality.setModifiedBy(userId);
abnormalities.add(abnormality);
}
abnormalityRepo.saveAll(abnormalities);
}
@Override
public void updateAbnormality(AbnormalityDto abnormalityDto,Long userId) {
validateWellnessProgramIds(abnormalityDto.wellnessProgramIds());
Long abnormalityId = abnormalityDto.id();
Abnormality abnormality = abnormalityRepo.findById(abnormalityId)
.orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId));
convertToEntity(abnormality, abnormalityDto);
abnormality.setModifiedBy(userId);
abnormalityRepo.save(abnormality);
}
@Override
public void deleteAbnormality(Long abnormalityId) {
Abnormality abnormality = abnormalityRepo.findById(abnormalityId)
.orElseThrow(() -> new RuntimeException("Abnormality not found for ID: " + abnormalityId));
abnormalityRepo.delete(abnormality);
}
private void validateWellnessProgramIds(List<Long> wellnessProgramIds) {
if (wellnessProgramIds != null) {
for (Long programId : wellnessProgramIds) {
if (!trainingMasterRepo.existsById(programId)) {
throw new RuntimeException("Wellness program not found for ID: " + programId);
}
}
}
}
private Abnormality convertToEntity(Abnormality abnormality, AbnormalityDto abnormalityDto) {
abnormality.setAbnormalityName(abnormalityDto.abnormalityName());
if (abnormalityDto.wellnessProgramIds() != null && !abnormalityDto.wellnessProgramIds().isEmpty()) {
String wellnessProgramIds = abnormalityDto.wellnessProgramIds().stream()
.map(String::valueOf)
.collect(Collectors.joining(","));
abnormality.setWellnessProgramIds(wellnessProgramIds);
} else {
abnormality.setWellnessProgramIds("");
}
return abnormality;
}
}