Skip to content

Commit

Permalink
Only allow pizza-ninjas inscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mvdnbrk committed Jan 10, 2025
1 parent ab743cc commit feeb20a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/Http/Controllers/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace App\Http\Controllers;

use App\Models\Inscription;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\View\View;

class CollectionController extends Controller
{
public function __invoke(): View
{
$inscriptions = Inscription::orderBy('name')->paginate(5);
$inscriptions = Inscription::whereHas('collection', function (Builder $query) {
$query->where('slug', 'pizza-ninjas');
})
->orderBy('name')
->paginate(5);

return view('collection', [
'inscriptions' => $inscriptions,
Expand Down
4 changes: 4 additions & 0 deletions app/Http/Controllers/InscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ class InscriptionController extends Controller
{
public function __invoke(Inscription $inscription): View
{
if (! $inscription->collection || $inscription->collection->slug !== 'pizza-ninjas') {
abort(404);
}

return view('inscription', [
'inscription' => $inscription,
]);
Expand Down
6 changes: 5 additions & 1 deletion app/Http/Controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Models\Inscription;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
Expand All @@ -21,7 +22,10 @@ public function __invoke(Request $request): RedirectResponse

$query = Str::of($validated['query'])->after('#')->toString();

$inscription = Inscription::where('name', 'LIKE', "%#{$query}")
$inscription = Inscription::whereHas('collection', function (Builder $query) {
$query->where('slug', 'pizza-ninjas');
})
->where('name', 'LIKE', "%#{$query}")
->orWhere('inscription_id', $query)
->orderBy('name')
->first();
Expand Down
27 changes: 27 additions & 0 deletions tests/Feature/ViewInscriptionPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature;

use App\Models\Inscription;
use App\Models\OrdinalsCollection;
use Illuminate\Foundation\Testing\RefreshDatabase;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
Expand All @@ -14,7 +15,12 @@ class ViewInscriptionPageTest extends TestCase
#[Test]
public function a_request_to_the_inscription_page_should_return_a_200_status_code(): void
{
$collection = OrdinalsCollection::factory()->create([
'slug' => 'pizza-ninjas',
]);

Inscription::factory()->create([
'collection_id' => $collection->id,
'inscription_id' => '9ad43b591e58b23d8550dfdae431432b6ea3a7079d09ef80ee1554c5a3f8d543i0',
]);

Expand All @@ -23,10 +29,31 @@ public function a_request_to_the_inscription_page_should_return_a_200_status_cod
->assertViewIs('inscription');
}

#[Test]
public function a_request_to_an_inscription_page_not_belonging_to_pizza_ninjas_collection_should_return_a_404(): void
{
$otherCollection = OrdinalsCollection::factory()->create([
'slug' => 'some-other-collection',
]);

Inscription::factory()->create([
'collection_id' => $otherCollection->id,
'inscription_id' => '9ad43b591e58b23d8550dfdae431432b6ea3a7079d09ef80ee1554c5a3f8d543i0',
]);

$this->get('/inscription/9ad43b591e58b23d8550dfdae431432b6ea3a7079d09ef80ee1554c5a3f8d543i0')
->assertNotFound();
}

#[Test]
public function a_request_to_the_inscription_page_should_see_the_right_title_and_image(): void
{
$collection = OrdinalsCollection::factory()->create([
'slug' => 'pizza-ninjas',
]);

Inscription::factory()->create([
'collection_id' => $collection->id,
'inscription_id' => '1234',
'name' => 'Pizza Ninjas #464',
]);
Expand Down

0 comments on commit feeb20a

Please sign in to comment.