Skip to content

Commit

Permalink
[collection] item filter
Browse files Browse the repository at this point in the history
  • Loading branch information
rastislav-chynoransky committed Apr 10, 2024
1 parent 7cd14b3 commit c8790cb
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 32 deletions.
6 changes: 3 additions & 3 deletions app/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function getReadingTimeAttribute()
return getEstimatedReadingTime($this->text, \App::getLocale());
}

public function getFilterItemsUrlAttribute(): ?string
public function getItemFilterAttribute(): ?array
{
if (!$this->url) {
return null;
Expand All @@ -153,9 +153,9 @@ public function getFilterItemsUrlAttribute(): ?string
fn(Stringable $value) => $value->explode('|')
)
);
$query = ['filter' => $filter->toArray()];
return $filter->toArray();
}

return route('api.v1.items.index', $query);
return $query['filter'] ?? [];
}
}
3 changes: 1 addition & 2 deletions app/Http/Controllers/Api/CollectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public function index()
->published()
->orderBy('published_at', 'desc')
->paginate();
$paginator->getCollection()->each->append('filter_items_count');
return CollectionResource::collection($paginator);
}

public function show(Collection $collection)
{
$collection->append('filter_items_url');
$collection->append('item_filter');
return new CollectionResource($collection);
}
}
31 changes: 31 additions & 0 deletions app/Http/Controllers/Api/CollectionItemController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Http\Controllers\Api;

use App\Collection;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Request;

class CollectionItemController extends Controller
{
public function index(Collection $collection)
{
$filter = $collection->item_filter;
if (!$filter) {
abort(404);
}

$parameters = ['filter' => $filter];

if (request()->has('size')) {
$parameters['size'] = request()->integer('size');
}

if (request()->has('page')) {
$parameters['page'] = request()->integer('page');
}

$request = Request::create(route('api.v1.items.index', $parameters));
return app()->handle($request);
}
}
2 changes: 1 addition & 1 deletion app/Http/Resources/CollectionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function toArray(Request $request): array
'text' => $this->text,
'header_image_src' => $this->header_image_src,
'header_image_srcset' => $this->header_image_srcset,
'filter_items_url' => $this->filter_items_url,
'item_filter' => $this->whenAppended('item_filter'),
];
}
}
5 changes: 5 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\Api\ArticleController;
use App\Http\Controllers\Api\CollectionController;
use App\Http\Controllers\Api\CollectionItemController;
use App\Http\Controllers\Api\NewsletterSubscriptionController;
use App\Http\Controllers\Api\SharedUserCollectionController;
use App\Http\Controllers\Api\TrackFeaturedPieceClick;
Expand Down Expand Up @@ -34,6 +35,10 @@
->names('api.collections')
->only(['index', 'show']);

Route::resource('/collections/{collection}/items', CollectionItemController::class)
->names('api.collections.items')
->only(['index']);

Route::resource('/newsletter-subscriptions', NewsletterSubscriptionController::class)->names(
'api.newsletter-subscriptions'
);
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/Api/CollectionItemsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Feature\Api;

use App\Collection;
use App\Elasticsearch\Repositories\ItemRepository;
use App\Item;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\RecreateSearchIndex;
use Tests\TestCase;

class CollectionItemsTest extends TestCase
{
use RefreshDatabase;
use RecreateSearchIndex;

public function testIndex()
{
Item::factory()->create(['author' => 'author-1']);
Item::factory()->create(['author' => 'author-2']);
app(ItemRepository::class)->refreshIndex();

$collection = Collection::factory()->create([
'url' => 'https://www.webumenia.sk/katalog-new?filter[author][]=author-1',
]);

$url = route('api.collections.items.index', [
'collection' => $collection,
'size' => 2,
]);
$response = $this->get($url);
$response->assertJsonCount(1, 'data');
}
}
13 changes: 3 additions & 10 deletions tests/Feature/Api/CollectionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ public function testIndex()
. 'http://localhost/images/kolekcie/image.1400.jpg 1400w, '
. 'http://localhost/images/kolekcie/image.jpg 1024w, '
. 'http://localhost/images/kolekcie/image.640.jpg 640w',
'filter_items_url' => route('api.v1.items.index', [
'filter' => [
'author' => ['author-1'],
],
]),
]);
}

Expand All @@ -53,11 +48,9 @@ public function testShow()
. 'http://localhost/images/kolekcie/image.1400.jpg 1400w, '
. 'http://localhost/images/kolekcie/image.jpg 1024w, '
. 'http://localhost/images/kolekcie/image.640.jpg 640w',
'filter_items_url' => route('api.v1.items.index', [
'filter' => [
'author' => 'author-1',
],
]),
'item_filter' => [
'author' => 'author-1',
],
]);
}
}
26 changes: 10 additions & 16 deletions tests/Unit/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,28 @@

class CollectionTest extends TestCase
{
public function testFilterItemsUrlFromWebumeniaUrl()
public function testItemFilterFromWebumeniaUrl()
{
$collection = Collection::factory()->make([
'url' =>
'https://www.webumenia.sk/katalog-new?filter[work_type][]=maliarstvo&filter[work_type][]=fotografia',
]);

$expected = route('api.v1.items.index', [
'filter' => [
'work_type' => ['maliarstvo', 'fotografia'],
],
]);

$this->assertEquals($expected, $collection->filter_items_url);
$expected = [
'work_type' => ['maliarstvo', 'fotografia'],
];
$this->assertEquals($expected, $collection->item_filter);
}

public function testFilterItemsUrlFromMoravskaGalerieUrl()
public function testItemFilterFromMoravskaGalerieUrl()
{
$collection = Collection::factory()->make([
'url' => 'https://sbirky.moravska-galerie.cz/?work_type=maliarstvo|fotografia',
]);

$expected = route('api.v1.items.index', [
'filter' => [
'work_type' => ['maliarstvo', 'fotografia'],
],
]);

$this->assertEquals($expected, $collection->filter_items_url);
$expected = [
'work_type' => ['maliarstvo', 'fotografia'],
];
$this->assertEquals($expected, $collection->item_filter);
}
}

0 comments on commit c8790cb

Please sign in to comment.