diff --git a/app/Http/Controllers/Api/SiteController.php b/app/Http/Controllers/Api/SiteController.php new file mode 100644 index 0000000..a92eeda --- /dev/null +++ b/app/Http/Controllers/Api/SiteController.php @@ -0,0 +1,27 @@ +filled('name')) { + $query->where('name', 'like', '%' . $request->string('name') . '%'); + } + + return SiteResource::collection($query->orderBy('name')->get()); + } + + public function show(Site $site) + { + return new SiteResource($site); + } +} diff --git a/app/Http/Resources/SiteResource.php b/app/Http/Resources/SiteResource.php new file mode 100644 index 0000000..2319a5a --- /dev/null +++ b/app/Http/Resources/SiteResource.php @@ -0,0 +1,21 @@ + $this->id, + 'name' => $this->name, + 'url' => $this->url, + 'description' => $this->description, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]; + } +} diff --git a/bootstrap/app.php b/bootstrap/app.php index 7a2848f..4b327d2 100644 --- a/bootstrap/app.php +++ b/bootstrap/app.php @@ -8,6 +8,7 @@ use Illuminate\Http\Request; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', + api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) diff --git a/routes/api.php b/routes/api.php new file mode 100644 index 0000000..b46f038 --- /dev/null +++ b/routes/api.php @@ -0,0 +1,7 @@ +count(3)->create(); + + $response = $this->getJson('/api/sites'); + + $response->assertOk()->assertJsonCount(3, 'data'); +}); + +test('it filters sites by partial name match', function () { + Site::factory()->create(['name' => 'My Awesome Blog']); + Site::factory()->create(['name' => 'Other Site']); + + $response = $this->getJson('/api/sites?name=Awesome'); + + $response->assertOk() + ->assertJsonCount(1, 'data') + ->assertJsonPath('data.0.name', 'My Awesome Blog'); +}); + +test('it shows a single site', function () { + $site = Site::factory()->create(['name' => 'Solo Site']); + + $response = $this->getJson("/api/sites/{$site->id}"); + + $response->assertOk()->assertJsonPath('data.name', 'Solo Site'); +}); + +test('it returns 404 for a missing site', function () { + $this->getJson('/api/sites/999')->assertNotFound(); +});