1.2: restructure controllers to Api/ prefix and update routes
This commit is contained in:
1 parent
cbcccfcbf8
commit
28c7a317e5
11 files changed
+137
-271
No files matched your search
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\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);
|
||||
}
|
||||
|
||||
public function getCategoryWithPosts(Request $request, $slug)
|
||||
{
|
||||
$category = Category::where('slug', $slug)->first();
|
||||
|
||||
if (!$category) {
|
||||
return response()->json([
|
||||
'message' => 'Category not found.'
|
||||
], 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 = [
|
||||
'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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user