Skip to content

Commit

Permalink
Fetch type endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
edersoares committed May 8, 2024
1 parent 32ae2b7 commit 8f4ae08
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 2 additions & 0 deletions routes/anything.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
declare(strict_types=1);

use Dex\Laravel\Anything\Http\Controllers\AnythingController;
use Dex\Laravel\Anything\Http\Controllers\FetchAnythingController;
use Dex\Laravel\Anything\Http\Controllers\StoreAnythingController;
use Illuminate\Support\Facades\Route;

Route::get('anything', AnythingController::class);
Route::get('anything/{type}', FetchAnythingController::class);
Route::put('anything/{type}/{slug}', StoreAnythingController::class);
21 changes: 21 additions & 0 deletions src/Http/Controllers/FetchAnythingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Dex\Laravel\Anything\Http\Controllers;

use Dex\Laravel\Anything\Models\Anything;
use Illuminate\Database\Eloquent\Collection;

class FetchAnythingController
{
public function __invoke(string $type): Collection
{
/** @var Collection<int, Anything> $anything */
$anything = Anything::query()
->where('type', $type)
->get();

return $anything;
}
}
10 changes: 9 additions & 1 deletion src/Models/Concerns/HasAnythingType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public static function bootHasAnythingType(): void
static::addGlobalScope(function (Builder $builder) {
/** @var Anything $model */
$model = $builder->getModel();
$builder->where('type', $model->getAnythingType());

$type = $model->getAnythingType();

// Allow that Anything model can query any type
if ($type === 'anything') {
return;
}

$builder->where('type', $type);
});
}

Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/FetchAnythingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
declare(strict_types=1);

use Dex\Laravel\Anything\Models\Anything;
use Workbench\Dex\Laravel\Anything\App\Models\Gender;
use Workbench\Dex\Laravel\Anything\App\Models\Race;
use Workbench\Dex\Laravel\Anything\Database\Seeders\GenderSeeder;
use Workbench\Dex\Laravel\Anything\Database\Seeders\RaceSeeder;

test('fetch all available anything', function () {
$anything = Anything::factory()->count(3)->create();
Expand All @@ -11,3 +15,20 @@
->assertOk()
->assertJson($anything->toArray());
});

test('fetch some type', function () {
$this->seed(GenderSeeder::class);
$this->seed(RaceSeeder::class);

$anything = Gender::query()->get();

$this->get('/anything/gender')
->assertOk()
->assertJson($anything->toArray());

$anything = Race::query()->get();

$this->get('/anything/race')
->assertOk()
->assertJson($anything->toArray());
});

0 comments on commit 8f4ae08

Please sign in to comment.