Skip to content

Commit

Permalink
Extended the product search with limiting, ordering a querying by slug
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Mar 14, 2024
1 parent 3237040 commit f6483e2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
- `shipping_countries`
- `theme`
- `color`
- Added `ProductSearch` features:
- added the optional $limit parameter to the `getResults()` method
- added the `orderBy()` method
- added the `slugEquals()` method: it takes into account other query parameters as well, whereas the findBySlug solely queries by the given slug
- Added the `payment_method_id` to the orders table
- Added the processing of the `payment_method_id` field to the OrderFactory (Foundation)
- Added the `BillpayerChanged` checkout event
Expand Down
30 changes: 26 additions & 4 deletions src/Foundation/Search/ProductSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class ProductSearch

protected Builder $masterProductQuery;

protected ?string $orderBy = null;

public function __construct()
{
$this->searcher = Search::new();
Expand Down Expand Up @@ -240,6 +242,21 @@ public function orHavingPropertyValues(array $propertyValues): self
return $this;
}

public function orderBy(string $column, string $direction = 'asc'): self
{
$this->orderBy = "$column:$direction";

return $this;
}

public function slugEquals(string $slug): self
{
$this->productQuery->where('slug', '=', $slug);
$this->masterProductQuery->where('slug', '=', $slug);

return $this;
}

public function withInactiveProducts(): self
{
$this->productQuery->withoutGlobalScope('withoutInactiveProducts');
Expand All @@ -266,9 +283,14 @@ public function withChannels(): self

public function getSearcher(): Searcher
{
[$orderBy, $direction] = is_null($this->orderBy) ? [null, 'asc'] : explode(':', $this->orderBy);
if ('desc' === strtolower($direction)) {
$this->searcher->orderByDesc();
}

return $this->searcher
->add($this->productQuery)
->add($this->masterProductQuery);
->add($this->productQuery, null, $orderBy)
->add($this->masterProductQuery, null, $orderBy);
}

public function simplePaginate(int $perPage = 15, array $columns = ['*'], string $pageName = 'page', int $page = null): Paginator
Expand All @@ -282,8 +304,8 @@ public function paginate(int $perPage = 15, array $columns = ['*'], string $page
return $this->getSearcher()->paginate($perPage, $pageName, $page)->search();
}

public function getResults(): Collection
public function getResults(int $limit = null): Collection
{
return $this->getSearcher()->search();
return is_null($limit) ? $this->getSearcher()->search() : $this->getSearcher()->simplePaginate($limit)->search()->getCollection();
}
}
47 changes: 47 additions & 0 deletions src/Foundation/Tests/ProductSearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,4 +502,51 @@ public function it_can_paginate()
$this->assertInstanceOf(LengthAwarePaginator::class, $results);
$this->assertCount(8, $results->items());
}

/** @test */
public function it_can_limit_the_number_of_results()
{
factory(Product::class, 4)->create();
factory(MasterProduct::class, 3)->create();

$finder = new ProductSearch();
$this->assertCount(7, $finder->getResults());
$this->assertCount(5, $finder->getResults(5));
}

/** @test */
public function it_can_order_the_results_by_an_explicit_field()
{
factory(Product::class)->create(['name' => 'Effendi']);
factory(Product::class)->create(['name' => 'Aber']);
factory(Product::class)->create(['name' => 'Zgomot']);
factory(MasterProduct::class)->create(['name' => 'Biotronic']);
factory(Product::class)->create(['name' => 'Hapsi']);
factory(Product::class)->create(['name' => 'Kozmix']);


$resultset = (new ProductSearch())->orderBy('name')->getResults()->all();
$this->assertEquals('Aber', $resultset[0]->name);
$this->assertEquals('Biotronic', $resultset[1]->name);
$this->assertEquals('Effendi', $resultset[2]->name);
$this->assertEquals('Hapsi', $resultset[3]->name);
$this->assertEquals('Kozmix', $resultset[4]->name);
$this->assertEquals('Zgomot', $resultset[5]->name);
}

/** @test */
public function it_can_order_and_limit_the_results()
{
factory(Product::class)->create(['name' => 'Ethereum']);
factory(Product::class)->create(['name' => 'Tether']);
factory(MasterProduct::class)->create(['name' => 'Bitcoin']);
factory(Product::class)->create(['name' => 'Dogecoin']);
factory(Product::class)->create(['name' => 'Avalanche']);


$resultset = (new ProductSearch())->orderBy('name')->getResults(3)->all();
$this->assertEquals('Avalanche', $resultset[0]->name);
$this->assertEquals('Bitcoin', $resultset[1]->name);
$this->assertEquals('Dogecoin', $resultset[2]->name);
}
}

0 comments on commit f6483e2

Please sign in to comment.