fix
This commit is contained in:
@@ -20,7 +20,7 @@ import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v3/marketing-analysis")
|
||||
@RequestMapping("/api/marketing/v3")
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class MarketingAnalysisV3Controller {
|
||||
@@ -30,15 +30,12 @@ public class MarketingAnalysisV3Controller {
|
||||
|
||||
private String extractUserIdFromHeader(String authHeader) {
|
||||
if (authHeader == null || authHeader.isEmpty()) {
|
||||
log.debug("Authorization header is null or empty");
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String userId = jwtService.extractUserIdFromHeader(authHeader);
|
||||
log.debug("Extracted userId from header: {}", userId);
|
||||
return userId;
|
||||
return jwtService.extractUserIdFromHeader(authHeader);
|
||||
} catch (Exception e) {
|
||||
log.error("Error extracting userId from header: {}", e.getMessage(), e);
|
||||
log.error("Error extracting userId: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -49,18 +46,14 @@ public class MarketingAnalysisV3Controller {
|
||||
@RequestBody @Valid MarketingAnalysisV3Request request
|
||||
) {
|
||||
String userId = extractUserIdFromHeader(authHeader);
|
||||
if (userId == null) {
|
||||
return unauthorizedResponse();
|
||||
}
|
||||
if (userId == null) return unauthorizedResponse();
|
||||
|
||||
try {
|
||||
String analysisId = service.createAndStartAnalysis(request, userId);
|
||||
|
||||
Map<String, String> responseData = Map.of(
|
||||
"analysisId", analysisId,
|
||||
"message", "Analysis V3 started successfully"
|
||||
"message", "Analysis V3 started"
|
||||
);
|
||||
|
||||
return ResponseEntity.accepted().body(ApiResponse.success("Анализ запущен", responseData));
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to start analysis V3", e);
|
||||
@@ -74,21 +67,14 @@ public class MarketingAnalysisV3Controller {
|
||||
@PathVariable String id
|
||||
) {
|
||||
String userId = extractUserIdFromHeader(authHeader);
|
||||
if (userId == null) {
|
||||
return unauthorizedResponse();
|
||||
}
|
||||
if (userId == null) return unauthorizedResponse();
|
||||
|
||||
try {
|
||||
Optional<MarketingAnalysisV3Document> analysisOpt = service.getAnalysisById(id);
|
||||
|
||||
if (analysisOpt.isEmpty()) {
|
||||
return notFoundResponse("Анализ не найден");
|
||||
}
|
||||
if (analysisOpt.isEmpty()) return notFoundResponse("Анализ не найден");
|
||||
|
||||
MarketingAnalysisV3Document analysis = analysisOpt.get();
|
||||
if (!analysis.getUserId().equals(userId)) {
|
||||
return forbiddenResponse();
|
||||
}
|
||||
if (!analysis.getUserId().equals(userId)) return forbiddenResponse();
|
||||
|
||||
return ResponseEntity.ok(ApiResponse.success(analysis));
|
||||
} catch (Exception e) {
|
||||
@@ -102,9 +88,7 @@ public class MarketingAnalysisV3Controller {
|
||||
@RequestHeader(value = "Authorization", required = false) String authHeader
|
||||
) {
|
||||
String userId = extractUserIdFromHeader(authHeader);
|
||||
if (userId == null) {
|
||||
return unauthorizedResponse();
|
||||
}
|
||||
if (userId == null) return unauthorizedResponse();
|
||||
|
||||
try {
|
||||
List<MarketingAnalysisV3Document> analyses = service.getAllByUser(userId);
|
||||
@@ -121,33 +105,27 @@ public class MarketingAnalysisV3Controller {
|
||||
ex.getBindingResult().getFieldErrors().forEach(error ->
|
||||
details.put(error.getField(), error.getDefaultMessage())
|
||||
);
|
||||
|
||||
ErrorResponse error = new ErrorResponse(
|
||||
"VALIDATION_ERROR",
|
||||
"Ошибка валидации входных данных",
|
||||
details
|
||||
);
|
||||
|
||||
ErrorResponse error = new ErrorResponse("VALIDATION_ERROR", "Ошибка валидации", details);
|
||||
return ResponseEntity.badRequest().body(ApiResponse.error("Ошибка валидации", error));
|
||||
}
|
||||
|
||||
private ResponseEntity<ApiResponse<Object>> unauthorizedResponse() {
|
||||
ErrorResponse error = new ErrorResponse("UNAUTHORIZED", "Требуется авторизация");
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ApiResponse.error("Не авторизован", error));
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body(ApiResponse.error("Не авторизован", new ErrorResponse("UNAUTHORIZED", "Требуется авторизация")));
|
||||
}
|
||||
|
||||
private ResponseEntity<ApiResponse<Object>> forbiddenResponse() {
|
||||
ErrorResponse error = new ErrorResponse("FORBIDDEN", "Нет доступа к этому ресурсу");
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(ApiResponse.error("Доступ запрещен", error));
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN)
|
||||
.body(ApiResponse.error("Доступ запрещен", new ErrorResponse("FORBIDDEN", "Нет доступа")));
|
||||
}
|
||||
|
||||
private ResponseEntity<ApiResponse<Object>> notFoundResponse(String message) {
|
||||
ErrorResponse error = new ErrorResponse("NOT_FOUND", message);
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ApiResponse.error("Не найдено", error));
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND)
|
||||
.body(ApiResponse.error("Не найдено", new ErrorResponse("NOT_FOUND", message)));
|
||||
}
|
||||
|
||||
private ResponseEntity<ApiResponse<Object>> internalErrorResponse(Exception e) {
|
||||
ErrorResponse error = new ErrorResponse("INTERNAL_SERVER_ERROR", e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.error("Ошибка сервера", error));
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body(ApiResponse.error("Ошибка сервера", new ErrorResponse("INTERNAL_SERVER_ERROR", e.getMessage())));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user