Files
parsaaghayi-backend/routes/api.php
T
Parsa Aghayi 3f386c7be8 Add the admin content API the CMS needs
Two gaps blocked building the admin panel:

About-me writes could only set the image. Title and description live in the
JSON translations column and were unreachable from the API, so the homepage
bio could never be edited through the app. Accept title_fa/title_en/
description_fa/description_en on both About Me form requests and write them
per locale through setTranslation().

Editing also needs raw rows. The public project and about-me endpoints only
return display-oriented payloads - one translated title, reshaped image
arrays, no second locale - which is not enough to populate a form. Add
GET /api/admin/resources/{resource} behind the same auth:sanctum + admin
middleware as the write routes, returning stored rows including every locale,
plus a public GET /api/website/project-categories so project forms have a
category picker.

Covered by AdminResourceReadTest: anonymous and non-admin callers are
rejected for every resource, unknown resource names 404, and project rows
expose both locales.
2026-10-01 15:28:23 -04:00

105 lines
4.6 KiB
PHP

<?php
use App\Http\Controllers\Api\Admin\AdminResourceController;
use App\Http\Controllers\Api\AuthController;
use App\Http\Controllers\Api\Blog\CategoryController;
use App\Http\Controllers\Api\Blog\HomeController;
use App\Http\Controllers\Api\Blog\PostController;
use App\Http\Controllers\Api\Blog\TagController;
use App\Http\Controllers\Api\Website\AboutMeController;
use App\Http\Controllers\Api\Website\ProjectCategoryController;
use App\Http\Controllers\Api\Website\ProjectController;
use App\Http\Controllers\Api\Website\ServiceController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Authentication
|--------------------------------------------------------------------------
| Used by the admin panel. Login is throttled hard because it is the one
| endpoint that accepts attacker-controlled credentials.
*/
Route::post('auth/login', [AuthController::class, 'login'])
->middleware('throttle:5,1')
->name('auth.login');
Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
Route::get('auth/me', [AuthController::class, 'me'])->name('auth.me');
Route::post('auth/logout', [AuthController::class, 'logout'])->name('auth.logout');
});
/*
|--------------------------------------------------------------------------
| Public read API
|--------------------------------------------------------------------------
| Anonymous, cacheable, GET-only. Nothing here can change state.
*/
Route::middleware('throttle:60,1')->group(function () {
Route::prefix('website')->group(function () {
Route::get('about-me', [AboutMeController::class, 'index']);
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']);
Route::get('projects/translation/{locale}', [ProjectController::class, 'index']);
Route::get('projects/{id}/translation/{locale}', [ProjectController::class, 'show']);
});
Route::prefix('blog')->group(function () {
Route::get('homepage', [HomeController::class, 'index']);
Route::get('posts', [PostController::class, 'index']);
Route::get('posts/{slug}', [PostController::class, 'show']);
Route::get('categories', [CategoryController::class, 'index']);
Route::get('categories/{id}', [CategoryController::class, 'show']);
Route::get('categories/{slug}/posts', [CategoryController::class, 'getCategoryWithPosts']);
Route::get('tags', [TagController::class, 'index']);
Route::get('tags/{id}', [TagController::class, 'show']);
});
});
/*
|--------------------------------------------------------------------------
| Admin write API
|--------------------------------------------------------------------------
| Every mutating endpoint requires an authenticated admin. These routes were
| previously anonymous, which allowed anyone to create, edit or delete every
| post, category, tag, project and about-me record on the live site.
*/
Route::middleware(['auth:sanctum', 'admin', 'throttle:30,1'])->group(function () {
Route::prefix('website')->group(function () {
Route::post('about-me', [AboutMeController::class, 'store']);
Route::put('about-me/{id}', [AboutMeController::class, 'update']);
Route::delete('about-me/{id}', [AboutMeController::class, 'destroy']);
Route::post('projects', [ProjectController::class, 'store']);
Route::put('projects/{id}', [ProjectController::class, 'update']);
Route::delete('projects/{id}', [ProjectController::class, 'destroy']);
});
Route::prefix('blog')->group(function () {
Route::post('posts', [PostController::class, 'store']);
Route::put('posts/{id}', [PostController::class, 'update']);
Route::delete('posts/{id}', [PostController::class, 'destroy']);
Route::post('categories', [CategoryController::class, 'store']);
Route::put('categories/{id}', [CategoryController::class, 'update']);
Route::delete('categories/{id}', [CategoryController::class, 'destroy']);
Route::post('tags', [TagController::class, 'store']);
Route::put('tags/{id}', [TagController::class, 'update']);
Route::delete('tags/{id}', [TagController::class, 'destroy']);
});
Route::get('admin/resources/{resource}', [AdminResourceController::class, 'index']);
});