|
45 | 45 | @AllArgsConstructor |
46 | 46 | public class AgentController { |
47 | 47 |
|
48 | | - private final AgentService agentService; |
49 | | - |
50 | | - /** Get agent list */ |
51 | | - @GetMapping("/list") |
52 | | - public List<Agent> list( |
53 | | - @RequestParam(value = "status", required = false) String status, |
54 | | - @RequestParam(value = "keyword", required = false) String keyword) { |
55 | | - List<Agent> result; |
56 | | - if (StringUtils.isNotBlank(keyword)) { |
57 | | - result = agentService.search(keyword); |
58 | | - } else if (StringUtils.isNotBlank(status)) { |
59 | | - result = agentService.findByStatus(status); |
60 | | - } else { |
61 | | - result = agentService.findAll(); |
62 | | - } |
63 | | - return result; |
64 | | - } |
65 | | - |
66 | | - /** Get agent details by ID */ |
67 | | - @GetMapping("/{id}") |
68 | | - public Agent get(@PathVariable Long id) { |
69 | | - return checkAgentExists(id); |
70 | | - } |
71 | | - |
72 | | - /** Create agent */ |
73 | | - @PostMapping |
74 | | - public Agent create(@RequestBody Agent agent) { |
75 | | - // Set default status |
76 | | - if (StringUtils.isBlank(agent.getStatus())) { |
77 | | - agent.setStatus("draft"); |
78 | | - } |
79 | | - return agentService.save(agent); |
80 | | - } |
81 | | - |
82 | | - /** Update agent */ |
83 | | - @PutMapping("/{id}") |
84 | | - public Agent update(@PathVariable Long id, @RequestBody Agent agent) { |
85 | | - checkAgentExists(id); |
86 | | - agent.setId(id); |
87 | | - return agentService.save(agent); |
88 | | - } |
89 | | - |
90 | | - /** Delete agent */ |
91 | | - @DeleteMapping("/{id}") |
92 | | - public void delete(@PathVariable Long id) { |
93 | | - checkAgentExists(id); |
94 | | - agentService.deleteById(id); |
95 | | - } |
96 | | - |
97 | | - /** Publish agent */ |
98 | | - @PostMapping("/{id}/publish") |
99 | | - public Agent publish(@PathVariable Long id) { |
100 | | - Agent agent = checkAgentExists(id); |
101 | | - agent.setStatus("published"); |
102 | | - return agentService.save(agent); |
103 | | - } |
104 | | - |
105 | | - /** Offline agent */ |
106 | | - @PostMapping("/{id}/offline") |
107 | | - public Agent offline(@PathVariable Long id) { |
108 | | - Agent agent = checkAgentExists(id); |
109 | | - agent.setStatus("offline"); |
110 | | - return agentService.save(agent); |
111 | | - } |
112 | | - |
113 | | - /** Get masked API Key status */ |
114 | | - @GetMapping("/{id}/api-key") |
115 | | - public ApiResponse<ApiKeyResponse> getApiKey(@PathVariable Long id) { |
116 | | - Agent agent = checkAgentExists(id); |
117 | | - String masked = agentService.getApiKeyMasked(id); |
118 | | - return buildApiKeyResponse(masked, agent.getApiKeyEnabled(), "获取 API Key 成功"); |
119 | | - } |
120 | | - |
121 | | - /** Generate API Key */ |
122 | | - @PostMapping("/{id}/api-key/generate") |
123 | | - public ApiResponse<ApiKeyResponse> generateApiKey(@PathVariable Long id) { |
124 | | - checkAgentExists(id); |
125 | | - Agent agent = agentService.generateApiKey(id); |
126 | | - return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "生成 API Key 成功"); |
127 | | - } |
128 | | - |
129 | | - /** Reset API Key */ |
130 | | - @PostMapping("/{id}/api-key/reset") |
131 | | - public ApiResponse<ApiKeyResponse> resetApiKey(@PathVariable Long id) { |
132 | | - checkAgentExists(id); |
133 | | - Agent agent = agentService.resetApiKey(id); |
134 | | - return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "重置 API Key 成功"); |
135 | | - } |
136 | | - |
137 | | - /** Delete API Key */ |
138 | | - @DeleteMapping("/{id}/api-key") |
139 | | - public ApiResponse<ApiKeyResponse> deleteApiKey(@PathVariable Long id) { |
140 | | - checkAgentExists(id); |
141 | | - Agent agent = agentService.deleteApiKey(id); |
142 | | - return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "删除 API Key 成功"); |
143 | | - } |
144 | | - |
145 | | - /** Toggle API Key enable flag */ |
146 | | - @PostMapping("/{id}/api-key/enable") |
147 | | - public ApiResponse<ApiKeyResponse> toggleApiKey( |
148 | | - @PathVariable Long id, @RequestParam("enabled") boolean enabled) { |
149 | | - checkAgentExists(id); |
150 | | - Agent agent = agentService.toggleApiKey(id, enabled); |
151 | | - return buildApiKeyResponse( |
152 | | - agent.getApiKey() == null ? null : "****", agent.getApiKeyEnabled(), "更新 API Key 状态成功"); |
153 | | - } |
154 | | - |
155 | | - private Agent checkAgentExists(Long id) { |
156 | | - Agent agent = agentService.findById(id); |
157 | | - if (agent == null) { |
158 | | - throw new ResponseStatusException( |
159 | | - HttpStatus.NOT_FOUND, "agent with id: %d not found".formatted(id)); |
160 | | - } |
161 | | - return agent; |
162 | | - } |
163 | | - |
164 | | - private ApiResponse<ApiKeyResponse> buildApiKeyResponse( |
165 | | - String apiKey, Integer apiKeyEnabled, String message) { |
166 | | - return ApiResponse.success(message, new ApiKeyResponse(apiKey, apiKeyEnabled)); |
167 | | - } |
| 48 | + private final AgentService agentService; |
| 49 | + |
| 50 | + /** Get agent list */ |
| 51 | + @GetMapping("/list") |
| 52 | + public List<Agent> list(@RequestParam(value = "status", required = false) String status, |
| 53 | + @RequestParam(value = "keyword", required = false) String keyword) { |
| 54 | + List<Agent> result; |
| 55 | + if (StringUtils.isNotBlank(keyword)) { |
| 56 | + result = agentService.search(keyword); |
| 57 | + } |
| 58 | + else if (StringUtils.isNotBlank(status)) { |
| 59 | + result = agentService.findByStatus(status); |
| 60 | + } |
| 61 | + else { |
| 62 | + result = agentService.findAll(); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + /** Get agent details by ID */ |
| 68 | + @GetMapping("/{id}") |
| 69 | + public Agent get(@PathVariable Long id) { |
| 70 | + return checkAgentExists(id); |
| 71 | + } |
| 72 | + |
| 73 | + /** Create agent */ |
| 74 | + @PostMapping |
| 75 | + public Agent create(@RequestBody Agent agent) { |
| 76 | + // Set default status |
| 77 | + if (StringUtils.isBlank(agent.getStatus())) { |
| 78 | + agent.setStatus("draft"); |
| 79 | + } |
| 80 | + return agentService.save(agent); |
| 81 | + } |
| 82 | + |
| 83 | + /** Update agent */ |
| 84 | + @PutMapping("/{id}") |
| 85 | + public Agent update(@PathVariable Long id, @RequestBody Agent agent) { |
| 86 | + checkAgentExists(id); |
| 87 | + agent.setId(id); |
| 88 | + return agentService.save(agent); |
| 89 | + } |
| 90 | + |
| 91 | + /** Delete agent */ |
| 92 | + @DeleteMapping("/{id}") |
| 93 | + public void delete(@PathVariable Long id) { |
| 94 | + checkAgentExists(id); |
| 95 | + agentService.deleteById(id); |
| 96 | + } |
| 97 | + |
| 98 | + /** Publish agent */ |
| 99 | + @PostMapping("/{id}/publish") |
| 100 | + public Agent publish(@PathVariable Long id) { |
| 101 | + Agent agent = checkAgentExists(id); |
| 102 | + agent.setStatus("published"); |
| 103 | + return agentService.save(agent); |
| 104 | + } |
| 105 | + |
| 106 | + /** Offline agent */ |
| 107 | + @PostMapping("/{id}/offline") |
| 108 | + public Agent offline(@PathVariable Long id) { |
| 109 | + Agent agent = checkAgentExists(id); |
| 110 | + agent.setStatus("offline"); |
| 111 | + return agentService.save(agent); |
| 112 | + } |
| 113 | + |
| 114 | + /** Get masked API Key status */ |
| 115 | + @GetMapping("/{id}/api-key") |
| 116 | + public ApiResponse<ApiKeyResponse> getApiKey(@PathVariable Long id) { |
| 117 | + Agent agent = checkAgentExists(id); |
| 118 | + String masked = agentService.getApiKeyMasked(id); |
| 119 | + return buildApiKeyResponse(masked, agent.getApiKeyEnabled(), "获取 API Key 成功"); |
| 120 | + } |
| 121 | + |
| 122 | + /** Generate API Key */ |
| 123 | + @PostMapping("/{id}/api-key/generate") |
| 124 | + public ApiResponse<ApiKeyResponse> generateApiKey(@PathVariable Long id) { |
| 125 | + checkAgentExists(id); |
| 126 | + Agent agent = agentService.generateApiKey(id); |
| 127 | + return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "生成 API Key 成功"); |
| 128 | + } |
| 129 | + |
| 130 | + /** Reset API Key */ |
| 131 | + @PostMapping("/{id}/api-key/reset") |
| 132 | + public ApiResponse<ApiKeyResponse> resetApiKey(@PathVariable Long id) { |
| 133 | + checkAgentExists(id); |
| 134 | + Agent agent = agentService.resetApiKey(id); |
| 135 | + return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "重置 API Key 成功"); |
| 136 | + } |
| 137 | + |
| 138 | + /** Delete API Key */ |
| 139 | + @DeleteMapping("/{id}/api-key") |
| 140 | + public ApiResponse<ApiKeyResponse> deleteApiKey(@PathVariable Long id) { |
| 141 | + checkAgentExists(id); |
| 142 | + Agent agent = agentService.deleteApiKey(id); |
| 143 | + return buildApiKeyResponse(agent.getApiKey(), agent.getApiKeyEnabled(), "删除 API Key 成功"); |
| 144 | + } |
| 145 | + |
| 146 | + /** Toggle API Key enable flag */ |
| 147 | + @PostMapping("/{id}/api-key/enable") |
| 148 | + public ApiResponse<ApiKeyResponse> toggleApiKey(@PathVariable Long id, @RequestParam("enabled") boolean enabled) { |
| 149 | + checkAgentExists(id); |
| 150 | + Agent agent = agentService.toggleApiKey(id, enabled); |
| 151 | + return buildApiKeyResponse(agent.getApiKey() == null ? null : "****", agent.getApiKeyEnabled(), |
| 152 | + "更新 API Key 状态成功"); |
| 153 | + } |
| 154 | + |
| 155 | + private Agent checkAgentExists(Long id) { |
| 156 | + Agent agent = agentService.findById(id); |
| 157 | + if (agent == null) { |
| 158 | + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "agent with id: %d not found".formatted(id)); |
| 159 | + } |
| 160 | + return agent; |
| 161 | + } |
| 162 | + |
| 163 | + private ApiResponse<ApiKeyResponse> buildApiKeyResponse(String apiKey, Integer apiKeyEnabled, String message) { |
| 164 | + return ApiResponse.success(message, new ApiKeyResponse(apiKey, apiKeyEnabled)); |
| 165 | + } |
| 166 | + |
168 | 167 | } |
0 commit comments