diff --git a/app/Http/Controllers/Api/Admin/AdminResourceController.php b/app/Http/Controllers/Api/Admin/AdminResourceController.php new file mode 100644 index 0000000..1b19599 --- /dev/null +++ b/app/Http/Controllers/Api/Admin/AdminResourceController.php @@ -0,0 +1,67 @@ + + */ + private const RESOURCES = [ + 'posts' => Post::class, + 'categories' => Category::class, + 'tags' => Tag::class, + 'projects' => Project::class, + 'project-categories' => ProjectCategory::class, + 'about-me' => AboutMe::class, + ]; + + /** + * 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 + { + $model = self::RESOURCES[$resource] ?? null; + + if ($model === null) { + return response()->json(['message' => 'Unknown admin resource.'], 404); + } + + $rows = $model::query()->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'], + default => [], + }; + } +} \ No newline at end of file diff --git a/app/Http/Controllers/Api/Website/AboutMeController.php b/app/Http/Controllers/Api/Website/AboutMeController.php index 7aa654d..77b1b70 100644 --- a/app/Http/Controllers/Api/Website/AboutMeController.php +++ b/app/Http/Controllers/Api/Website/AboutMeController.php @@ -52,7 +52,9 @@ class AboutMeController extends Controller */ public function store(StoreAboutMeRequest $request) { - $aboutMe = AboutMe::create($request->validated()); + $aboutMe = AboutMe::create(['image' => $request->input('image')]); + $this->applyTranslations($aboutMe, $request); + return response()->json($aboutMe, 201); } @@ -69,10 +71,29 @@ class AboutMeController extends Controller public function update(UpdateAboutMeRequest $request, $id) { $aboutMe = AboutMe::findOrFail($id); - $aboutMe->update($request->validated()); + + if ($request->has('image')) { + $aboutMe->image = $request->input('image'); + } + + $this->applyTranslations($aboutMe, $request); + $aboutMe->save(); + return response()->json($aboutMe); } + private function applyTranslations(AboutMe $aboutMe, $request): void + { + foreach (['fa', 'en'] as $locale) { + foreach (['title', 'description'] as $key) { + $field = $key.'_'.$locale; + if ($request->has($field)) { + $aboutMe->setTranslation($key, $locale, $request->input($field)); + } + } + } + } + /** * @OA\Delete( * path="/website/about-me/{id}", diff --git a/app/Http/Controllers/Api/Website/ProjectCategoryController.php b/app/Http/Controllers/Api/Website/ProjectCategoryController.php new file mode 100644 index 0000000..5995595 --- /dev/null +++ b/app/Http/Controllers/Api/Website/ProjectCategoryController.php @@ -0,0 +1,28 @@ +query('locale', 'fa'); + + $categories = ProjectCategory::query()->get(); + + return response()->json([ + 'data' => $categories->map(function (ProjectCategory $category) use ($locale) { + return [ + 'id' => $category->id, + 'title' => $category->getTranslation('title', $locale), + 'description' => $category->getTranslation('description', $locale), + 'image' => $category->image, + ]; + }), + ]); + } +} \ No newline at end of file diff --git a/app/Http/Requests/Website/StoreAboutMeRequest.php b/app/Http/Requests/Website/StoreAboutMeRequest.php index cccef8b..ed8f298 100644 --- a/app/Http/Requests/Website/StoreAboutMeRequest.php +++ b/app/Http/Requests/Website/StoreAboutMeRequest.php @@ -15,6 +15,10 @@ class StoreAboutMeRequest extends FormRequest { return [ 'image' => 'required|string|max:255', + 'title_fa' => 'nullable|string|max:255', + 'title_en' => 'nullable|string|max:255', + 'description_fa' => 'nullable|string', + 'description_en' => 'nullable|string', ]; } -} +} \ No newline at end of file diff --git a/app/Http/Requests/Website/UpdateAboutMeRequest.php b/app/Http/Requests/Website/UpdateAboutMeRequest.php index 76efea8..8eba099 100644 --- a/app/Http/Requests/Website/UpdateAboutMeRequest.php +++ b/app/Http/Requests/Website/UpdateAboutMeRequest.php @@ -15,6 +15,10 @@ class UpdateAboutMeRequest extends FormRequest { return [ 'image' => 'sometimes|required|string|max:255', + 'title_fa' => 'nullable|string|max:255', + 'title_en' => 'nullable|string|max:255', + 'description_fa' => 'nullable|string', + 'description_en' => 'nullable|string', ]; } -} +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index da42d3c..b195fd7 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,11 +1,13 @@ group(function () { Route::get('about-me/{id}', [AboutMeController::class, 'show']); Route::get('about-me/{id}/translation/{locale}', [AboutMeController::class, 'getTranslation']); + Route::get('project-categories', [ProjectCategoryController::class, 'index']); + Route::get('services/{locale}', [ServiceController::class, 'index']); Route::get('services/{id}/translation/{locale}', [ServiceController::class, 'showTranslation']); @@ -95,4 +99,6 @@ Route::middleware(['auth:sanctum', 'admin', 'throttle:30,1'])->group(function () Route::put('tags/{id}', [TagController::class, 'update']); Route::delete('tags/{id}', [TagController::class, 'destroy']); }); + + Route::get('admin/resources/{resource}', [AdminResourceController::class, 'index']); }); diff --git a/tests/Feature/AdminResourceReadTest.php b/tests/Feature/AdminResourceReadTest.php new file mode 100644 index 0000000..2af7037 --- /dev/null +++ b/tests/Feature/AdminResourceReadTest.php @@ -0,0 +1,87 @@ + + */ + public static function resourceProvider(): array + { + return [ + 'posts' => ['posts'], + 'categories' => ['categories'], + 'tags' => ['tags'], + 'projects' => ['projects'], + 'project categories' => ['project-categories'], + 'about me' => ['about-me'], + ]; + } + + #[DataProvider('resourceProvider')] + public function test_anonymous_requests_cannot_read_admin_resources(string $resource): void + { + $this->getJson("/api/admin/resources/{$resource}")->assertUnauthorized(); + } + + #[DataProvider('resourceProvider')] + public function test_non_admins_cannot_read_admin_resources(string $resource): void + { + Sanctum::actingAs(User::factory()->create()); + + $this->getJson("/api/admin/resources/{$resource}")->assertForbidden(); + } + + #[DataProvider('resourceProvider')] + public function test_admins_can_read_admin_resources(string $resource): void + { + Sanctum::actingAs(User::factory()->create(['is_admin' => true])); + + $this->getJson("/api/admin/resources/{$resource}") + ->assertOk() + ->assertJsonStructure(['data']); + } + + public function test_unknown_resources_are_rejected(): void + { + Sanctum::actingAs(User::factory()->create(['is_admin' => true])); + + $this->getJson('/api/admin/resources/users')->assertNotFound(); + $this->getJson('/api/admin/resources/personal_access_tokens')->assertNotFound(); + } + + public function test_project_rows_expose_every_locale(): void + { + Sanctum::actingAs(User::factory()->create(['is_admin' => true])); + + $project = Project::factory()->create([ + 'image1' => 'a.png', + 'image2' => 'b.png', + 'translations' => [ + 'title' => ['fa' => 'عنوان', 'en' => 'Title'], + 'description' => ['fa' => 'توضیح', 'en' => 'Description'], + ], + ]); + + $this->getJson('/api/admin/resources/projects') + ->assertOk() + ->assertJsonPath("data.0.id", $project->id) + ->assertJsonPath('data.0.image1', 'a.png') + ->assertJsonPath('data.0.translations.title.fa', 'عنوان') + ->assertJsonPath('data.0.translations.title.en', 'Title'); + } +} \ No newline at end of file