package net.gepafin.tendermanagement.dao; import net.gepafin.tendermanagement.config.Translator; import net.gepafin.tendermanagement.constants.GepafinConstant; import net.gepafin.tendermanagement.entities.CallEntity; import net.gepafin.tendermanagement.entities.FaqEntity; import net.gepafin.tendermanagement.entities.UserEntity; import net.gepafin.tendermanagement.model.request.FaqReq; import net.gepafin.tendermanagement.model.response.FaqResponseBean; import net.gepafin.tendermanagement.repositories.CallRepository; import net.gepafin.tendermanagement.repositories.FaqRepository; import net.gepafin.tendermanagement.repositories.UserRepository; import net.gepafin.tendermanagement.util.DateTimeUtil; import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException; import net.gepafin.tendermanagement.web.rest.api.errors.Status; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.time.LocalDateTime; @Component public class FaqDao { @Autowired private FaqRepository faqRepository; @Autowired private UserRepository userRepository; @Autowired private CallDao callDao; @Autowired private CallRepository callRepository; public FaqResponseBean createFaq(FaqReq faqRequest, Long userId, Long callId) { FaqEntity entity = new FaqEntity(); CallEntity callEntity = callRepository.findById(callId) .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.CALL_NOT_FOUND))); entity = callDao.convertToFaqEntity(faqRequest, callEntity, userId); faqRepository.save(entity); return convertFaqEntityToResponseBean(entity); } public FaqResponseBean getFaqById(Long id) { return faqRepository.findById(id).map(this::convertFaqEntityToResponseBean) .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.FAQ_NOT_FOUND))); } public FaqResponseBean updateFaq(Long id, FaqReq faqRequest, Long userId) { FaqEntity entity = faqRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.FAQ_NOT_FOUND))); updateFaqEntity(entity, faqRequest, userId, entity.getCall()); faqRepository.save(entity); return convertFaqEntityToResponseBean(entity); } public void deleteFaq(Long id) { FaqEntity entity = faqRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.FAQ_NOT_FOUND))); faqRepository.deleteById(entity.getId()); } private FaqResponseBean convertFaqEntityToResponseBean(FaqEntity entity) { FaqResponseBean response = new FaqResponseBean(); response.setId(entity.getId()); response.setUserId(entity.getUser().getId()); response.setIsVisible(entity.getIsVisible()); response.setUpdatedDate(entity.getUpdatedDate()); response.setCreatedDate(entity.getCreatedDate()); response.setQuestionShort(entity.getQuestionShort()); response.setQuestion(entity.getQuestion()); response.setResponseShort(entity.getResponseShort()); response.setResponse(entity.getResponse()); response.setResponseDate(entity.getResponseDate()); return response; } private void updateFaqEntity(FaqEntity faqEntity, FaqReq faqReq, Long userId, CallEntity callEntity) { faqEntity.setQuestion(faqReq.getQuestion()); UserEntity userEntity = userRepository.findById(userId) .orElseThrow(() -> new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.LOOK_UP_DATA_NOT_VALID_MSG))); faqEntity.setUser(userEntity); faqEntity.setIsVisible(true); if (faqReq.getIsVisible() != null) { faqEntity.setIsVisible(faqReq.getIsVisible()); } faqEntity.setQuestionShort(faqReq.getQuestionShort()); faqEntity.setQuestion(faqReq.getQuestion()); if (faqReq.getResponse() != null || faqReq.getResponseShort() != null) { faqEntity.setResponseDate(DateTimeUtil.DateServerToUTC(LocalDateTime.now())); } faqEntity.setResponseShort(faqReq.getResponseShort()); faqEntity.setResponse(faqReq.getResponse()); faqEntity.setCall(callEntity); } }