package kr.re.etri.autoflow.controllers; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import kr.re.etri.autoflow.service.ExternalAuthService; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.Map; @RestController @RequestMapping("/api/external-auth") @Tag(name = "ExternalAuthController", description = "외부 백엔드 로그인 및 Bearer 토큰 조회 API") @RequiredArgsConstructor public class ExternalAuthController { private final ExternalAuthService externalAuthService; @Operation(summary = "외부 로그인 후 사용자 정보 조회", description = "외부 Spring 백엔드에 로그인 요청 후 id, name, token을 반환합니다.") @PostMapping("/signin") public ResponseEntity signin(@RequestBody SigninRequest request) { try { Map userInfo = externalAuthService.getUserInfo(request.id(), request.password()); return ResponseEntity.ok(ApiResponse.success(userInfo)); } catch (Exception e) { return ResponseEntity.ok(ApiResponse.failure(e.getMessage())); } } @Operation(summary = "Edge 패키지 목록 조회", description = "로그인 후 받은 id와 token을 사용해 외부 Edge 패키지 검색 API를 호출합니다.") @GetMapping("/edge-search") public ResponseEntity edgeSearch( @RequestParam String id, @RequestParam String token ) { try { Map edgeResult = externalAuthService.getEdgePackageList(id, token); return ResponseEntity.ok(ApiResponse.success(edgeResult)); } catch (Exception e) { return ResponseEntity.ok(ApiResponse.failure(e.getMessage())); } } // DTO: 요청 public static record SigninRequest(String id, String password) { } // DTO: 응답 public static class ApiResponse { private boolean success; private Object data; private String errorMessage; public ApiResponse(boolean success, Object data, String errorMessage) { this.success = success; this.data = data; this.errorMessage = errorMessage; } public static ApiResponse success(Object data) { return new ApiResponse(true, data, null); } public static ApiResponse failure(String errorMessage) { return new ApiResponse(false, null, errorMessage); } public boolean isSuccess() { return success; } public Object getData() { return data; } public String getErrorMessage() { return errorMessage; } } }