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.
153 lines
4.9 KiB
PHP
153 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Website;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Website\StoreAboutMeRequest;
|
|
use App\Http\Requests\Website\UpdateAboutMeRequest;
|
|
use App\Models\Website\AboutMe;
|
|
|
|
/**
|
|
* @OA\Tag(name="About Me", description="About Me section endpoints")
|
|
*/
|
|
class AboutMeController extends Controller
|
|
{
|
|
/**
|
|
* @OA\Get(
|
|
* path="/website/about-me",
|
|
* tags={"About Me"},
|
|
* summary="Get the first About Me entry with skills",
|
|
* @OA\Response(response=200, description="OK", @OA\JsonContent(ref="#/components/schemas/AboutMe"))
|
|
* )
|
|
*/
|
|
public function index()
|
|
{
|
|
$aboutMe = AboutMe::with('skills')->first();
|
|
return response()->json($aboutMe);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/website/about-me/{id}",
|
|
* tags={"About Me"},
|
|
* summary="Get a specific About Me entry",
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$aboutMe = AboutMe::with('skills')->findOrFail($id);
|
|
return response()->json($aboutMe);
|
|
}
|
|
|
|
/**
|
|
* @OA\Post(
|
|
* path="/website/about-me",
|
|
* tags={"About Me"},
|
|
* summary="Create an About Me entry",
|
|
* @OA\RequestBody(@OA\MediaType(mediaType="application/json")),
|
|
* @OA\Response(response=201, description="Created")
|
|
* )
|
|
*/
|
|
public function store(StoreAboutMeRequest $request)
|
|
{
|
|
$aboutMe = AboutMe::create(['image' => $request->input('image')]);
|
|
$this->applyTranslations($aboutMe, $request);
|
|
|
|
return response()->json($aboutMe, 201);
|
|
}
|
|
|
|
/**
|
|
* @OA\Put(
|
|
* path="/website/about-me/{id}",
|
|
* tags={"About Me"},
|
|
* summary="Update an About Me entry",
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
* @OA\RequestBody(@OA\MediaType(mediaType="application/json")),
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function update(UpdateAboutMeRequest $request, $id)
|
|
{
|
|
$aboutMe = AboutMe::findOrFail($id);
|
|
|
|
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}",
|
|
* tags={"About Me"},
|
|
* summary="Delete an About Me entry",
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
* @OA\Response(response=204, description="No Content")
|
|
* )
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
AboutMe::destroy($id);
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/website/about-me/{id}/translation/{locale}",
|
|
* tags={"About Me"},
|
|
* summary="Get localized About Me with skills",
|
|
* @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")),
|
|
* @OA\Parameter(name="locale", in="path", required=true, @OA\Schema(type="string", enum={"fa","en"})),
|
|
* @OA\Response(response=200, description="OK", @OA\JsonContent(ref="#/components/schemas/AboutMe")),
|
|
* @OA\Response(response=404, description="Translation not found")
|
|
* )
|
|
*/
|
|
public function getTranslation($id, $locale)
|
|
{
|
|
$aboutMe = AboutMe::with('skills')->findOrFail($id);
|
|
|
|
if (!$aboutMe->hasTranslation('title', $locale)) {
|
|
return response()->json(['message' => 'Translation not found', 'locale' => $locale], 404);
|
|
}
|
|
|
|
$skills = $aboutMe->skills->map(function ($skill) use ($locale) {
|
|
return [
|
|
'id' => $skill->id,
|
|
'title' => $skill->getTranslation('title', $locale),
|
|
'description' => $skill->getTranslation('description', $locale),
|
|
'image' => $skill->image,
|
|
'link' => $skill->link,
|
|
'percentage' => $skill->percentage,
|
|
];
|
|
});
|
|
|
|
$data = [
|
|
'id' => $aboutMe->id,
|
|
'title' => $aboutMe->getTranslation('title', $locale),
|
|
'description' => $aboutMe->getTranslation('description', $locale),
|
|
'image' => $aboutMe->image,
|
|
'skills' => $skills,
|
|
];
|
|
|
|
return response()->json($data);
|
|
}
|
|
}
|