Skip to content

Commit

Permalink
Merge pull request #32 from cslant/feat/api-get-tag-by-slug
Browse files Browse the repository at this point in the history
Feat/api get tag by slug
  • Loading branch information
thuankg1752 authored Jan 11, 2025
2 parents afe708f + ef96a57 commit 404c850
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions routes/blog-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@

Route::group(['prefix' => 'tags'], function () {
Route::get('/', [TagController::class, 'index']);
Route::get('{slug}', [TagController::class, 'findBySlug']);
});
});
57 changes: 57 additions & 0 deletions src/Http/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 33 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.1

Method CSlant\Blog\Api\Http\Controllers\TagController::findBySlug() has no return type specified.

Check failure on line 33 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.2

Method CSlant\Blog\Api\Http\Controllers\TagController::findBySlug() has no return type specified.

Check failure on line 33 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.3

Method CSlant\Blog\Api\Http\Controllers\TagController::findBySlug() has no return type specified.
{
/** @var Slug $slug */
$slug = SlugHelper::getSlug($slug, SlugHelper::getPrefix(Tag::class));

Check failure on line 36 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.1

Class Botble\Blog\Models\Tag not found.

Check failure on line 36 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.2

Class Botble\Blog\Models\Tag not found.

Check failure on line 36 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.3

Class Botble\Blog\Models\Tag not found.
if (!$slug) {
return $this
->httpResponse()
->setError()
->setCode(404)
->setMessage('Not found');
}

$tag = Tag::query()

Check failure on line 45 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.1

Call to static method query() on an unknown class Botble\Blog\Models\Tag.

Check failure on line 45 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.2

Call to static method query() on an unknown class Botble\Blog\Models\Tag.

Check failure on line 45 in src/Http/Controllers/TagController.php

View workflow job for this annotation

GitHub Actions / PHPStan - P8.3

Call to static method query() on an unknown class Botble\Blog\Models\Tag.
->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();
}
}

0 comments on commit 404c850

Please sign in to comment.