100 lines
2.6 KiB
PHP
100 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\ApiProvider;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class ApiConfigController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of API providers
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$query = ApiProvider::query();
|
|
|
|
if ($request->has('active')) {
|
|
$query->where('active', $request->boolean('active'));
|
|
}
|
|
|
|
return response()->json([
|
|
'data' => $query->get()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created API provider
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'provider_id' => 'required|string|unique:api_providers',
|
|
'name' => 'required|string',
|
|
'api_key' => 'required|string',
|
|
'endpoint' => 'required|url'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => 'Validation failed.',
|
|
'details' => $validator->errors()
|
|
], 400);
|
|
}
|
|
|
|
$provider = ApiProvider::create($request->all());
|
|
|
|
return response()->json([
|
|
'message' => 'API provider has been successfully added.',
|
|
'provider_id' => $provider->provider_id
|
|
], 201);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified API provider
|
|
*/
|
|
public function destroy(string $provider_id)
|
|
{
|
|
$provider = ApiProvider::where('provider_id', $provider_id)->first();
|
|
|
|
if (!$provider) {
|
|
return response()->json([
|
|
'error' => 'API provider not found.'
|
|
], 404);
|
|
}
|
|
|
|
$provider->delete();
|
|
|
|
return response()->json([
|
|
'message' => 'API provider has been successfully removed.'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Activate the specified API provider
|
|
*/
|
|
public function activate(string $provider_id)
|
|
{
|
|
$provider = ApiProvider::where('provider_id', $provider_id)->first();
|
|
|
|
if (!$provider) {
|
|
return response()->json([
|
|
'error' => '未找到 API 提供者。'
|
|
], 404);
|
|
}
|
|
|
|
// 將其他提供者設為非活躍
|
|
ApiProvider::where('provider_id', '!=', $provider_id)
|
|
->update(['active' => false]);
|
|
|
|
// 設定當前提供者為活躍
|
|
$provider->active = true;
|
|
$provider->save();
|
|
|
|
return response()->json([
|
|
'message' => sprintf("API 提供者 '%s' 現已成為活躍狀態。", $provider->name)
|
|
]);
|
|
}
|
|
}
|