Add tag-by-slug posts endpoint for real tag pages
Deploy Backend / deploy (push) Successful in 2m43s

This commit is contained in:
Parsa Aghayi committed 2026-10-02 06:48:53 -04:00
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",
+1
View File
@@ -63,6 +63,7 @@ Route::middleware('throttle:60,1')->group(function () {
Route::get('categories/{slug}/posts', [CategoryController::class, 'getCategoryWithPosts']);
Route::get('tags', [TagController::class, 'index']);
Route::get('tags/by-slug/{slug}/posts', [TagController::class, 'getTagWithPosts']);
Route::get('tags/{id}', [TagController::class, 'show']);
});
});
+27
View File
@@ -2,7 +2,10 @@
namespace Tests\Feature\Blog;
use App\Models\Blog\Category;
use App\Models\Blog\Post;
use App\Models\Blog\Tag;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
@@ -52,4 +55,28 @@ class TagTest extends TestCase
$response->assertStatus(200);
$this->assertDatabaseMissing('tags', ['id' => $tag->id]);
}
public function test_can_get_tag_with_posts_by_slug()
{
$category = Category::factory()->create();
$user = User::factory()->create();
$tag = Tag::factory()->create();
$posts = Post::factory()->count(2)->create([
'category_id' => $category->id,
'author_id' => $user->id,
]);
$tag->posts()->attach($posts->pluck('id'));
$response = $this->getJson("/api/blog/tags/by-slug/{$tag->slug}/posts");
$response
->assertStatus(200)
->assertJsonPath('tag.slug', $tag->slug)
->assertJsonCount(2, 'posts')
->assertJsonPath('meta.total', 2);
$this->getJson('/api/blog/tags/by-slug/does-not-exist/posts')
->assertStatus(404);
}
}