80 lines
2.4 KiB
PHP
80 lines
2.4 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;
|
|
|
|
class AboutMeController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$aboutMe = AboutMe::with('skills.translations')->first();
|
|
return response()->json($aboutMe);
|
|
}
|
|
|
|
public function show($id)
|
|
{
|
|
$aboutMe = AboutMe::with('skills.translations')->findOrFail($id);
|
|
return response()->json($aboutMe);
|
|
}
|
|
|
|
public function store(StoreAboutMeRequest $request)
|
|
{
|
|
$aboutMe = AboutMe::create($request->validated());
|
|
return response()->json($aboutMe, 201);
|
|
}
|
|
|
|
public function update(UpdateAboutMeRequest $request, $id)
|
|
{
|
|
$aboutMe = AboutMe::findOrFail($id);
|
|
$aboutMe->update($request->validated());
|
|
return response()->json($aboutMe);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
AboutMe::destroy($id);
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
public function getTranslation($id, $locale)
|
|
{
|
|
$aboutMe = AboutMe::with(['translations' => function ($query) use ($locale) {
|
|
$query->where('locale', $locale);
|
|
}, 'skills.translations' => function ($query) use ($locale) {
|
|
$query->where('locale', $locale);
|
|
}])->findOrFail($id);
|
|
|
|
if ($aboutMe->translations->isEmpty()) {
|
|
return response()->json(['message' => 'Translation not found', 'locale' => $locale], 404);
|
|
}
|
|
|
|
$translation = $aboutMe->translations->first();
|
|
$skills = $aboutMe->skills->map(function ($skill) use ($locale) {
|
|
$translation = $skill->getTranslation($locale);
|
|
|
|
return [
|
|
'id' => $skill->id,
|
|
'title' => $translation ? $translation->title : null,
|
|
'description' => $translation ? $translation->description : null,
|
|
'image' => $skill->image,
|
|
'link' => $skill->link,
|
|
'percentage' => $skill->percentage,
|
|
];
|
|
});
|
|
|
|
$data = [
|
|
'id' => $aboutMe->id,
|
|
'title' => $translation->title,
|
|
'description' => $translation->description,
|
|
'image' => $aboutMe->image,
|
|
'skills' => $skills,
|
|
];
|
|
|
|
return response()->json($data);
|
|
}
|
|
}
|