Add tag-by-slug posts endpoint for real tag pages
Deploy Backend / deploy (push) Successful in 2m43s
Deploy Backend / deploy (push) Successful in 2m43s
This commit is contained in:
1 parent
d4f41b9c2e
commit
21fa37af1f
3 files changed
+89
No files matched your search
@@ -6,6 +6,7 @@ use App\Http\Controllers\Concerns\ValidatesListQuery;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Blog\StoreTagRequest;
|
||||
use App\Http\Requests\Blog\UpdateTagRequest;
|
||||
use App\Http\Resources\PostResource;
|
||||
use App\Http\Resources\TagResource;
|
||||
use App\Models\Blog\Tag;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -58,6 +59,66 @@ class TagController extends Controller
|
||||
return new TagResource($tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Get(
|
||||
* path="/blog/tags/by-slug/{slug}/posts",
|
||||
* tags={"Tags"},
|
||||
* summary="Get a tag (and its posts) by slug",
|
||||
*
|
||||
* @OA\Parameter(name="slug", in="path", required=true, @OA\Schema(type="string")),
|
||||
*
|
||||
* @OA\Response(response=200, description="OK")
|
||||
* )
|
||||
*/
|
||||
public function getTagWithPosts(Request $request, $slug)
|
||||
{
|
||||
$tag = Tag::where('slug', $slug)->first();
|
||||
|
||||
if (! $tag) {
|
||||
return response()->json([
|
||||
'message' => 'Tag 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 = $tag->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([
|
||||
'tag' => new TagResource($tag),
|
||||
'posts' => PostResource::collection($posts),
|
||||
'meta' => $meta,
|
||||
'links' => $links,
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/blog/tags",
|
||||
|
||||
Reference in new issue
Block a user