Compare commits

...

1 Commits

Author SHA1 Message Date
Nabeel Shahzad
e2bcd72e39 Flight history #807 2020-09-08 09:29:46 -04:00
4 changed files with 67 additions and 4 deletions

View File

@@ -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
{

View File

@@ -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 = [

View File

@@ -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
*/

View File

@@ -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
*/