@@ -10,6 +10,7 @@ import ru.soune.nocopy.dto.search.GlobalSearchStatusResponse;
|
||||
import ru.soune.nocopy.entity.file.FileEntity;
|
||||
import ru.soune.nocopy.entity.search.GlobalSearchTask;
|
||||
import ru.soune.nocopy.entity.search.SearchStatus;
|
||||
import ru.soune.nocopy.exception.NotFoundAuthToken;
|
||||
import ru.soune.nocopy.repository.GlobalSearchTaskRepository;
|
||||
import ru.soune.nocopy.service.register.AuthService;
|
||||
import ru.soune.nocopy.service.search.GlobalSearchService;
|
||||
@@ -34,7 +35,6 @@ public class GlobalSearchController {
|
||||
public ResponseEntity<?> startSearch(
|
||||
@RequestBody GlobalSearchStartRequest request,
|
||||
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
|
||||
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
@@ -42,23 +42,32 @@ public class GlobalSearchController {
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
|
||||
List<FileEntity> filesToProcess = globalSearchService.getFilesToProcess(request, userId);
|
||||
|
||||
if (filesToProcess.isEmpty()) return ResponseEntity.ok().body("Files for search not found");
|
||||
|
||||
String taskId = globalSearchService.startSearch(request, userId, filesToProcess);
|
||||
|
||||
Optional<GlobalSearchTask> taskOptional = globalSearchTaskRepository.findById(taskId);
|
||||
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
|
||||
GlobalSearchTask task = taskOptional.orElseThrow();
|
||||
GlobalSearchStartResponse response = new GlobalSearchStartResponse();
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(SearchStatus.ACCEPTED.name());
|
||||
response.setTotalFiles(task.getTotalFiles());
|
||||
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
|
||||
List<FileEntity> filesToProcess = globalSearchService.getFilesToProcess(request, userId);
|
||||
|
||||
if (filesToProcess.isEmpty()) return ResponseEntity.ok().body("Files for search not found");
|
||||
|
||||
String taskId = globalSearchService.startSearch(request, userId, filesToProcess);
|
||||
|
||||
Optional<GlobalSearchTask> taskOptional = globalSearchTaskRepository.findById(taskId);
|
||||
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
|
||||
GlobalSearchTask task = taskOptional.orElseThrow();
|
||||
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(SearchStatus.ACCEPTED.name());
|
||||
response.setTotalFiles(task.getTotalFiles());
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
@@ -73,12 +82,21 @@ public class GlobalSearchController {
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
Optional<GlobalSearchTask> taskOptional;
|
||||
|
||||
Optional<GlobalSearchTask> taskOptional = searchTaskRepository.findByUserIdAndStatus(userId,
|
||||
"PROCESSING");
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
taskOptional = searchTaskRepository.findByUserIdAndStatus(userId,
|
||||
"PROCESSING");
|
||||
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(taskOptional.get());
|
||||
}
|
||||
@@ -93,23 +111,29 @@ public class GlobalSearchController {
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
GlobalSearchStatusResponse response;
|
||||
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
try {
|
||||
Long userId = authService.useUserAuthToken(tokenHeader);
|
||||
|
||||
Optional<GlobalSearchTask> taskOptional = searchTaskRepository.findByTaskIdAndUserId(taskId, userId);
|
||||
Optional<GlobalSearchTask> taskOptional = searchTaskRepository.findByTaskIdAndUserId(taskId, userId);
|
||||
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
|
||||
GlobalSearchTask task = taskOptional.orElseThrow();
|
||||
GlobalSearchTask task = taskOptional.orElseThrow();
|
||||
|
||||
int progress = task.getTotalFiles() > 0
|
||||
? task.getProcessedFiles() * 100 / task.getTotalFiles()
|
||||
: 0;
|
||||
int progress = task.getTotalFiles() > 0 ? task.getProcessedFiles() * 100 / task.getTotalFiles() : 0;
|
||||
|
||||
GlobalSearchStatusResponse response = new GlobalSearchStatusResponse();
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(task.getStatus());
|
||||
response.setProgress(progress);
|
||||
response = new GlobalSearchStatusResponse();
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(task.getStatus());
|
||||
response.setProgress(progress);
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user