mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 06:56:15 +00:00
#591 - tags
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\Tag;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
@ -16,10 +17,13 @@ class CreateTag
|
||||
*/
|
||||
public function create(User $user, array $input): Tag
|
||||
{
|
||||
Validator::make($input, self::rules())->validate();
|
||||
|
||||
$tag = Tag::query()
|
||||
->where('project_id', $user->current_project_id)
|
||||
->where('name', $input['name'])
|
||||
->first();
|
||||
|
||||
if ($tag) {
|
||||
throw ValidationException::withMessages([
|
||||
'name' => ['Tag with this name already exists.'],
|
||||
@ -47,7 +51,7 @@ public static function rules(): array
|
||||
],
|
||||
'color' => [
|
||||
'required',
|
||||
Rule::in(config('core.tag_colors')),
|
||||
Rule::in(config('core.colors')),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace App\Actions\Tag;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class EditTag
|
||||
@ -12,6 +13,8 @@ class EditTag
|
||||
*/
|
||||
public function edit(Tag $tag, array $input): void
|
||||
{
|
||||
Validator::make($input, self::rules())->validate();
|
||||
|
||||
$tag->name = $input['name'];
|
||||
$tag->color = $input['color'];
|
||||
|
||||
@ -29,7 +32,7 @@ public static function rules(): array
|
||||
],
|
||||
'color' => [
|
||||
'required',
|
||||
Rule::in(config('core.tag_colors')),
|
||||
Rule::in(config('core.colors')),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
use App\Models\Server;
|
||||
use App\Models\Site;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class SyncTags
|
||||
@ -12,8 +13,10 @@ class SyncTags
|
||||
/**
|
||||
* @param array<string, mixed> $input
|
||||
*/
|
||||
public function sync(array $input): void
|
||||
public function sync(int $projectId, array $input): void
|
||||
{
|
||||
Validator::make($input, self::rules($projectId))->validate();
|
||||
|
||||
/** @var Server|Site $taggable */
|
||||
$taggable = $input['taggable_type']::findOrFail($input['taggable_id']);
|
||||
|
||||
|
64
app/Http/Controllers/TagController.php
Normal file
64
app/Http/Controllers/TagController.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Tag\CreateTag;
|
||||
use App\Actions\Tag\DeleteTag;
|
||||
use App\Actions\Tag\EditTag;
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Inertia;
|
||||
use Inertia\Response;
|
||||
use Spatie\RouteAttributes\Attributes\Delete;
|
||||
use Spatie\RouteAttributes\Attributes\Get;
|
||||
use Spatie\RouteAttributes\Attributes\Middleware;
|
||||
use Spatie\RouteAttributes\Attributes\Patch;
|
||||
use Spatie\RouteAttributes\Attributes\Post;
|
||||
use Spatie\RouteAttributes\Attributes\Prefix;
|
||||
|
||||
#[Prefix('settings/tags')]
|
||||
#[Middleware(['auth'])]
|
||||
class TagController extends Controller
|
||||
{
|
||||
#[Get('/', name: 'tags')]
|
||||
public function index(): Response
|
||||
{
|
||||
$this->authorize('viewAny', Tag::class);
|
||||
|
||||
return Inertia::render('tags/index', [
|
||||
'tags' => TagResource::collection(Tag::getByProjectId(user()->current_project_id)->simplePaginate(config('web.pagination_size'))),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Post('/', name: 'tags.store')]
|
||||
public function store(Request $request): RedirectResponse
|
||||
{
|
||||
$this->authorize('create', Tag::class);
|
||||
|
||||
app(CreateTag::class)->create(user(), $request->input());
|
||||
|
||||
return back()->with('success', 'Tag created.');
|
||||
}
|
||||
|
||||
#[Patch('/{tag}', name: 'tags.update')]
|
||||
public function update(Request $request, Tag $tag): RedirectResponse
|
||||
{
|
||||
$this->authorize('update', $tag);
|
||||
|
||||
app(EditTag::class)->edit($tag, $request->input());
|
||||
|
||||
return back()->with('success', 'Tag updated.');
|
||||
}
|
||||
|
||||
#[Delete('/{tag}', name: 'tags.destroy')]
|
||||
public function destroy(Tag $tag): RedirectResponse
|
||||
{
|
||||
$this->authorize('delete', $tag);
|
||||
|
||||
app(DeleteTag::class)->delete($tag);
|
||||
|
||||
return back()->with('success', 'Tag deleted.');
|
||||
}
|
||||
}
|
26
app/Http/Resources/TagResource.php
Normal file
26
app/Http/Resources/TagResource.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use App\Models\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin Tag */
|
||||
class TagResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'project_id' => $this->project_id,
|
||||
'name' => $this->name,
|
||||
'color' => $this->color,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Database\Factories\TagFactory;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@ -18,7 +19,7 @@
|
||||
*/
|
||||
class Tag extends AbstractModel
|
||||
{
|
||||
/** @use HasFactory<\Database\Factories\TagFactory> */
|
||||
/** @use HasFactory<TagFactory> */
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
|
Reference in New Issue
Block a user