145 lines
5.3 KiB
PHP
145 lines
5.3 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Blog;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Http\Requests\Blog\StoreCategoryRequest;
|
||
use App\Http\Requests\Blog\UpdateCategoryRequest;
|
||
use App\Http\Resources\CategoryResource;
|
||
use App\Http\Resources\PostResource;
|
||
use App\Models\Blog\Category;
|
||
use Illuminate\Http\Request;
|
||
|
||
class CategoryController extends Controller
|
||
{
|
||
// نمایش لیست دستهبندیها با صفحهبندی
|
||
public function index(Request $request)
|
||
{
|
||
// گرفتن فیلتر و سورت از درخواست کاربر
|
||
$filter = $request->input('filter'); // فیلتر (به عنوان مثال، نام دستهبندی)
|
||
$sortBy = $request->input('sort_by', 'created_at'); // مرتبسازی (پیشفرض بر اساس زمان ایجاد)
|
||
$sortDirection = $request->input('sort_direction', 'desc'); // جهت مرتبسازی (پیشفرض نزولی)
|
||
$perPage = $request->input('per_page', 10); // تعداد آیتمها در هر صفحه (پیشفرض ۱۰)
|
||
|
||
// شروع کوئری با مدل دستهبندی و روابط
|
||
$query = Category::with('posts');
|
||
|
||
// اعمال فیلتر در صورت موجود بودن
|
||
if (!empty($filter)) {
|
||
$query->where('name', 'like', '%' . $filter . '%');
|
||
}
|
||
|
||
// اعمال مرتبسازی
|
||
$query->orderBy($sortBy, $sortDirection);
|
||
|
||
// گرفتن نتایج با پجینیشن
|
||
$categories = $query->paginate($perPage);
|
||
|
||
// بازگشت دادهها با استفاده از ریسورس
|
||
return CategoryResource::collection($categories);
|
||
}
|
||
|
||
// نمایش یک دستهبندی خاص
|
||
public function show($id)
|
||
{
|
||
$category = Category::with('posts')->findOrFail($id);
|
||
return new CategoryResource($category);
|
||
}
|
||
|
||
/**
|
||
* نمایش دستهبندی و پستهای مرتبط با آن بر اساس slug
|
||
*
|
||
* @param \Illuminate\Http\Request $request
|
||
* @param string $slug
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function getCategoryWithPosts(Request $request, $slug)
|
||
{
|
||
// یافتن دستهبندی بر اساس slug
|
||
$category = Category::where('slug', $slug)->first();
|
||
|
||
if (!$category) {
|
||
return response()->json([
|
||
'message' => 'دستهبندی مورد نظر یافت نشد.'
|
||
], 404);
|
||
}
|
||
|
||
// دریافت تعداد پستها در هر صفحه (پیشفرض: ۱۰)
|
||
$perPage = $request->input('per_page', 10);
|
||
|
||
// دریافت فیلتر و سورت از درخواست
|
||
$filter = $request->input('filter'); // فیلتر (به عنوان مثال، عنوان پست)
|
||
$sortBy = $request->input('sort_by', 'published_at'); // مرتبسازی (پیشفرض بر اساس تاریخ انتشار)
|
||
$sortDirection = $request->input('sort_direction', 'desc'); // جهت مرتبسازی (پیشفرض نزولی)
|
||
|
||
// شروع کوئری با پستهای مربوط به دستهبندی
|
||
$postsQuery = $category->posts()->with(['category', 'tags', 'author']);
|
||
|
||
// اعمال فیلتر در صورت موجود بودن
|
||
if (!empty($filter)) {
|
||
$postsQuery->where('title', 'like', '%' . $filter . '%');
|
||
}
|
||
|
||
// اعمال مرتبسازی
|
||
$postsQuery->orderBy($sortBy, $sortDirection);
|
||
|
||
// گرفتن پستها با پجینیشن
|
||
$posts = $postsQuery->paginate($perPage);
|
||
|
||
// ساخت meta و links برای pagination
|
||
$meta = [
|
||
'total' => $posts->total(),
|
||
'per_page' => $posts->perPage(),
|
||
'current_page' => $posts->currentPage(),
|
||
'last_page' => $posts->lastPage(),
|
||
'from' => $posts->firstItem(),
|
||
'to' => $posts->lastItem(),
|
||
];
|
||
|
||
$links = [
|
||
'first' => $posts->url(1),
|
||
'last' => $posts->url($posts->lastPage()),
|
||
'prev' => $posts->previousPageUrl(),
|
||
'next' => $posts->nextPageUrl(),
|
||
];
|
||
|
||
// بازگشت اطلاعات دستهبندی و پستها
|
||
return response()->json([
|
||
'category' => new CategoryResource($category),
|
||
'posts' => PostResource::collection($posts),
|
||
'meta' => $meta,
|
||
'links' => $links,
|
||
], 200);
|
||
}
|
||
|
||
|
||
|
||
// ذخیره یک دستهبندی جدید
|
||
public function store(StoreCategoryRequest $request)
|
||
{
|
||
$category = Category::create($request->validated());
|
||
|
||
return new CategoryResource($category);
|
||
}
|
||
|
||
// بهروزرسانی یک دستهبندی خاص
|
||
public function update(UpdateCategoryRequest $request, $id)
|
||
{
|
||
$category = Category::findOrFail($id);
|
||
$category->update($request->validated());
|
||
|
||
return new CategoryResource($category);
|
||
}
|
||
|
||
// حذف یک دستهبندی خاص
|
||
public function destroy($id)
|
||
{
|
||
$category = Category::findOrFail($id);
|
||
// قبل از حذف، مطمئن شوید که پستها به دستهبندی دیگری منتقل شدهاند یا حذف شوند
|
||
// برای سادگی، اینجا فقط دستهبندی را حذف میکنیم
|
||
$category->delete();
|
||
|
||
return response()->json(['message' => 'Category deleted successfully'], 200);
|
||
}
|
||
}
|