Skip to content

Commit

Permalink
add dateFrom and dateTo methods for filtering by date range
Browse files Browse the repository at this point in the history
  • Loading branch information
imahmood authored and salehhashemi1992 committed May 18, 2024
1 parent 19281f9 commit b65075a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ protected function compare(string $field, string $operator, string $value): self
return $this;
}

protected function dateFrom(string $field, string $date): static
{
if ($date) {
$this->getQuery()->where($field, '>=', $date.' 00:00:00');
}

return $this;
}

protected function dateTo(string $field, string $date): static
{
if ($date) {
$this->getQuery()->where($field, '<=', $date.' 23:59:59');
}

return $this;
}

public function setQuery(QueryBuilder $builder): void
{
$this->builder = $builder;
Expand Down
29 changes: 29 additions & 0 deletions tests/RepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,35 @@ public function testGetFilteredPosts()
$this->assertEquals('published', $filteredPosts->first()->status);
}

public function testFilterResultsByDate()
{
Post::factory()->create([
'title' => 'Post 03',
'created_at' => now()->subDay(),
]);

Post::factory()->create([
'title' => 'Post 02',
'created_at' => now()->subDays(2),
]);

Post::factory()->create([
'title' => 'Post 01',
'created_at' => now()->subDays(3),
]);

/** @var \Salehhashemi\Repository\Tests\TestSupport\Repositories\PostRepositoryInterface $postRepo */
$postRepo = $this->app->make(PostRepositoryInterface::class);

/** @var \Illuminate\Database\Eloquent\Collection $filteredPosts */
$filteredPosts = $postRepo->search([
'created_from' => now()->subDays(3)->toDateString(),
'created_to' => now()->subDays(2)->toDateString(),
]);

$this->assertSame(['Post 01', 'Post 02'], $filteredPosts->pluck('title')->toArray());
}

public function testGetCriteriaPosts()
{
Post::factory()->create(['is_featured' => 1]);
Expand Down
8 changes: 5 additions & 3 deletions tests/TestSupport/Filters/PostFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public function applyFilter(array $queryParams): QueryBuilder
{
$this
->whereLike('title', $queryParams['title'] ?? '', self::WILD_BOTH)
->whereValue('status', $queryParams['status'] ?? '')
->compare('created_at', '>=', $queryParams['created_from'] ?? '')
->compare('created_at', '<=', $queryParams['created_to'] ?? '');
->whereValue('status', $queryParams['status'] ?? '');

$this
->dateFrom('created_at', $queryParams['created_from'] ?? '')
->dateTo('created_at', $queryParams['created_to'] ?? '');

if (! empty($queryParams['category_id'])) {
$this->getQuery()->whereHas('categories', function ($query) use ($queryParams) {
Expand Down

0 comments on commit b65075a

Please sign in to comment.