1.2: restructure controllers to Api/ prefix and update routes
Deploy Backend / deploy (push) Has been cancelled
Deploy Backend / deploy (push) Has been cancelled
This commit is contained in:
1 parent
c740b69675
commit
299a98d506
11 files changed
+137
-271
No files matched your search
@@ -0,0 +1,79 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Website;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Website\StoreProjectRequest;
|
||||
use App\Http\Requests\Website\UpdateProjectRequest;
|
||||
use App\Models\Website\Project;
|
||||
use App\Models\Website\ProjectCategory;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function index($locale): \Illuminate\Http\JsonResponse
|
||||
{
|
||||
$perPage = request()->query('per_page', 10);
|
||||
|
||||
$projects = Project::with('translations', 'category.translations')->paginate($perPage);
|
||||
$formattedProjects = $projects->items();
|
||||
|
||||
$formattedProjects = array_map(function ($project) use ($locale) {
|
||||
$translation = $project->translations->firstWhere('locale', $locale);
|
||||
$categoryTranslation = $project->category->translations->firstWhere('locale', $locale);
|
||||
|
||||
return [
|
||||
'id' => $project->id,
|
||||
'title' => $translation ? $translation->title : null,
|
||||
'category' => $categoryTranslation ? $categoryTranslation->title : null,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
],
|
||||
'created_at' => $project->created_at,
|
||||
'updated_at' => $project->updated_at,
|
||||
];
|
||||
}, $formattedProjects);
|
||||
|
||||
return response()->json([
|
||||
'projects' => [
|
||||
'current_page' => $projects->currentPage(),
|
||||
'data' => $formattedProjects,
|
||||
'total' => $projects->total(),
|
||||
'last_page' => $projects->lastPage(),
|
||||
],
|
||||
'categories' => ProjectCategory::all()->map(function ($category) use ($locale) {
|
||||
return [
|
||||
'id' => $category->id,
|
||||
'title' => $category->translations->firstWhere('locale', $locale)->title ?? null,
|
||||
'image' => $category->image,
|
||||
];
|
||||
}),
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(StoreProjectRequest $request)
|
||||
{
|
||||
$project = Project::create($request->only(['category_id', 'image1', 'image2']));
|
||||
|
||||
foreach (['fa', 'en'] as $locale) {
|
||||
$project->translations()->create([
|
||||
'locale' => $locale,
|
||||
'title' => $request->input("title_$locale"),
|
||||
'description' => $request->input("description_$locale"),
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json($project->load('translations'), 201);
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$project = Project::with('translations', 'category.translations')->findOrFail($id);
|
||||
|
||||
$translation = $project->translations->firstWhere('locale', 'fa');
|
||||
$categoryTranslation = $project->category->translations->firstWhere('locale', 'fa');
|
||||
|
||||
$formattedProject = [
|
||||
'id' => $project->id,
|
||||
'title' => $translation ? $translation->title : null,
|
||||
'category' => $categoryTranslation ? $categoryTranslation->title : null,
|
||||
'images' => [
|
||||
['src' => $project->image1, 'position' => '1', 'zIndex' => 1],
|
||||
['src' => $project->image2, 'position' => '2', 'zIndex' => 2],
|
||||
],
|
||||
'created_at' => $project->created_at,
|
||||
'updated_at' => $project->updated_at,
|
||||
];
|
||||
|
||||
return response()->json(['project' => $formattedProject]);
|
||||
}
|
||||
|
||||
public function update(UpdateProjectRequest $request, $id)
|
||||
{
|
||||
$project = Project::findOrFail($id);
|
||||
$project->update($request->only(['category_id', 'image1', 'image2']));
|
||||
|
||||
foreach (['fa', 'en'] as $locale) {
|
||||
$project->translations()->updateOrCreate(
|
||||
['locale' => $locale],
|
||||
[
|
||||
'title' => $request->input("title_$locale"),
|
||||
'description' => $request->input("description_$locale"),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
return response()->json($project->load('translations'));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
Project::destroy($id);
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Website;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Website\Service;
|
||||
|
||||
class ServiceController extends Controller
|
||||
{
|
||||
public function index($locale)
|
||||
{
|
||||
$services = Service::with(['translations' => function ($query) use ($locale) {
|
||||
$query->where('locale', $locale);
|
||||
}])->get();
|
||||
|
||||
$response = $services->map(function ($service) use ($locale) {
|
||||
return [
|
||||
'id' => $service->id,
|
||||
'imageSrc' => $service->imageSrc,
|
||||
'altText' => $service->translations->first()->altText ?? '',
|
||||
'created_at' => $service->created_at,
|
||||
'updated_at' => $service->updated_at,
|
||||
'title' => optional($service->translations->first())->title,
|
||||
'description' => optional($service->translations->first())->description,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json($response);
|
||||
}
|
||||
|
||||
public function showTranslation($id, $locale)
|
||||
{
|
||||
$service = Service::with('translations')->findOrFail($id);
|
||||
|
||||
return response()->json([
|
||||
'id' => $service->id,
|
||||
'imageSrc' => $service->imageSrc,
|
||||
'title' => $service->getTitle($locale),
|
||||
'description' => $service->getDescription($locale),
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user