From 0965465601959641049e29e67034aa194e05f423 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 16 Apr 2018 16:33:41 -0500 Subject: [PATCH 1/3] add whereNotInOrder() to repository --- .../Controllers/Admin/PirepController.php | 4 +-- app/Interfaces/Repository.php | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Admin/PirepController.php b/app/Http/Controllers/Admin/PirepController.php index 5b80af67..21a7a79c 100644 --- a/app/Http/Controllers/Admin/PirepController.php +++ b/app/Http/Controllers/Admin/PirepController.php @@ -190,9 +190,7 @@ class PirepController extends Controller $this->pirepRepo->pushCriteria($criterea); $pireps = $this->pirepRepo - ->findWhere(['status', '!=', PirepState::CANCELLED]) - ->findWhere(['status', '!=', PirepState::DRAFT]) - ->orderBy('created_at', 'desc') + ->whereNotInOrder('status', [PirepState::CANCELLED, PirepState::DRAFT], 'created_at', 'desc') ->paginate(); return view('admin.pireps.index', [ diff --git a/app/Interfaces/Repository.php b/app/Interfaces/Repository.php index d29b7d6b..e9dba006 100644 --- a/app/Interfaces/Repository.php +++ b/app/Interfaces/Repository.php @@ -76,4 +76,29 @@ abstract class Repository extends \Prettus\Repository\Eloquent\BaseRepository return $q; }); } + + /** + * Find records with a WHERE clause but also sort them + * @param string $col + * @param array $values + * @param string $sort_by + * @param string $order_by + * @return $this + */ + public function whereNotInOrder($col, $values, $sort_by, $order_by = 'asc') + { + return $this->scopeQuery(function ($query) use ($col, $values, $sort_by, $order_by) { + $q = $query->whereNotIn($col, $values); + # See if there are multi-column sorts + if (\is_array($sort_by)) { + foreach ($sort_by as $key => $sort) { + $q = $q->orderBy($key, $sort); + } + } else { + $q = $q->orderBy($sort_by, $order_by); + } + + return $q; + }); + } } From ea32353890d4d70e123fd767a967bb1317f48ddb Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 16 Apr 2018 16:46:51 -0500 Subject: [PATCH 2/3] Fix flight on days deactivation test --- app/Interfaces/Repository.php | 2 +- tests/FlightTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Interfaces/Repository.php b/app/Interfaces/Repository.php index e9dba006..c1d265e4 100644 --- a/app/Interfaces/Repository.php +++ b/app/Interfaces/Repository.php @@ -78,7 +78,7 @@ abstract class Repository extends \Prettus\Repository\Eloquent\BaseRepository } /** - * Find records with a WHERE clause but also sort them + * Find records where values don't match a list but sort the rest * @param string $col * @param array $values * @param string $sort_by diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 94c95b85..08ddc7eb 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -163,7 +163,7 @@ class FlightTest extends TestCase $this->user = factory(App\Models\User::class)->create(); // Set it to Monday or Tuesday, depending on what today is - if (date('N') === 1) { // today is a monday + if (date('N') === '1') { // today is a monday $days = Days::getDaysMask([Days::TUESDAY]); } else { $days = Days::getDaysMask([Days::MONDAY]); From 59302ded0f02b3f4d66440525ff4bc6eab7b5b39 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Mon, 16 Apr 2018 16:57:22 -0500 Subject: [PATCH 3/3] Fix another flight day test --- tests/FlightTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/FlightTest.php b/tests/FlightTest.php index 08ddc7eb..0f449bbb 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -172,8 +172,6 @@ class FlightTest extends TestCase factory(App\Models\Flight::class, 5)->create(); $flight = factory(App\Models\Flight::class)->create([ 'days' => $days, - #'start_date' => Carbon\Carbon::now('UTC')->subDay(1), - #'end_date' => Carbon\Carbon::now('UTC')->addDays(1), ]); // Run the event that will enable/disable flights @@ -221,7 +219,7 @@ class FlightTest extends TestCase $this->user = factory(App\Models\User::class)->create(); // Set it to Monday or Tuesday, depending on what today is - if (date('N') === 1) { // today is a monday + if (date('N') === '1') { // today is a monday $days = Days::getDaysMask([Days::TUESDAY]); } else { $days = Days::getDaysMask([Days::MONDAY]);