From e2bcd72e395dcdbb39911d831a601de74a907148 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 8 Sep 2020 09:29:46 -0400 Subject: [PATCH] Flight history #807 --- app/Contracts/Repository.php | 3 +- app/Repositories/PirepRepository.php | 3 -- app/Services/AircraftService.php | 22 ++++++++++++++ tests/PIREPTest.php | 43 ++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/app/Contracts/Repository.php b/app/Contracts/Repository.php index 191f6f40..fdb82064 100644 --- a/app/Contracts/Repository.php +++ b/app/Contracts/Repository.php @@ -6,7 +6,8 @@ use Illuminate\Validation\Validator; use Prettus\Repository\Eloquent\BaseRepository; /** - * @mixin \Prettus\Repository\Eloquent\BaseRepository + * @mixin Model + * @mixin BaseRepository */ abstract class Repository extends BaseRepository { diff --git a/app/Repositories/PirepRepository.php b/app/Repositories/PirepRepository.php index 800c395e..50b955c3 100644 --- a/app/Repositories/PirepRepository.php +++ b/app/Repositories/PirepRepository.php @@ -7,9 +7,6 @@ use App\Models\Enums\PirepState; use App\Models\Pirep; use App\Models\User; -/** - * Class PirepRepository - */ class PirepRepository extends Repository { protected $fieldSearchable = [ diff --git a/app/Services/AircraftService.php b/app/Services/AircraftService.php index 047c7757..b61adf54 100644 --- a/app/Services/AircraftService.php +++ b/app/Services/AircraftService.php @@ -4,11 +4,13 @@ namespace App\Services; use App\Contracts\Service; use App\Models\Aircraft; +use App\Models\Enums\PirepState; use App\Models\Pirep; use App\Repositories\PirepRepository; class AircraftService extends Service { + /** @var PirepRepository */ private $pirepRepo; public function __construct(PirepRepository $pirepRepo) @@ -16,6 +18,26 @@ class AircraftService extends Service $this->pirepRepo = $pirepRepo; } + /** + * Get a list of the PIREPs that have been flown by this aircraft + * + * @param Aircraft $aircraft + * @param int $count + * + * @return Pirep[] Collection of PIREPs + */ + public function getFlightHistory(Aircraft $aircraft, $count = 5) + { + return $this->pirepRepo + ->orderBy('created_at', 'desc') + ->limit($count) + ->where([ + 'aircraft_id' => $aircraft->id, + 'state' => PirepState::ACCEPTED, + ]) + ->get(); + } + /** * Recalculate all aircraft stats and hours */ diff --git a/tests/PIREPTest.php b/tests/PIREPTest.php index f04df710..f220af4c 100644 --- a/tests/PIREPTest.php +++ b/tests/PIREPTest.php @@ -21,6 +21,7 @@ use App\Services\FlightService; use App\Services\PirepService; use App\Services\UserService; use Carbon\Carbon; +use Illuminate\Support\Collection; use Illuminate\Support\Facades\Notification; class PIREPTest extends TestCase @@ -317,6 +318,48 @@ class PIREPTest extends TestCase $this->assertNotEquals($last_pirep->id, $latest_pirep->id); } + /** + * Create multiple accepted PIREPs in an order, and then retrieve the aircraft history + * and also check that they're returned, in order + */ + public function testAircraftGetPirepHistory() + { + /** @var User $user */ + $user = factory(User::class)->create([ + 'flights' => 0, + 'flight_time' => 0, + 'rank_id' => 1, + ]); + + $pirep_id_order = []; + for ($i = 1; $i <= 2; $i++) { + /** @var Pirep $pirep */ + $pirep = factory(Pirep::class)->create([ + 'airline_id' => $user->airline_id, + 'aircraft_id' => 1, + 'user_id' => $user->id, + 'created_at' => Carbon::now()->subHours($i), // subtract $i hours + ]); + + $pirep_id_order[] = $pirep->id; + + $this->pirepSvc->create($pirep); + $this->pirepSvc->accept($pirep); + } + + $aircraft = Aircraft::find(1); + + /** @var AircraftService $aircraftSvc */ + $aircraftSvc = app(AircraftService::class); + + /** @var Collection $pirep_history */ + $pirep_history = $aircraftSvc->getFlightHistory($aircraft, 5); + for ($i = 0; $i < $pirep_history->count(); $i++) { + $pirep = $pirep_history->get($i); + $this->assertEquals($pirep_id_order[$i], $pirep->id); + } + } + /** * check the stats/ranks, etc have incremented properly */