ohctechv3/.svn/pristine/f5/f5acfb0d5e109c4350369482470b308213b145a1.svn-base
2024-10-28 15:03:36 +05:30

66 lines
2.3 KiB
Plaintext

package com.healthcare.ohctech.service.impl;
import com.healthcare.ohctech.dto.InjuryTypeDto;
import com.healthcare.ohctech.entity.InjuryType;
import com.healthcare.ohctech.repository.InjuryTypeRepo;
import com.healthcare.ohctech.service.InjuryTypeService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@Service
public class InjuryTypeServiceImpl implements InjuryTypeService {
private final InjuryTypeRepo injuryTypeRepo;
public InjuryTypeServiceImpl(InjuryTypeRepo injuryTypeRepo) {
this.injuryTypeRepo = injuryTypeRepo;
}
@Override
public InjuryType getInjuryTypeById(Long injuryTypeId) {
return injuryTypeRepo.findById(injuryTypeId)
.orElseThrow(() -> new RuntimeException("Injury Type not found for ID: " + injuryTypeId));
}
@Override
public Page<InjuryType> getAllInjuryTypes(Pageable pageable) {
return injuryTypeRepo.findAll(pageable);
}
@Override
public void addInjuryType(InjuryTypeDto injuryTypeDto, Long userId) {
InjuryType injuryType = convertToEntity(new InjuryType(), injuryTypeDto);
injuryType.setModifiedBy(userId);
injuryTypeRepo.save(injuryType);
}
@Override
public void updateInjuryType(InjuryTypeDto injuryTypeDto, Long userId) {
Long injuryTypeId = injuryTypeDto.id();
InjuryType injuryType = injuryTypeRepo.findById(injuryTypeId)
.orElseThrow(() -> new RuntimeException("Injury Type not found for ID: " + injuryTypeId));
convertToEntity(injuryType, injuryTypeDto);
injuryType.setModifiedBy(userId);
injuryTypeRepo.save(injuryType);
}
@Override
public void deleteInjuryType(Long injuryTypeId) {
InjuryType injuryType = injuryTypeRepo.findById(injuryTypeId)
.orElseThrow(() -> new RuntimeException("Injury Type not found for ID: " + injuryTypeId));
injuryTypeRepo.delete(injuryType);
}
private InjuryType convertToEntity(InjuryType injuryType, InjuryTypeDto injuryTypeDto) {
injuryType.setInjuryTypeName(injuryTypeDto.injuryTypeName());
injuryType.setInjuryTypeCode(injuryTypeDto.injuryTypeCode());
injuryType.setInjuryTypeDesc(injuryTypeDto.injuryTypeDesc());
return injuryType;
}
}