Refactored to optional type

This commit is contained in:
Max W. 2024-02-17 17:04:46 +01:00
parent f30961f1f0
commit 66d33e7e56
4 changed files with 29 additions and 11 deletions

View File

@ -14,6 +14,8 @@ import org.springframework.stereotype.Repository;
import java.lang.reflect.Type; import java.lang.reflect.Type;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
@Repository @Repository
@RequiredArgsConstructor @RequiredArgsConstructor
@ -44,11 +46,17 @@ public class FileUploadRepository {
r.db("sharepulse").table("file_uploads").insert(map).run(connection); r.db("sharepulse").table("file_uploads").insert(map).run(connection);
} }
public FileUpload retrieveFileUploadByFileId(String fileId) { public Optional<FileUpload> retrieveFileUploadByFileId(String fileId) {
return r.db("sharepulse").table("file_uploads") try {
.filter(r.hashMap("fileId", fileId)) FileUpload fileUpload = r.db("sharepulse").table("file_uploads")
.run(connection, FileUpload.class) .filter(r.hashMap("fileId", fileId))
.next(); .run(connection, FileUpload.class)
.next();
return Optional.ofNullable(fileUpload);
} catch (NoSuchElementException e) {
return Optional.empty();
}
} }
public void updateFileUpload(FileUpload updatedFileUpload) { public void updateFileUpload(FileUpload updatedFileUpload) {

View File

@ -28,9 +28,9 @@ public class TestRestResource extends ApiRestController {
} }
@GetMapping("test") @GetMapping("test")
public String test(@RequestParam String id) { public FileUpload test(@RequestParam String id) {
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id); FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id).orElse(null);
return fileUpload != null ? fileUpload.toString() : "FileUpload not found for id: " + id; return fileUpload;
} }
@GetMapping("test/download") @GetMapping("test/download")

View File

@ -22,6 +22,7 @@ import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
@Slf4j @Slf4j
@RestController @RestController
@ -41,7 +42,11 @@ public class Download extends ApiRestController {
@RequestParam(value = "password", required = false) String password) throws IOException { @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 { try {
fileSecurityService.verifyDownloadPermission(fileUpload, password); fileSecurityService.verifyDownloadPermission(fileUpload, password);
@ -73,7 +78,11 @@ public class Download extends ApiRestController {
@GetMapping("/download-info") @GetMapping("/download-info")
public ResponseEntity<Object> getDownloadInfo(@RequestParam String fileId) { 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; boolean downloadable = !fileUpload.isSingleDownload() || fileUpload.getDownloadCount() == 0;

View File

@ -17,6 +17,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Date; import java.util.Date;
import java.util.Optional;
@Slf4j @Slf4j
@Service @Service
@ -104,7 +105,7 @@ public class FileService {
return new File(getTempDirPath() + File.separator + fileId); return new File(getTempDirPath() + File.separator + fileId);
} }
public FileUpload getFileUploadByFileId(String fileId) { public Optional<FileUpload> getFileUploadByFileId(String fileId) {
return fileUploadRepository.retrieveFileUploadByFileId(fileId); return fileUploadRepository.retrieveFileUploadByFileId(fileId);
} }