2026-03-06 17:28:52 +07:00
|
|
|
package ru.soune.nocopy.controller;
|
|
|
|
|
|
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartRequest;
|
|
|
|
|
import ru.soune.nocopy.dto.search.GlobalSearchStartResponse;
|
|
|
|
|
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.repository.GlobalSearchTaskRepository;
|
|
|
|
|
import ru.soune.nocopy.service.register.AuthService;
|
|
|
|
|
import ru.soune.nocopy.service.search.GlobalSearchService;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/v1/global-search")
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
|
public class GlobalSearchController {
|
|
|
|
|
|
|
|
|
|
private final AuthService authService;
|
|
|
|
|
|
|
|
|
|
private final GlobalSearchService globalSearchService;
|
|
|
|
|
|
|
|
|
|
private final GlobalSearchTaskRepository globalSearchTaskRepository;
|
|
|
|
|
|
|
|
|
|
private final GlobalSearchTaskRepository searchTaskRepository;
|
|
|
|
|
|
|
|
|
|
@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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GetMapping("/status/{taskId}")
|
2026-03-10 22:47:43 +07:00
|
|
|
public ResponseEntity<?> getStatus(@PathVariable String taskId,
|
2026-03-06 17:28:52 +07:00
|
|
|
@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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Long userId = authService.useUserAuthToken(tokenHeader);
|
|
|
|
|
|
|
|
|
|
Optional<GlobalSearchTask> taskOptional = searchTaskRepository.findByTaskIdAndUserId(taskId, userId);
|
|
|
|
|
|
|
|
|
|
if (taskOptional.isEmpty()) return ResponseEntity.ok().body("Task not found");
|
|
|
|
|
|
|
|
|
|
GlobalSearchTask task = taskOptional.orElseThrow();
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(response);
|
|
|
|
|
}
|
|
|
|
|
}
|