Add calls and API to get user's fleet, determined by rank #35

This commit is contained in:
Nabeel Shahzad
2018-01-10 18:40:01 -06:00
parent 8aacd844b3
commit bf4e164e0d
6 changed files with 164 additions and 5 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Services\UserService;
use Auth;
use Illuminate\Http\Request;
@@ -9,17 +10,21 @@ use App\Repositories\UserRepository;
use App\Models\UserBid;
use App\Http\Resources\Subfleet as SubfleetResource;
use App\Http\Resources\Flight as FlightResource;
use App\Http\Resources\User as UserResource;
class UserController extends RestController
{
protected $userRepo;
protected $userRepo, $userSvc;
public function __construct(UserRepository $userRepo)
{
public function __construct(
UserRepository $userRepo,
UserService $userSvc
) {
$this->userRepo = $userRepo;
$this->userSvc = $userSvc;
}
/**
@@ -42,6 +47,8 @@ class UserController extends RestController
/**
* Return all of the bids for the passed-in user
* @param $id
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function bids($id)
{
@@ -51,4 +58,23 @@ class UserController extends RestController
return FlightResource::collection($flights);
}
/**
* Return the fleet that this user is allowed to
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function fleet(Request $request)
{
if($request->id === null) {
$id = Auth::user()->id;
} else {
$id = $request->id;
}
$user = $this->userRepo->find($id);
$subfleets = $this->userSvc->getAllowableSubfleets($user);
return SubfleetResource::collection($subfleets);
}
}

View File

@@ -53,7 +53,9 @@ Route::group(['middleware' => ['api.auth']], function ()
# This is the info of the user whose token is in use
Route::get('user', 'UserController@index');
Route::get('user/fleet', 'UserController@fleet');
Route::get('users/{id}', 'UserController@get');
Route::get('users/{id}/bids', 'UserController@bids');
Route::get('users/{id}/fleet', 'UserController@fleet');
});

View File

@@ -2,6 +2,7 @@
namespace App\Services;
use Illuminate\Support\Collection;
use Log;
use App\Facades\Utils;
use App\Models\User;
@@ -53,6 +54,18 @@ class UserService extends BaseService
return $user;
}
/**
* Return the subfleets this user is allowed access to,
* based on their current rank
* @param $user
* @return Collection
*/
public function getAllowableSubfleets($user)
{
$subfleets = $user->rank->subfleets();
return $subfleets->with('aircraft')->get();
}
/**
* Change the user's state. PENDING to ACCEPTED, etc
* Send out an email

66
tests/PilotTest.php Normal file
View File

@@ -0,0 +1,66 @@
<?php
use App\Services\UserService;
use Tests\TestData;
class PilotTest extends TestCase
{
protected $userSvc;
public function setUp()
{
parent::setUp();
$this->userSvc = app(UserService::class);
}
public function testRankSubfleets()
{
# Add subfleets and aircraft, but also add another
# set of subfleets
$subfleetA = TestData::createSubfleetWithAircraft();
TestData::createSubfleetWithAircraft();
$rank = TestData::createRank(10, [$subfleetA['subfleet']->id]);
$user = factory(App\Models\User::class)->create([
'rank_id' => $rank->id,
]);
$added_aircraft = $subfleetA['aircraft']->pluck('id');
$subfleets = $this->userSvc->getAllowableSubfleets($user);
$this->assertEquals(1, $subfleets->count());
$subfleet = $subfleets[0];
$all_aircraft = $subfleet->aircraft->pluck('id');
$this->assertEquals($added_aircraft, $all_aircraft);
/**
* Check via API
*/
$resp = $this->get('/api/user/fleet', [], $user);
$body = $resp->json();
# Get the subfleet that's been added in
$subfleet_from_api = $body['data'][0];
$this->assertEquals($subfleet->id, $subfleet_from_api['id']);
# Get all the aircraft from that subfleet
$aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id');
$this->assertEquals($added_aircraft, $aircraft_from_api);
/**
* Check the user ID call
*/
$resp = $this->get('/api/users/'.$user->id.'/fleet', [], $user);
$body = $resp->json();
# Get the subfleet that's been added in
$subfleet_from_api = $body['data'][0];
$this->assertEquals($subfleet->id, $subfleet_from_api['id']);
# Get all the aircraft from that subfleet
$aircraft_from_api = collect($subfleet_from_api['aircraft'])->pluck('id');
$this->assertEquals($added_aircraft, $aircraft_from_api);
}
}

View File

@@ -1,7 +1,5 @@
<?php
use App\Models\Fare;
use App\Models\Subfleet;
class SubfleetTest extends TestCase
{

54
tests/TestData.php Normal file
View File

@@ -0,0 +1,54 @@
<?php
/**
*
*/
namespace Tests;
class TestData
{
/**
* Create a rank and associate the given subfleet IDs with it
* @param int $hours
* @param array $subfleet_ids
* @return mixed
*/
public static function createRank($hours=0, $subfleet_ids=[])
{
$attrs = [];
if($hours === null) {
$attrs['hours'] = $hours;
}
$rank = factory(\App\Models\Rank::class)->create($attrs);
if(!empty($subfleet_ids)) {
$rank->subfleets()->syncWithoutDetaching($subfleet_ids);
}
return $rank;
}
/**
* Create a subfleet with a number of aircraft assigned
* @param null $aircraft_count
* @return mixed
*/
public static function createSubfleetWithAircraft($aircraft_count = null)
{
$subfleet = factory(\App\Models\Subfleet::class)->create();
if($aircraft_count === null) {
$aircraft_count = \random_int(2, 10);
}
$aircraft = factory(\App\Models\Aircraft::class, $aircraft_count)->create([
'subfleet_id' => $subfleet->id
]);
return [
'subfleet' => $subfleet,
'aircraft' => $aircraft,
];
}
}