Refactored to optional type
This commit is contained in:
parent
f30961f1f0
commit
66d33e7e56
@ -14,6 +14,8 @@ import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
@RequiredArgsConstructor
|
||||
@ -44,11 +46,17 @@ public class FileUploadRepository {
|
||||
r.db("sharepulse").table("file_uploads").insert(map).run(connection);
|
||||
}
|
||||
|
||||
public FileUpload retrieveFileUploadByFileId(String fileId) {
|
||||
return r.db("sharepulse").table("file_uploads")
|
||||
.filter(r.hashMap("fileId", fileId))
|
||||
.run(connection, FileUpload.class)
|
||||
.next();
|
||||
public Optional<FileUpload> retrieveFileUploadByFileId(String fileId) {
|
||||
try {
|
||||
FileUpload fileUpload = r.db("sharepulse").table("file_uploads")
|
||||
.filter(r.hashMap("fileId", fileId))
|
||||
.run(connection, FileUpload.class)
|
||||
.next();
|
||||
return Optional.ofNullable(fileUpload);
|
||||
} catch (NoSuchElementException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void updateFileUpload(FileUpload updatedFileUpload) {
|
||||
|
@ -28,9 +28,9 @@ public class TestRestResource extends ApiRestController {
|
||||
}
|
||||
|
||||
@GetMapping("test")
|
||||
public String test(@RequestParam String id) {
|
||||
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id);
|
||||
return fileUpload != null ? fileUpload.toString() : "FileUpload not found for id: " + id;
|
||||
public FileUpload test(@RequestParam String id) {
|
||||
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id).orElse(null);
|
||||
return fileUpload;
|
||||
}
|
||||
|
||||
@GetMapping("test/download")
|
||||
|
@ -22,6 +22,7 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@ -41,7 +42,11 @@ public class Download extends ApiRestController {
|
||||
@RequestParam(value = "password", required = false) String password) throws IOException {
|
||||
|
||||
|
||||
FileUpload fileUpload = fileService.getFileUploadByFileId(fileId);
|
||||
Optional<FileUpload> optionalFileUpload = fileService.getFileUploadByFileId(fileId);
|
||||
FileUpload fileUpload = optionalFileUpload.orElse(null);
|
||||
if(optionalFileUpload.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
try {
|
||||
fileSecurityService.verifyDownloadPermission(fileUpload, password);
|
||||
@ -73,7 +78,11 @@ public class Download extends ApiRestController {
|
||||
|
||||
@GetMapping("/download-info")
|
||||
public ResponseEntity<Object> getDownloadInfo(@RequestParam String fileId) {
|
||||
FileUpload fileUpload = fileService.getFileUploadByFileId(fileId);
|
||||
Optional<FileUpload> optionalFileUpload = fileService.getFileUploadByFileId(fileId);
|
||||
FileUpload fileUpload = optionalFileUpload.orElse(null);
|
||||
if(optionalFileUpload.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
boolean downloadable = !fileUpload.isSingleDownload() || fileUpload.getDownloadCount() == 0;
|
||||
|
||||
|
@ -17,6 +17,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Date;
|
||||
import java.util.Optional;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@ -104,7 +105,7 @@ public class FileService {
|
||||
return new File(getTempDirPath() + File.separator + fileId);
|
||||
}
|
||||
|
||||
public FileUpload getFileUploadByFileId(String fileId) {
|
||||
public Optional<FileUpload> getFileUploadByFileId(String fileId) {
|
||||
return fileUploadRepository.retrieveFileUploadByFileId(fileId);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user