diff --git a/routes/blog-api.php b/routes/blog-api.php index 5f3763d..e94b406 100644 --- a/routes/blog-api.php +++ b/routes/blog-api.php @@ -37,5 +37,6 @@ Route::group(['prefix' => 'tags'], function () { Route::get('/', [TagController::class, 'index']); + Route::get('{slug}', [TagController::class, 'findBySlug']); }); }); diff --git a/src/Http/Controllers/TagController.php b/src/Http/Controllers/TagController.php index 7e9ed6b..7aea4fa 100644 --- a/src/Http/Controllers/TagController.php +++ b/src/Http/Controllers/TagController.php @@ -2,8 +2,65 @@ namespace CSlant\Blog\Api\Http\Controllers; +use Botble\Blog\Models\Tag; +use CSlant\Blog\Api\Enums\StatusEnum; +use CSlant\Blog\Api\Http\Resources\TagResource; +use CSlant\Blog\Core\Facades\Base\SlugHelper; use CSlant\Blog\Core\Http\Controllers\Base\BaseTagController; +use CSlant\Blog\Core\Http\Responses\Base\BaseHttpResponse; +use CSlant\Blog\Core\Models\Slug; +/** + * Class TagController + * + * @package CSlant\Blog\Api\Http\Controllers + * + * @group Blog + * + * @authenticated + * + * @method BaseHttpResponse httpResponse() + * @method BaseHttpResponse setData(mixed $data) + */ class TagController extends BaseTagController { + /** + * Get tag by slug + * + * @group Blog + * @queryParam slug Find by slug of tag. + */ + public function findBySlug(string $slug) + { + /** @var Slug $slug */ + $slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Tag::class)); + if (!$slug) { + return $this + ->httpResponse() + ->setError() + ->setCode(404) + ->setMessage('Not found'); + } + + $tag = Tag::query() + ->with('slugable') + ->where([ + 'id' => $slug->reference_id, + 'status' => StatusEnum::PUBLISHED, + ]) + ->first(); + + if (!$tag) { + return $this + ->httpResponse() + ->setError() + ->setCode(404) + ->setMessage('Not found'); + } + + return $this + ->httpResponse() + ->setData(new TagResource($tag)) + ->toApiResponse(); + } }