refactored code
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package net.gepafin.tendermanagement.dao;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
@@ -16,9 +15,9 @@ import net.gepafin.tendermanagement.entities.CallEntity;
|
||||
import net.gepafin.tendermanagement.entities.DocumentEntity;
|
||||
import net.gepafin.tendermanagement.enums.DocumentTypeEnum;
|
||||
import net.gepafin.tendermanagement.model.response.DocumentResponseBean;
|
||||
import net.gepafin.tendermanagement.repositories.CallRepository;
|
||||
import net.gepafin.tendermanagement.repositories.DocumentRepository;
|
||||
import net.gepafin.tendermanagement.service.AmazonS3Service;
|
||||
import net.gepafin.tendermanagement.service.CallService;
|
||||
import net.gepafin.tendermanagement.util.Utils;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.ResourceNotFoundException;
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
@@ -39,13 +38,11 @@ public class DocumentDao {
|
||||
private CallDao callDao;
|
||||
|
||||
@Autowired
|
||||
private CallRepository callRepository;
|
||||
private CallService callService;
|
||||
|
||||
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, Long callId, DocumentTypeEnum fileType) {
|
||||
List<DocumentEntity> documentEntities = new ArrayList<>();
|
||||
CallEntity callEntity = callRepository.findById(callId)
|
||||
.orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
|
||||
CallEntity callEntity = callService.validateCall(callId);
|
||||
for (MultipartFile file : files) {
|
||||
try {
|
||||
uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file);
|
||||
@@ -79,7 +76,7 @@ public class DocumentDao {
|
||||
}
|
||||
|
||||
public void deleteFile(Long documentId) {
|
||||
DocumentEntity documentEntity = getDocumentEntity(documentId);
|
||||
DocumentEntity documentEntity = validateDocument(documentId);
|
||||
// String fileName= Utils.extractFileName(documentEntity.getFilePath());
|
||||
// deleteFileOnAmazonS3(fileName);
|
||||
documentEntity.setIsDeleted(true);
|
||||
@@ -94,17 +91,13 @@ public class DocumentDao {
|
||||
return null;
|
||||
}
|
||||
|
||||
private DocumentEntity getDocumentEntity(Long documentId) {
|
||||
Optional<DocumentEntity> documentEntity = documentRepository.findById(documentId);
|
||||
if (documentEntity.isEmpty()) {
|
||||
throw new ResourceNotFoundException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
return documentEntity.orElse(null);
|
||||
public DocumentEntity validateDocument(Long id) {
|
||||
return documentRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
|
||||
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)));
|
||||
}
|
||||
|
||||
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
|
||||
DocumentEntity documentEntity = getDocumentEntity(documentId);
|
||||
DocumentEntity documentEntity = validateDocument(documentId);
|
||||
String fileName = Utils.extractFileName(documentEntity.getFilePath());
|
||||
deleteFileOnAmazonS3(fileName);
|
||||
uploadFileOnAmazonS3 result = null;
|
||||
@@ -122,11 +115,7 @@ public class DocumentDao {
|
||||
}
|
||||
|
||||
public DocumentResponseBean getDocument(Long documentId) {
|
||||
Optional<DocumentEntity> documentEntity = documentRepository.findById(documentId);
|
||||
if (documentEntity.isEmpty()) {
|
||||
new ResourceNotFoundException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
|
||||
}
|
||||
return callDao.convertToDocumentResponseBean(documentEntity.orElse(null));
|
||||
DocumentEntity documentEntity = validateDocument(documentId);
|
||||
return callDao.convertToDocumentResponseBean(documentEntity);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user