88 lines
2.0 KiB
PHP
88 lines
2.0 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);
|
||
|
|
||
|
$toolRepo = new ToolRepo($json->json());
|
||
|
|
||
|
$tool = new Tool();
|
||
|
$tool->create([
|
||
|
'name' => $toolRepo->name,
|
||
|
'description' => $toolRepo->description,
|
||
|
'discovery_url' => $url,
|
||
|
'api_key' => $request->input('api_key'),
|
||
|
'data' => $toolRepo,
|
||
|
'user_id' => $request->user('api')->id,
|
||
|
]);
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|