PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 5s
PR CI / security (pull_request) Failing after 42s
PR CI / test (lowest) (pull_request) Successful in 48s
PR CI / test (stable) (pull_request) Successful in 49s
PR CI / ui (pull_request) Successful in 53s
PR CI / pr-ci (pull_request) Failing after 1s
PR CI / release-automerge (pull_request) Skipped
81 lines
2.6 KiB
PHP
81 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Modules\Posts;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
/**
|
|
* The categories and tags of the posts: make one, rename one everywhere, merge two, or take one
|
|
* off every post.
|
|
*/
|
|
class LabelController
|
|
{
|
|
public function __construct(private readonly PostLabels $labels) {}
|
|
|
|
public function index(): JsonResponse
|
|
{
|
|
return response()->json($this->labels->overview());
|
|
}
|
|
|
|
/** A category or tag that no post uses yet; the post form offers it from then on. */
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
$type = $this->type($request);
|
|
$request->validate(['name' => $this->nameRules($type)], $this->messages());
|
|
|
|
$name = $request->string('name')->trim()->toString();
|
|
|
|
if (! $this->labels->create($type, $name)) {
|
|
return response()->json(['message' => $already = __('The :type ":name" already exists.', ['type' => __($type), 'name' => $name]), 'errors' => ['name' => [$already]]], 422);
|
|
}
|
|
|
|
return response()->json(['created' => $name], 201);
|
|
}
|
|
|
|
public function update(Request $request): JsonResponse
|
|
{
|
|
$type = $this->type($request);
|
|
$request->validate(['name' => ['required', 'string'], 'new_name' => $this->nameRules($type)], $this->messages());
|
|
|
|
$from = $request->string('name')->toString();
|
|
$to = $request->string('new_name')->trim()->toString();
|
|
|
|
return response()->json(['posts' => $this->labels->rename($type, $from, $to)]);
|
|
}
|
|
|
|
public function destroy(Request $request): JsonResponse
|
|
{
|
|
$type = $this->type($request);
|
|
$request->validate(['name' => ['required', 'string']]);
|
|
|
|
return response()->json(['posts' => $this->labels->remove($type, $request->string('name')->toString())]);
|
|
}
|
|
|
|
/**
|
|
* The same limits as the post form.
|
|
*
|
|
* @return list<mixed>
|
|
*/
|
|
private function nameRules(string $type): array
|
|
{
|
|
return ['required', 'string', 'max:'.($type === PostLabels::CATEGORY ? 60 : 40), ...($type === PostLabels::TAG ? ['not_regex:/,/'] : [])];
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
private function messages(): array
|
|
{
|
|
$comma = 'A tag cannot hold a comma: the post form separates tags with it.';
|
|
|
|
return ['name.not_regex' => $comma, 'new_name.not_regex' => $comma];
|
|
}
|
|
|
|
private function type(Request $request): string
|
|
{
|
|
$request->validate(['type' => ['required', Rule::in([PostLabels::CATEGORY, PostLabels::TAG])]]);
|
|
|
|
return $request->string('type')->toString();
|
|
}
|
|
}
|