mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-14 12:30:49 +08:00
tag and installer&updater use english
This commit is contained in:
101
app/Http/Controllers/TagController.php
Normal file
101
app/Http/Controllers/TagController.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Tag;
|
||||
use App\Repositories\TagRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class TagController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(TagRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
private function getRules($id = null): array
|
||||
{
|
||||
return [
|
||||
'name' => ['required', 'string', Rule::unique('tags', 'name')->ignore($id)],
|
||||
'color' => 'required|string',
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = TagResource::collection($result);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$data = $request->all();
|
||||
if (isset($data['priority'])) {
|
||||
$data['priority'] = intval($data['priority']);
|
||||
}
|
||||
$result = $this->repository->store($data);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = Tag::query()->findOrFail($id);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate($this->getRules($id));
|
||||
$data = $request->all();
|
||||
if (isset($data['priority'])) {
|
||||
$data['priority'] = intval($data['priority']);
|
||||
}
|
||||
$result = $this->repository->update($data, $id);
|
||||
$resource = new TagResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user