@@ -14,7 +14,6 @@ import ru.soune.nocopy.dto.violation.ViolationRequest;
|
||||
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.search.GlobalSearchService;
|
||||
import ru.soune.nocopy.service.violation.ViolationService;
|
||||
@@ -37,92 +36,47 @@ public class GlobalSearchController {
|
||||
|
||||
@PostMapping("/start")
|
||||
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);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
@RequestBody GlobalSearchStartRequest request) {
|
||||
GlobalSearchStartResponse response = new GlobalSearchStartResponse();
|
||||
|
||||
try {
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
|
||||
List<FileEntity> filesToProcess = globalSearchService.getFilesToProcess(request, userId);
|
||||
List<FileEntity> filesToProcess = globalSearchService.getFilesToProcess(request, userId);
|
||||
|
||||
if (filesToProcess.isEmpty()) return ResponseEntity.ok().body("Files for search not found");
|
||||
if (filesToProcess.isEmpty()) return ResponseEntity.ok().body("Files for search not found");
|
||||
|
||||
String taskId = globalSearchService.startSearch(request, userId, filesToProcess);
|
||||
String taskId = globalSearchService.startSearch(request, userId, filesToProcess);
|
||||
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(SearchStatus.ACCEPTED.name());
|
||||
response.setTotalFiles(filesToProcess.size());
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
response.setTaskId(taskId);
|
||||
response.setStatus(SearchStatus.ACCEPTED.name());
|
||||
response.setTotalFiles(filesToProcess.size());
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
|
||||
@GetMapping("/actual-task")
|
||||
public ResponseEntity<?> getStatus(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
|
||||
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
public ResponseEntity<?> getStatus() {
|
||||
Optional<GlobalSearchTask> taskOptional;
|
||||
|
||||
try {
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
|
||||
taskOptional = searchTaskRepository.findByUserIdAndStatus(userId,
|
||||
"PROCESSING");
|
||||
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));
|
||||
}
|
||||
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
||||
|
||||
return ResponseEntity.ok(taskOptional.get());
|
||||
}
|
||||
|
||||
@GetMapping("/violation-summary")
|
||||
public ResponseEntity<?> getSummary(@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
|
||||
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
|
||||
public ResponseEntity<?> getSummary() {
|
||||
List<FileViolationSummaryDTO> fileViolationsSummary;
|
||||
|
||||
try {
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
|
||||
fileViolationsSummary = violationService.getFileViolationsSummary(userId);
|
||||
fileViolationsSummary = violationService.getFileViolationsSummary(userId);
|
||||
|
||||
if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
||||
} catch (NotFoundAuthToken e) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
if (fileViolationsSummary.isEmpty()) return ResponseEntity.ok().body("Violations summary not found");
|
||||
|
||||
return ResponseEntity.ok(fileViolationsSummary);
|
||||
}
|
||||
@@ -139,38 +93,23 @@ public class GlobalSearchController {
|
||||
}
|
||||
|
||||
@GetMapping("/status/{taskId}")
|
||||
public ResponseEntity<?> getStatus(@PathVariable String taskId,
|
||||
@RequestHeader(value = "Authorization", required = false) String tokenHeader) {
|
||||
|
||||
if (tokenHeader == null || tokenHeader.isBlank()) {
|
||||
Map<String, Object> errorData = new HashMap<>();
|
||||
errorData.put("token", tokenHeader);
|
||||
|
||||
return ResponseEntity.ok().body(Map.of("error", errorData));
|
||||
}
|
||||
public ResponseEntity<?> getStatus(@PathVariable String taskId) {
|
||||
GlobalSearchStatusResponse response;
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
|
||||
try {
|
||||
Long userId = (Long) httpServletRequest.getAttribute("userId");
|
||||
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;
|
||||
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