updated code

This commit is contained in:
rajesh
2024-08-28 12:46:41 +05:30
parent a9adc41f76
commit 1b9359971a
2 changed files with 96 additions and 89 deletions

View File

@@ -29,97 +29,104 @@ import java.util.List;
@Component @Component
public class DocumentDao { public class DocumentDao {
@Autowired @Autowired
private AmazonS3Service amazonS3Service; private AmazonS3Service amazonS3Service;
@Autowired @Autowired
private DocumentRepository documentRepository; private DocumentRepository documentRepository;
@Autowired @Autowired
private CallDao callDao; private CallDao callDao;
@Autowired @Autowired
private CallRepository callRepository; private CallRepository callRepository;
public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files,Long callId, DocumentTypeEnum fileType) { public List<DocumentResponseBean> uploadFiles(List<MultipartFile> files, Long callId, DocumentTypeEnum fileType) {
List<DocumentEntity> documentEntities = new ArrayList<>(); List<DocumentEntity> documentEntities = new ArrayList<>();
CallEntity callEntity=callRepository.findById(callId).orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND, Translator.toLocale(GepafinConstant.CALL_NOT_FOUND))); CallEntity callEntity = callRepository.findById(callId)
for (MultipartFile file : files) { .orElseThrow(() -> new ResourceNotFoundException(Status.NOT_FOUND,
try { Translator.toLocale(GepafinConstant.CALL_NOT_FOUND)));
uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file); for (MultipartFile file : files) {
if(result!=null) { try {
DocumentEntity documentEntity = new DocumentEntity(); uploadFileOnAmazonS3 result = uploadFileOnAmazonS3(file);
documentEntity.setFileName(result.fileName()); if (result != null) {
documentEntity.setCall(callEntity); DocumentEntity documentEntity = new DocumentEntity();
documentEntity.setType(fileType.getValue()); documentEntity.setFileName(result.fileName());
documentEntity.setFilePath(result.filepath()); documentEntity.setCall(callEntity);
documentEntity.setIsDeleted(false); documentEntity.setType(fileType.getValue());
documentEntities.add(documentEntity); documentEntity.setFilePath(result.filepath());
} documentEntity.setIsDeleted(false);
} catch (IOException e) {} documentEntities.add(documentEntity);
} }
documentRepository.saveAll(documentEntities); } catch (IOException e) {
return documentEntities.stream() }
.map(callDao::convertToDocumentResponseBean) }
.collect(Collectors.toList()); documentRepository.saveAll(documentEntities);
} return documentEntities.stream().map(callDao::convertToDocumentResponseBean).collect(Collectors.toList());
}
private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException { private uploadFileOnAmazonS3 uploadFileOnAmazonS3(MultipartFile file) throws IOException {
String extension = FilenameUtils.getExtension(file.getOriginalFilename()); String extension = FilenameUtils.getExtension(file.getOriginalFilename());
String fileName = StringUtils.cleanPath(file.getOriginalFilename()); String fileName = StringUtils.cleanPath(file.getOriginalFilename());
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.')); String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
fileName = (firstNameContain + "." + extension); fileName = (firstNameContain + "." + extension);
String filepath = amazonS3Service.upload(fileName, file); String filepath = amazonS3Service.upload(fileName, file);
uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath); uploadFileOnAmazonS3 result = new uploadFileOnAmazonS3(fileName, filepath);
return result; return result;
} }
private record uploadFileOnAmazonS3(String fileName, String filepath) { private record uploadFileOnAmazonS3(String fileName, String filepath) {
} }
public void deleteFile(Long documentId){ public void deleteFile(Long documentId) {
DocumentEntity documentEntity = getDocumentEntity(documentId); DocumentEntity documentEntity = getDocumentEntity(documentId);
String fileName= Utils.extractFileName(documentEntity.getFilePath()); // String fileName= Utils.extractFileName(documentEntity.getFilePath());
deleteFileOnAmazonS3(fileName); // deleteFileOnAmazonS3(fileName);
documentRepository.delete(documentEntity); documentEntity.setIsDeleted(true);
} documentRepository.save(documentEntity);
}
private DocumentEntity deleteFileOnAmazonS3(String fileName) { private DocumentEntity deleteFileOnAmazonS3(String fileName) {
try { try {
amazonS3Service.delete(fileName); amazonS3Service.delete(fileName);
}catch (Exception e){} } catch (Exception e) {
return null; }
} return null;
}
private DocumentEntity getDocumentEntity(Long documentId) { private DocumentEntity getDocumentEntity(Long documentId) {
Optional<DocumentEntity> documentEntity= documentRepository.findById(documentId); Optional<DocumentEntity> documentEntity = documentRepository.findById(documentId);
if(documentEntity.isEmpty()){ if (documentEntity.isEmpty()) {
throw new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); throw new ResourceNotFoundException(Status.VALIDATION_ERROR,
} Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
return documentEntity.orElse(null); }
} return documentEntity.orElse(null);
}
public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum){ public DocumentResponseBean updateDocument(Long documentId, MultipartFile file, DocumentTypeEnum documentTypeEnum) {
DocumentEntity documentEntity = getDocumentEntity(documentId); DocumentEntity documentEntity = getDocumentEntity(documentId);
String fileName= Utils.extractFileName(documentEntity.getFilePath()); String fileName = Utils.extractFileName(documentEntity.getFilePath());
deleteFileOnAmazonS3(fileName); deleteFileOnAmazonS3(fileName);
uploadFileOnAmazonS3 result= null; uploadFileOnAmazonS3 result = null;
try { try {
result = uploadFileOnAmazonS3(file); result = uploadFileOnAmazonS3(file);
} catch (IOException e) {} } catch (IOException e) {
if(result!=null){ }
documentEntity.setFilePath(result.filepath); if (result != null) {
documentEntity.setFileName(result.fileName); documentEntity.setFilePath(result.filepath);
documentEntity.setType(documentTypeEnum.getValue()); documentEntity.setFileName(result.fileName);
documentRepository.save(documentEntity); documentEntity.setType(documentTypeEnum.getValue());
} documentRepository.save(documentEntity);
return callDao.convertToDocumentResponseBean(documentEntity); }
} return callDao.convertToDocumentResponseBean(documentEntity);
public DocumentResponseBean getDocument(Long documentId) { }
Optional<DocumentEntity> documentEntity= documentRepository.findById(documentId);
if(documentEntity.isEmpty()){ public DocumentResponseBean getDocument(Long documentId) {
new ResourceNotFoundException(Status.VALIDATION_ERROR, Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND)); Optional<DocumentEntity> documentEntity = documentRepository.findById(documentId);
} if (documentEntity.isEmpty()) {
return callDao.convertToDocumentResponseBean(documentEntity.orElse(null)); new ResourceNotFoundException(Status.VALIDATION_ERROR,
} Translator.toLocale(GepafinConstant.DOCUMENT_NOT_FOUND));
} }
return callDao.convertToDocumentResponseBean(documentEntity.orElse(null));
}
}

