package net.gepafin.tendermanagement.dao; import net.gepafin.tendermanagement.entities.LookUpDataEntity; import net.gepafin.tendermanagement.entities.LookUpDataEntity.LookUpDataTypeEnum; import net.gepafin.tendermanagement.model.request.LookUpDataRequest; import net.gepafin.tendermanagement.model.response.LookUpDataResponseBean; import net.gepafin.tendermanagement.repositories.LookUpDataRepository; import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException; import net.gepafin.tendermanagement.web.rest.api.errors.Status; import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; import java.util.stream.Collectors; @Component public class LookUpDataDao { @Autowired private LookUpDataRepository lookUpDataRepository; public LookUpDataResponseBean createLookUpData(LookUpDataRequest lookUpDataReq) { LookUpDataEntity entity = convertLookUpDataReqToLookUpDataEntity(lookUpDataReq); return convertLookUpDataEntityToResponseBean(entity); } private LookUpDataEntity convertLookUpDataReqToLookUpDataEntity(LookUpDataRequest lookUpDataReq) { LookUpDataEntity entity = new LookUpDataEntity(); entity.setTitle(lookUpDataReq.getTitle()); entity.setType(lookUpDataReq.getType().getValue()); entity.setValue(lookUpDataReq.getValue()); lookUpDataRepository.save(entity); return entity; } public LookUpDataResponseBean getLookUpDataById(Long id) { return lookUpDataRepository.findById(id) .map(this::convertLookUpDataEntityToResponseBean) .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.LOOKUP_DATA_NOT_FOUND))); } public LookUpDataResponseBean updateLookUpData(Long id, LookUpDataRequest lookUpDataReq) { LookUpDataEntity entity = lookUpDataRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.LOOKUP_DATA_NOT_FOUND))); entity.setTitle(lookUpDataReq.getTitle()); entity.setType(lookUpDataReq.getType().getValue()); entity.setValue(lookUpDataReq.getValue()); lookUpDataRepository.save(entity); return convertLookUpDataEntityToResponseBean(entity); } public void deleteLookUpData(Long id) { LookUpDataEntity entity = lookUpDataRepository.findById(id) .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.LOOKUP_DATA_NOT_FOUND))); lookUpDataRepository.deleteById(entity.getId()); } private LookUpDataResponseBean convertLookUpDataEntityToResponseBean(LookUpDataEntity entity) { LookUpDataResponseBean response = new LookUpDataResponseBean(); response.setId(entity.getId()); response.setTitle(entity.getTitle()); response.setType(LookUpDataEntity.LookUpDataTypeEnum.valueOf(entity.getType())); response.setValue(entity.getValue()); response.setCreatedDate(entity.getCreatedDate()); response.setUpdatedDate(entity.getUpdatedDate()); return response; } public List getLookUpDataByType(LookUpDataTypeEnum type) { return lookUpDataRepository.findByType(type.getValue()) .stream() .map(this::convertLookUpDataEntityToResponseBean) .collect(Collectors.toList()); } }