Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for Date Range Filtering #19

Merged
merged 1 commit into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading