98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Blog\Category;
|
|
use App\Models\Blog\Comment;
|
|
use App\Models\Blog\Post;
|
|
use App\Models\Blog\Tag;
|
|
use App\Models\NewsletterSubscription;
|
|
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,
|
|
'comments' => Comment::class,
|
|
'newsletter-subscriptions' => NewsletterSubscription::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'],
|
|
'comments' => ['post'],
|
|
];
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
$query = $model::query()->with(self::RELATIONS[$resource] ?? [])->orderByDesc('id');
|
|
|
|
if ($request->has('per_page') && $request->integer('per_page') > 0) {
|
|
return response()->json($query->paginate(min($request->integer('per_page'), 100)));
|
|
}
|
|
|
|
$limit = min($request->integer('limit', 200), 500);
|
|
$rows = $query->limit($limit)->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 => [],
|
|
};
|
|
}
|
|
}
|