Tag blog posts and sign them with a member
Tags are named in every language and made from the page form; the byline is the member who signs a post on the site. The public API sends both, filters posts by tag and lists the tags in use. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -17,6 +17,8 @@ use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
||||
|
||||
class PageController extends Controller
|
||||
{
|
||||
protected const WITH = ['category', 'author', 'featuredMedia', 'tags'];
|
||||
|
||||
/**
|
||||
* GET /api/v1/pages
|
||||
*
|
||||
@@ -30,7 +32,7 @@ class PageController extends Controller
|
||||
$locale = app()->getLocale();
|
||||
|
||||
$pages = Page::query()
|
||||
->with(['category', 'author', 'featuredMedia'])
|
||||
->with(self::WITH)
|
||||
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')))
|
||||
->when($request->filled('category_id'), fn ($q) => $q->where('category_id', $request->integer('category_id')))
|
||||
->when($request->has('is_featured'), fn ($q) => $q->where('is_featured', $request->boolean('is_featured')))
|
||||
@@ -57,7 +59,7 @@ class PageController extends Controller
|
||||
$this->authorize('create', Page::class);
|
||||
|
||||
$page = new Page;
|
||||
$page->fill(TranslatablePayload::merge($page, $request->validated(), $page->translatable));
|
||||
$page->fill(TranslatablePayload::merge($page, collect($request->validated())->except('tag_ids')->all(), $page->translatable));
|
||||
$page->author_id ??= $request->user()->id;
|
||||
|
||||
// A page created as published with no explicit date goes live now.
|
||||
@@ -66,8 +68,9 @@ class PageController extends Controller
|
||||
}
|
||||
|
||||
$page->save();
|
||||
$this->syncTags($page, $request);
|
||||
|
||||
return PageResource::make($page->load(['category', 'author', 'featuredMedia']))
|
||||
return PageResource::make($page->load(self::WITH))
|
||||
->response()
|
||||
->setStatusCode(201);
|
||||
}
|
||||
@@ -76,22 +79,23 @@ class PageController extends Controller
|
||||
{
|
||||
$this->authorize('view', $page);
|
||||
|
||||
return PageResource::make($page->load(['category', 'author', 'featuredMedia']));
|
||||
return PageResource::make($page->load(self::WITH));
|
||||
}
|
||||
|
||||
public function update(PageRequest $request, Page $page): PageResource
|
||||
{
|
||||
$this->authorize('update', $page);
|
||||
|
||||
$page->fill(TranslatablePayload::merge($page, $request->validated(), $page->translatable));
|
||||
$page->fill(TranslatablePayload::merge($page, collect($request->validated())->except('tag_ids')->all(), $page->translatable));
|
||||
|
||||
if ($page->status === PageStatus::Published && $page->published_at === null) {
|
||||
$page->published_at = now();
|
||||
}
|
||||
|
||||
$page->save();
|
||||
$this->syncTags($page, $request);
|
||||
|
||||
return PageResource::make($page->load(['category', 'author', 'featuredMedia']));
|
||||
return PageResource::make($page->load(self::WITH));
|
||||
}
|
||||
|
||||
public function destroy(Page $page): JsonResponse
|
||||
@@ -109,7 +113,7 @@ class PageController extends Controller
|
||||
|
||||
$page->restore();
|
||||
|
||||
return PageResource::make($page->load(['category', 'author', 'featuredMedia']));
|
||||
return PageResource::make($page->load(self::WITH));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,4 +139,11 @@ class PageController extends Controller
|
||||
|
||||
$query->orderBy(in_array($column, $sortable, true) ? $column : 'created_at', $direction);
|
||||
}
|
||||
|
||||
protected function syncTags(Page $page, PageRequest $request): void
|
||||
{
|
||||
if ($request->has('tag_ids')) {
|
||||
$page->tags()->sync($request->validated('tag_ids') ?? []);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
82
app/Http/Controllers/Api/V1/TagController.php
Normal file
82
app/Http/Controllers/Api/V1/TagController.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Http\Controllers\Api\V1;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Tag;
|
||||
use App\Rules\Translatable;
|
||||
use App\Rules\UniqueTranslatedSlug;
|
||||
use App\Support\TranslatablePayload;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Blog tags. Small enough that the whole list is one answer.
|
||||
*/
|
||||
class TagController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorize('viewAny', Tag::class);
|
||||
|
||||
return response()->json([
|
||||
'data' => Tag::query()->withCount('pages')->orderBy('id')->get()->map(fn (Tag $tag) => $this->present($tag))->values(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$this->authorize('create', Tag::class);
|
||||
|
||||
$tag = new Tag;
|
||||
$tag->fill(TranslatablePayload::merge($tag, $this->validated($request, null), $tag->translatable))->save();
|
||||
|
||||
return response()->json(['data' => $this->present($tag->loadCount('pages'))], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Tag $tag): JsonResponse
|
||||
{
|
||||
$this->authorize('update', $tag);
|
||||
|
||||
$tag->fill(TranslatablePayload::merge($tag, $this->validated($request, $tag), $tag->translatable))->save();
|
||||
|
||||
return response()->json(['data' => $this->present($tag->loadCount('pages'))]);
|
||||
}
|
||||
|
||||
public function destroy(Tag $tag): JsonResponse
|
||||
{
|
||||
$this->authorize('delete', $tag);
|
||||
|
||||
$tag->delete();
|
||||
|
||||
return response()->json(status: 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function validated(Request $request, ?Tag $tag): array
|
||||
{
|
||||
$sometimes = $tag === null ? 'required' : 'sometimes';
|
||||
|
||||
return $request->validate([
|
||||
'name' => [$sometimes, new Translatable(requireDefaultLocale: $tag === null, max: 80)],
|
||||
'slug' => [$sometimes, new Translatable(requireDefaultLocale: $tag === null, max: 80), new UniqueTranslatedSlug(Tag::class, $tag?->id)],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function present(Tag $tag): array
|
||||
{
|
||||
return [
|
||||
'id' => $tag->id,
|
||||
'name' => (object) $tag->getTranslations('name'),
|
||||
'slug' => (object) $tag->getTranslations('slug'),
|
||||
'pages_count' => (int) ($tag->pages_count ?? 0),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user