Let the admin panel manage services, skills and testimonials

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.
This commit is contained in:
Parsa Aghayi committed 2026-10-01 18:19:32 -04:00
1 parent 6b03c0fbc3
commit 99f786265e
13 files changed
+1106 -2

No files matched your search

@@ -0,0 +1,259 @@
<?php
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\StoreServiceRequest;
use App\Http\Requests\Admin\StoreSkillRequest;
use App\Http\Requests\Admin\StoreTestimonialRequest;
use App\Http\Requests\Admin\UpdateServiceRequest;
use App\Http\Requests\Admin\UpdateSkillRequest;
use App\Http\Requests\Admin\UpdateTestimonialRequest;
use App\Models\Website\Service;
use App\Models\Website\Skill;
use App\Models\Website\Testimonial;
use App\Models\Website\TestimonialTranslation;
class AdminCrudController extends Controller
{
public static function resources(): array
{
return [
'services' => Service::class,
'skills' => Skill::class,
'testimonials' => Testimonial::class,
];
}
public function storeService(StoreServiceRequest $request)
{
$service = Service::create([
'imageSrc' => $request->input('imageSrc'),
'translations' => [],
]);
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['title']['fa'])) {
$service->setTranslation('title', 'fa', $translations['title']['fa']);
}
if (isset($translations['title']['en'])) {
$service->setTranslation('title', 'en', $translations['title']['en']);
}
if (isset($translations['description']['fa'])) {
$service->setTranslation('description', 'fa', $translations['description']['fa']);
}
if (isset($translations['description']['en'])) {
$service->setTranslation('description', 'en', $translations['description']['en']);
}
}
$service->save();
return response()->json($service, 201);
}
public function updateService(UpdateServiceRequest $request, $id)
{
$service = Service::findOrFail($id);
if ($request->has('imageSrc')) {
$service->imageSrc = $request->input('imageSrc');
}
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['title']['fa'])) {
$service->setTranslation('title', 'fa', $translations['title']['fa']);
}
if (isset($translations['title']['en'])) {
$service->setTranslation('title', 'en', $translations['title']['en']);
}
if (isset($translations['description']['fa'])) {
$service->setTranslation('description', 'fa', $translations['description']['fa']);
}
if (isset($translations['description']['en'])) {
$service->setTranslation('description', 'en', $translations['description']['en']);
}
}
$service->save();
return response()->json($service);
}
public function destroyService($id)
{
$service = Service::findOrFail($id);
$service->delete();
return response()->json(null, 200);
}
public function storeSkill(StoreSkillRequest $request)
{
$skill = Skill::create([
'image' => $request->input('image'),
'link' => $request->input('link'),
'percentage' => $request->input('percentage'),
'about_me_id' => $request->input('about_me_id'),
'translations' => [],
]);
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['title']['fa'])) {
$skill->setTranslation('title', 'fa', $translations['title']['fa']);
}
if (isset($translations['title']['en'])) {
$skill->setTranslation('title', 'en', $translations['title']['en']);
}
if (isset($translations['description']['fa'])) {
$skill->setTranslation('description', 'fa', $translations['description']['fa']);
}
if (isset($translations['description']['en'])) {
$skill->setTranslation('description', 'en', $translations['description']['en']);
}
}
$skill->save();
return response()->json($skill, 201);
}
public function updateSkill(UpdateSkillRequest $request, $id)
{
$skill = Skill::findOrFail($id);
if ($request->has('image')) {
$skill->image = $request->input('image');
}
if ($request->has('link')) {
$skill->link = $request->input('link');
}
if ($request->has('percentage')) {
$skill->percentage = $request->input('percentage');
}
if ($request->has('about_me_id')) {
$skill->about_me_id = $request->input('about_me_id');
}
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['title']['fa'])) {
$skill->setTranslation('title', 'fa', $translations['title']['fa']);
}
if (isset($translations['title']['en'])) {
$skill->setTranslation('title', 'en', $translations['title']['en']);
}
if (isset($translations['description']['fa'])) {
$skill->setTranslation('description', 'fa', $translations['description']['fa']);
}
if (isset($translations['description']['en'])) {
$skill->setTranslation('description', 'en', $translations['description']['en']);
}
}
$skill->save();
return response()->json($skill);
}
public function destroySkill($id)
{
$skill = Skill::findOrFail($id);
$skill->delete();
return response()->json(null, 200);
}
public function storeTestimonial(StoreTestimonialRequest $request)
{
$testimonial = Testimonial::create([
'image' => $request->input('image'),
'client_link' => $request->input('client_link'),
]);
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['client_name']['fa'])) {
TestimonialTranslation::create([
'testimonial_id' => $testimonial->id,
'locale' => 'fa',
'client_name' => $translations['client_name']['fa'],
'client_position' => $translations['client_position']['fa'] ?? '',
'feedback' => $translations['feedback']['fa'] ?? '',
]);
}
if (isset($translations['client_name']['en'])) {
TestimonialTranslation::create([
'testimonial_id' => $testimonial->id,
'locale' => 'en',
'client_name' => $translations['client_name']['en'],
'client_position' => $translations['client_position']['en'] ?? '',
'feedback' => $translations['feedback']['en'] ?? '',
]);
}
}
$testimonial->load('translations');
return response()->json($testimonial, 201);
}
public function updateTestimonial(UpdateTestimonialRequest $request, $id)
{
$testimonial = Testimonial::findOrFail($id);
if ($request->has('image')) {
$testimonial->image = $request->input('image');
}
if ($request->has('client_link')) {
$testimonial->client_link = $request->input('client_link');
}
$testimonial->save();
if ($request->has('translations')) {
$translations = $request->input('translations');
if (isset($translations['client_name']['fa'])) {
TestimonialTranslation::updateOrCreate(
['testimonial_id' => $testimonial->id, 'locale' => 'fa'],
[
'client_name' => $translations['client_name']['fa'],
'client_position' => $translations['client_position']['fa'] ?? '',
'feedback' => $translations['feedback']['fa'] ?? '',
]
);
}
if (isset($translations['client_name']['en'])) {
TestimonialTranslation::updateOrCreate(
['testimonial_id' => $testimonial->id, 'locale' => 'en'],
[
'client_name' => $translations['client_name']['en'],
'client_position' => $translations['client_position']['en'] ?? '',
'feedback' => $translations['feedback']['en'] ?? '',
]
);
}
}
$testimonial->load('translations');
return response()->json($testimonial);
}
public function destroyTestimonial($id)
{
$testimonial = Testimonial::findOrFail($id);
$testimonial->translations()->delete();
$testimonial->delete();
return response()->json(null, 200);
}
}
@@ -37,6 +37,8 @@ class AdminResourceController extends Controller
private const RELATIONS = [
'posts' => ['tags'],
'projects' => ['category'],
'skills' => ['aboutMe'],
'testimonials' => ['translations'],
];
/**
@@ -49,7 +51,9 @@ class AdminResourceController extends Controller
*/
public function index(Request $request, string $resource): JsonResponse
{
$model = self::RESOURCES[$resource] ?? null;
$resources = array_merge(self::RESOURCES, AdminCrudController::resources());
$model = $resources[$resource] ?? null;
if ($model === null) {
return response()->json(['message' => 'Unknown admin resource.'], 404);
@@ -72,7 +76,10 @@ class AdminResourceController extends Controller
'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 => [],
};
}
}
}