92 lines
2.1 KiB
PHP
92 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ToolRequest;
|
|
use App\Models\Tool;
|
|
use App\Repositories\Tool\Tool as ToolRepo;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class ToolController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
return $this->success(
|
|
Tool::whereUserId($request->user('api')->id)->get()
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @throws \Exception
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$request->validate([
|
|
'name' => 'string|required',
|
|
'description' => 'string|nullable',
|
|
'url' => 'string|required',
|
|
'api_key' => 'string|nullable',
|
|
]);
|
|
|
|
$url = $request->input('url');
|
|
|
|
// 检测是否存在
|
|
if (Tool::whereDiscoveryUrl($url)->exists()) {
|
|
return $this->conflict('The tool already exists');
|
|
}
|
|
|
|
$json = Http::get($url);
|
|
|
|
$tool = new Tool();
|
|
$tool = $tool->create([
|
|
'name' => '',
|
|
'description' => '',
|
|
'discovery_url' => $url,
|
|
'api_key' => $request->input('api_key'),
|
|
'user_id' => $request->user('api')->id,
|
|
]);
|
|
|
|
$toolRepo = new ToolRepo($tool->id, $json->json());
|
|
$tool->update([
|
|
'name' => $toolRepo->name,
|
|
'description' => $toolRepo->description,
|
|
'data' => $toolRepo,
|
|
]);
|
|
|
|
return $this->created($tool);
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(ToolRequest $request, Tool $tool)
|
|
{
|
|
return $this->success($tool);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, string $id)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(ToolRequest $request, Tool $tool)
|
|
{
|
|
$tool->delete();
|
|
|
|
return $this->deleted();
|
|
}
|
|
}
|