These three had read endpoints or nothing at all, so their content could only be changed by hand in the database. All writes are admin-only and rate limited like the existing admin routes. Testimonials are the awkward one: the migration that collapsed the legacy per-locale tables into JSON columns deliberately skipped them, so they still live in a child table. Testimonial therefore exposes a translations accessor to present the same translations[key][locale] shape as Service and Skill, which is what the generic resource form binds to. Reading the child relation has to go through getRelation(), since a plain $this->translations inside an accessor of that name re-enters the accessor and silently yields nulls. The store requests now mark the NOT NULL columns as required. Leaving them nullable let a bad payload reach the database and come back as a 500 instead of a 422, and about_me_id is additionally a cascading foreign key. Also registers the three resources with the admin read endpoint so the panel can list them, eager loading the relations their forms need.
86 lines
3.0 KiB
PHP
86 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Blog\Category;
|
|
use App\Models\Blog\Post;
|
|
use App\Models\Blog\Tag;
|
|
use App\Models\Website\AboutMe;
|
|
use App\Models\Website\Project;
|
|
use App\Models\Website\ProjectCategory;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class AdminResourceController extends Controller
|
|
{
|
|
/**
|
|
* Resources the admin panel may read and manage.
|
|
*
|
|
* @var array<string, class-string>
|
|
*/
|
|
private const RESOURCES = [
|
|
'posts' => Post::class,
|
|
'categories' => Category::class,
|
|
'tags' => Tag::class,
|
|
'projects' => Project::class,
|
|
'project-categories' => ProjectCategory::class,
|
|
'about-me' => AboutMe::class,
|
|
];
|
|
|
|
/**
|
|
* Relations eager loaded per resource, so an editing form is populated
|
|
* from a single request instead of a fan-out of lookups.
|
|
*
|
|
* @var array<string, array<int, string>>
|
|
*/
|
|
private const RELATIONS = [
|
|
'posts' => ['tags'],
|
|
'projects' => ['category'],
|
|
'skills' => ['aboutMe'],
|
|
'testimonials' => ['translations'],
|
|
];
|
|
|
|
/**
|
|
* Raw rows for content management.
|
|
*
|
|
* The public API intentionally returns display-oriented payloads: single
|
|
* translated values, reshaped image arrays, no internal ids for some
|
|
* fields. An editing form needs the stored row instead, including both
|
|
* locales, so this endpoint serialises the model as-is.
|
|
*/
|
|
public function index(Request $request, string $resource): JsonResponse
|
|
{
|
|
$resources = array_merge(self::RESOURCES, AdminCrudController::resources());
|
|
|
|
$model = $resources[$resource] ?? null;
|
|
|
|
if ($model === null) {
|
|
return response()->json(['message' => 'Unknown admin resource.'], 404);
|
|
}
|
|
|
|
$rows = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id')->get();
|
|
|
|
return response()->json(['data' => $rows]);
|
|
}
|
|
|
|
/**
|
|
* Field names accepted per resource, so the admin panel and the write
|
|
* endpoints cannot drift apart silently.
|
|
*/
|
|
public static function writableFields(string $resource): array
|
|
{
|
|
return match ($resource) {
|
|
'posts' => ['title', 'body', 'excerpt', 'slug', 'author_id', 'category_id', 'featured_image', 'views', 'likes', 'published_at', 'tags'],
|
|
'categories' => ['name', 'slug', 'description', 'short_description', 'image'],
|
|
'tags' => ['name', 'slug'],
|
|
'projects' => ['category_id', 'image1', 'image2', 'title_fa', 'title_en', 'description_fa', 'description_en'],
|
|
'about-me' => ['image', 'title_fa', 'title_en', 'description_fa', 'description_en'],
|
|
'services' => ['imageSrc', 'translations'],
|
|
'skills' => ['image', 'link', 'percentage', 'about_me_id', 'translations'],
|
|
'testimonials' => ['image', 'client_link', 'translations'],
|
|
default => [],
|
|
};
|
|
}
|
|
}
|