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:
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 => [],
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreServiceRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// website_services.imageSrc is NOT NULL, so leaving it out has to be
|
||||
// a 422 rather than a 500 from the database constraint.
|
||||
'imageSrc' => 'required|string|max:255',
|
||||
'translations' => 'required|array',
|
||||
'translations.title' => 'required|array',
|
||||
'translations.title.fa' => 'required|string|max:255',
|
||||
'translations.title.en' => 'nullable|string|max:255',
|
||||
'translations.description' => 'required|array',
|
||||
'translations.description.fa' => 'required|string',
|
||||
'translations.description.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreSkillRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// image, percentage and about_me_id are all NOT NULL on
|
||||
// website_skills, so they are required here to keep a bad payload a
|
||||
// 422 instead of a 500. about_me_id is also a foreign key with
|
||||
// ON DELETE CASCADE, hence exists:.
|
||||
'image' => 'required|string|max:255',
|
||||
'link' => 'nullable|string|max:255',
|
||||
'percentage' => 'required|integer|min:0|max:100',
|
||||
'about_me_id' => 'required|exists:website_about_me,id',
|
||||
'translations' => 'required|array',
|
||||
'translations.title' => 'required|array',
|
||||
'translations.title.fa' => 'required|string|max:255',
|
||||
'translations.title.en' => 'nullable|string|max:255',
|
||||
'translations.description' => 'required|array',
|
||||
'translations.description.fa' => 'required|string',
|
||||
'translations.description.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreTestimonialRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
// Both columns are NOT NULL on website_testimonials.
|
||||
'image' => 'required|string|max:255',
|
||||
'client_link' => 'required|string|max:255',
|
||||
'translations' => 'required|array',
|
||||
'translations.client_name' => 'required|array',
|
||||
'translations.client_name.fa' => 'required|string|max:255',
|
||||
'translations.client_name.en' => 'nullable|string|max:255',
|
||||
'translations.client_position' => 'required|array',
|
||||
'translations.client_position.fa' => 'required|string|max:255',
|
||||
'translations.client_position.en' => 'nullable|string|max:255',
|
||||
'translations.feedback' => 'required|array',
|
||||
'translations.feedback.fa' => 'required|string',
|
||||
'translations.feedback.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateServiceRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'imageSrc' => 'nullable|string|max:255',
|
||||
'translations' => 'nullable|array',
|
||||
'translations.title' => 'nullable|array',
|
||||
'translations.title.fa' => 'nullable|string|max:255',
|
||||
'translations.title.en' => 'nullable|string|max:255',
|
||||
'translations.description' => 'nullable|array',
|
||||
'translations.description.fa' => 'nullable|string',
|
||||
'translations.description.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateSkillRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'nullable|string|max:255',
|
||||
'link' => 'nullable|string|max:255',
|
||||
'percentage' => 'nullable|integer|min:0|max:100',
|
||||
'about_me_id' => 'nullable|exists:website_about_me,id',
|
||||
'translations' => 'nullable|array',
|
||||
'translations.title' => 'nullable|array',
|
||||
'translations.title.fa' => 'nullable|string|max:255',
|
||||
'translations.title.en' => 'nullable|string|max:255',
|
||||
'translations.description' => 'nullable|array',
|
||||
'translations.description.fa' => 'nullable|string',
|
||||
'translations.description.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Admin;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class UpdateTestimonialRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'image' => 'nullable|string|max:255',
|
||||
'client_link' => 'nullable|string|max:255',
|
||||
'translations' => 'nullable|array',
|
||||
'translations.client_name' => 'nullable|array',
|
||||
'translations.client_name.fa' => 'nullable|string|max:255',
|
||||
'translations.client_name.en' => 'nullable|string|max:255',
|
||||
'translations.client_position' => 'nullable|array',
|
||||
'translations.client_position.fa' => 'nullable|string|max:255',
|
||||
'translations.client_position.en' => 'nullable|string|max:255',
|
||||
'translations.feedback' => 'nullable|array',
|
||||
'translations.feedback.fa' => 'nullable|string',
|
||||
'translations.feedback.en' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user