View File

@@ -19,7 +19,7 @@ import java.util.List;
public interface LookUpDataApi { public interface LookUpDataApi {
@Operation(summary = "API to create LookUp Data", @Operation(summary = "Api to create LookUp Data",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -31,7 +31,7 @@ public interface LookUpDataApi {
@PostMapping(value = "", consumes = "application/json", produces = "application/json") @PostMapping(value = "", consumes = "application/json", produces = "application/json")
ResponseEntity<Response<LookUpDataResponseBean>> createLookUpData(HttpServletRequest request, @Valid @RequestBody LookUpDataRequest lookUpDataReq); ResponseEntity<Response<LookUpDataResponseBean>> createLookUpData(HttpServletRequest request, @Valid @RequestBody LookUpDataRequest lookUpDataReq);
@Operation(summary = "API to get LookUp Data by id", @Operation(summary = "Api to get LookUp Data by id",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -43,7 +43,7 @@ public interface LookUpDataApi {
@GetMapping(value = "/{id}", produces = "application/json") @GetMapping(value = "/{id}", produces = "application/json")
ResponseEntity<Response<LookUpDataResponseBean>> getLookUpDataById(HttpServletRequest request, @PathVariable Long id); ResponseEntity<Response<LookUpDataResponseBean>> getLookUpDataById(HttpServletRequest request, @PathVariable Long id);
@Operation(summary = "API to update LookUp Data", @Operation(summary = "Api to update LookUp Data",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -55,7 +55,7 @@ public interface LookUpDataApi {
@PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json") @PutMapping(value = "/{id}", consumes = "application/json", produces = "application/json")
ResponseEntity<Response<LookUpDataResponseBean>> updateLookUpData(HttpServletRequest request, @PathVariable Long id, @Valid @RequestBody LookUpDataRequest lookUpDataReq); ResponseEntity<Response<LookUpDataResponseBean>> updateLookUpData(HttpServletRequest request, @PathVariable Long id, @Valid @RequestBody LookUpDataRequest lookUpDataReq);
@Operation(summary = "API to delete LookUp Data", @Operation(summary = "Api to delete LookUp Data",
responses = { responses = {
@ApiResponse(responseCode = "204", description = "No Content"), @ApiResponse(responseCode = "204", description = "No Content"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {
@@ -67,7 +67,7 @@ public interface LookUpDataApi {
@DeleteMapping(value = "/{id}") @DeleteMapping(value = "/{id}")
ResponseEntity<Response<Void>> deleteLookUpData(HttpServletRequest request, @PathVariable Long id); ResponseEntity<Response<Void>> deleteLookUpData(HttpServletRequest request, @PathVariable Long id);
@Operation(summary = "API to get LookUp Data by type", @Operation(summary = "Api to get LookUp Data by type",
responses = { responses = {
@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = { @ApiResponse(responseCode = "404", description = "Not Found", content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, examples = {