From b65075a22077b97157436b8ece985def018df7b7 Mon Sep 17 00:00:00 2001 From: Mahmood Dehghani Date: Fri, 10 May 2024 11:24:12 +0330 Subject: [PATCH] add dateFrom and dateTo methods for filtering by date range --- src/BaseFilter.php | 18 +++++++++++++++ tests/RepositoryTest.php | 29 ++++++++++++++++++++++++ tests/TestSupport/Filters/PostFilter.php | 8 ++++--- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/BaseFilter.php b/src/BaseFilter.php index 8c9bbb7..1fa7263 100644 --- a/src/BaseFilter.php +++ b/src/BaseFilter.php @@ -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; diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index 7086b28..ea95bfd 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -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]); diff --git a/tests/TestSupport/Filters/PostFilter.php b/tests/TestSupport/Filters/PostFilter.php index d270a05..aebc8b1 100644 --- a/tests/TestSupport/Filters/PostFilter.php +++ b/tests/TestSupport/Filters/PostFilter.php @@ -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) {