-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
431da1b
commit 7f5e68e
Showing
47 changed files
with
1,380 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Actions\Tag; | ||
|
||
use App\Models\Server; | ||
use App\Models\Site; | ||
use App\Models\Tag; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
|
||
class AttachTag | ||
{ | ||
public function attach(User $user, array $input): Tag | ||
{ | ||
$this->validate($input); | ||
|
||
/** @var Server|Site $taggable */ | ||
$taggable = $input['taggable_type']::findOrFail($input['taggable_id']); | ||
|
||
$tag = Tag::query()->where('name', $input['name'])->first(); | ||
if ($tag) { | ||
if (! $taggable->tags->contains($tag->id)) { | ||
$taggable->tags()->attach($tag->id); | ||
} | ||
|
||
return $tag; | ||
} | ||
|
||
$tag = new Tag([ | ||
'project_id' => $user->currentProject->id, | ||
'name' => $input['name'], | ||
'color' => config('core.tag_colors')[array_rand(config('core.tag_colors'))], | ||
]); | ||
$tag->save(); | ||
|
||
$taggable->tags()->attach($tag->id); | ||
|
||
return $tag; | ||
} | ||
|
||
private function validate(array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => [ | ||
'required', | ||
], | ||
'taggable_id' => [ | ||
'required', | ||
'integer', | ||
], | ||
'taggable_type' => [ | ||
'required', | ||
Rule::in(config('core.taggable_types')), | ||
], | ||
])->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace App\Actions\Tag; | ||
|
||
use App\Models\Tag; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class CreateTag | ||
{ | ||
public function create(User $user, array $input): Tag | ||
{ | ||
$this->validate($input); | ||
|
||
$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.'], | ||
]); | ||
} | ||
|
||
$tag = new Tag([ | ||
'project_id' => $user->currentProject->id, | ||
'name' => $input['name'], | ||
'color' => $input['color'], | ||
]); | ||
$tag->save(); | ||
|
||
return $tag; | ||
} | ||
|
||
private function validate(array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'name' => [ | ||
'required', | ||
], | ||
'color' => [ | ||
'required', | ||
Rule::in(config('core.tag_colors')), | ||
], | ||
])->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace App\Actions\Tag; | ||
|
||
use App\Models\Tag; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class DeleteTag | ||
{ | ||
public function delete(Tag $tag): void | ||
{ | ||
DB::table('taggables')->where('tag_id', $tag->id)->delete(); | ||
$tag->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Actions\Tag; | ||
|
||
use App\Models\Server; | ||
use App\Models\Site; | ||
use App\Models\Tag; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
|
||
class DetachTag | ||
{ | ||
public function detach(Tag $tag, array $input): void | ||
{ | ||
$this->validate($input); | ||
|
||
/** @var Server|Site $taggable */ | ||
$taggable = $input['taggable_type']::findOrFail($input['taggable_id']); | ||
|
||
$taggable->tags()->detach($tag->id); | ||
} | ||
|
||
private function validate(array $input): void | ||
{ | ||
Validator::make($input, [ | ||
'taggable_id' => [ | ||
'required', | ||
'integer', | ||
], | ||
'taggable_type' => [ | ||
'required', | ||
Rule::in(config('core.taggable_types')), | ||
], | ||
])->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Actions\Tag; | ||
|
||
use App\Models\Tag; | ||
use Illuminate\Support\Facades\Validator; | ||
use Illuminate\Validation\Rule; | ||
use Illuminate\Validation\ValidationException; | ||
|
||
class EditTag | ||
{ | ||
public function edit(Tag $tag, array $input): void | ||
{ | ||
$this->validate($input); | ||
|
||
$tag->name = $input['name']; | ||
$tag->color = $input['color']; | ||
|
||
$tag->save(); | ||
} | ||
|
||
/** | ||
* @throws ValidationException | ||
*/ | ||
private function validate(array $input): void | ||
{ | ||
$rules = [ | ||
'name' => [ | ||
'required', | ||
], | ||
'color' => [ | ||
'required', | ||
Rule::in(config('core.tag_colors')), | ||
], | ||
]; | ||
Validator::make($input, $rules)->validate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Settings; | ||
|
||
use App\Actions\Tag\AttachTag; | ||
use App\Actions\Tag\CreateTag; | ||
use App\Actions\Tag\DeleteTag; | ||
use App\Actions\Tag\DetachTag; | ||
use App\Actions\Tag\EditTag; | ||
use App\Facades\Toast; | ||
use App\Helpers\HtmxResponse; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Tag; | ||
use App\Models\User; | ||
use Illuminate\Contracts\View\View; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
|
||
class TagController extends Controller | ||
{ | ||
public function index(Request $request): View | ||
{ | ||
$data = [ | ||
'tags' => Tag::getByProjectId(auth()->user()->current_project_id)->get(), | ||
]; | ||
|
||
if ($request->has('edit')) { | ||
$data['editTag'] = Tag::find($request->input('edit')); | ||
} | ||
|
||
return view('settings.tags.index', $data); | ||
} | ||
|
||
public function create(Request $request): HtmxResponse | ||
{ | ||
/** @var User $user */ | ||
$user = $request->user(); | ||
|
||
app(CreateTag::class)->create( | ||
$user, | ||
$request->input(), | ||
); | ||
|
||
Toast::success('Tag created.'); | ||
|
||
return htmx()->redirect(route('settings.tags')); | ||
} | ||
|
||
public function update(Tag $tag, Request $request): HtmxResponse | ||
{ | ||
app(EditTag::class)->edit( | ||
$tag, | ||
$request->input(), | ||
); | ||
|
||
Toast::success('Tag updated.'); | ||
|
||
return htmx()->redirect(route('settings.tags')); | ||
} | ||
|
||
public function attach(Request $request): RedirectResponse | ||
{ | ||
/** @var User $user */ | ||
$user = $request->user(); | ||
|
||
app(AttachTag::class)->attach($user, $request->input()); | ||
|
||
return back()->with([ | ||
'status' => 'tag-created', | ||
]); | ||
} | ||
|
||
public function detach(Request $request, Tag $tag): RedirectResponse | ||
{ | ||
app(DetachTag::class)->detach($tag, $request->input()); | ||
|
||
return back()->with([ | ||
'status' => 'tag-detached', | ||
]); | ||
} | ||
|
||
public function delete(Tag $tag): RedirectResponse | ||
{ | ||
app(DeleteTag::class)->delete($tag); | ||
|
||
Toast::success('Tag deleted.'); | ||
|
||
return back(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphToMany; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $project_id | ||
* @property string $name | ||
* @property string $color | ||
* @property Carbon $created_at | ||
* @property Carbon $updated_at | ||
*/ | ||
class Tag extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $fillable = [ | ||
'project_id', | ||
'name', | ||
'color', | ||
]; | ||
|
||
protected $casts = [ | ||
'project_id' => 'int', | ||
]; | ||
|
||
public function project(): BelongsTo | ||
{ | ||
return $this->belongsTo(Project::class); | ||
} | ||
|
||
public function servers(): MorphToMany | ||
{ | ||
return $this->morphedByMany(Server::class, 'taggable'); | ||
} | ||
|
||
public function sites(): MorphToMany | ||
{ | ||
return $this->morphedByMany(Site::class, 'taggable'); | ||
} | ||
|
||
public static function getByProjectId(int $projectId): Builder | ||
{ | ||
return self::query() | ||
->where('project_id', $projectId) | ||
->orWhereNull('project_id'); | ||
} | ||
} |
Oops, something went wrong.