107 lines
3.7 KiB
PHP
107 lines
3.7 KiB
PHP
<?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('category')->paginate($perPage);
|
|
$formattedProjects = $projects->items();
|
|
|
|
$formattedProjects = array_map(function ($project) use ($locale) {
|
|
return [
|
|
'id' => $project->id,
|
|
'title' => $project->getTranslation('title', $locale),
|
|
'category' => $project->category?->getTranslation('title', $locale),
|
|
'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->getTranslation('title', $locale),
|
|
'image' => $category->image,
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
|
|
public function store(StoreProjectRequest $request)
|
|
{
|
|
$project = Project::create($request->only(['category_id', 'image1', 'image2']));
|
|
|
|
foreach (['fa', 'en'] as $locale) {
|
|
$project->setTranslation('title', $locale, $request->input("title_$locale"));
|
|
$project->setTranslation('description', $locale, $request->input("description_$locale"));
|
|
}
|
|
$project->save();
|
|
|
|
return response()->json($project, 201);
|
|
}
|
|
|
|
public function show($id, $locale)
|
|
{
|
|
$project = Project::with('category')->findOrFail($id);
|
|
|
|
$formattedProject = [
|
|
'id' => $project->id,
|
|
'title' => $project->getTranslation('title', $locale),
|
|
'category' => $project->category?->getTranslation('title', 'fa'),
|
|
'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) {
|
|
if ($request->has("title_$locale")) {
|
|
$project->setTranslation('title', $locale, $request->input("title_$locale"));
|
|
}
|
|
if ($request->has("description_$locale")) {
|
|
$project->setTranslation('description', $locale, $request->input("description_$locale"));
|
|
}
|
|
}
|
|
$project->save();
|
|
|
|
return response()->json($project);
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
Project::destroy($id);
|
|
return response()->json(null, 204);
|
|
}
|
|
}
|