Add call to get user's PIREPs #192

This commit is contained in:
Nabeel Shahzad 2018-02-19 10:35:49 -06:00
parent 5c58dfe1ae
commit 3d9d17d9b9
4 changed files with 98 additions and 11 deletions

View File

@ -2,6 +2,9 @@
namespace App\Http\Controllers\Api;
use App\Models\Enums\PirepState;
use App\Repositories\Criteria\WhereCriteria;
use App\Repositories\PirepRepository;
use App\Repositories\SubfleetRepository;
use App\Services\UserService;
use Auth;
@ -11,27 +14,46 @@ 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\Pirep as PirepResource;
use App\Http\Resources\Subfleet as SubfleetResource;
use App\Http\Resources\User as UserResource;
use Prettus\Repository\Criteria\RequestCriteria;
use Prettus\Repository\Exceptions\RepositoryException;
class UserController extends RestController
{
protected $subfleetRepo,
protected $pirepRepo,
$subfleetRepo,
$userRepo,
$userSvc;
public function __construct(
PirepRepository $pirepRepo,
SubfleetRepository $subfleetRepo,
UserRepository $userRepo,
UserService $userSvc
) {
$this->pirepRepo = $pirepRepo;
$this->subfleetRepo = $subfleetRepo;
$this->userRepo = $userRepo;
$this->userSvc = $userSvc;
}
/**
* @param Request $request
* @return int|mixed
*/
protected function getUserId(Request $request)
{
if ($request->id === null) {
return Auth::user()->id;
}
return $request->id;
}
/**
* Return the profile for the currently auth'd user
*/
@ -68,16 +90,37 @@ class UserController extends RestController
*/
public function fleet(Request $request)
{
if($request->id === null) {
$id = Auth::user()->id;
} else {
$id = $request->id;
}
$user = $this->userRepo->find($id);
$user = $this->userRepo->find($this->getUserId($request));
$subfleets = $this->userSvc->getAllowableSubfleets($user);
return SubfleetResource::collection($subfleets);
}
/**
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
* @throws RepositoryException
*/
public function pireps(Request $request)
{
$this->pirepRepo->pushCriteria(new RequestCriteria($request));
$where = [
'user_id' => $this->getUserId($request),
];
if(filled($request->query('state'))) {
$where['state'] = $request->query('state');
} else {
$where[] = ['state', '!=', PirepState::CANCELLED];
}
$this->pirepRepo->pushCriteria(new WhereCriteria($request, $where));
$pireps = $this->pirepRepo
->orderBy('created_at', 'desc')
->paginate();
return PirepResource::collection($pireps);
}
}

View File

@ -6,8 +6,8 @@ namespace App\Models\Enums;
class PirepState extends EnumBase {
public const REJECTED = -1;
public const IN_PROGRESS = 0;
public const PENDING = 1;
public const IN_PROGRESS = 0; // flight is ongoing
public const PENDING = 1; // waiting admin approval
public const ACCEPTED = 2;
public const CANCELLED = 3;
public const DELETED = 4;

View File

@ -62,9 +62,11 @@ 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('user/pireps', 'UserController@pireps');
Route::get('users/{id}', 'UserController@get');
Route::get('users/{id}/bids', 'UserController@bids');
Route::get('users/{id}/fleet', 'UserController@fleet');
Route::get('users/{id}/pireps', 'UserController@pireps');
});

View File

@ -113,6 +113,48 @@ class PIREPTest extends TestCase
$this->assertEquals($route, $saved_route);
}
/**
*
*/
public function testGetUserPireps()
{
$this->user = factory(App\Models\User::class)->create();
$pirep_done = factory(App\Models\Pirep::class)->create([
'user_id' => $this->user->id,
'state' => PirepState::ACCEPTED
]);
$pirep_in_progress = factory(App\Models\Pirep::class)->create([
'user_id' => $this->user->id,
'state' => PirepState::IN_PROGRESS
]);
$pirep_cancelled = factory(App\Models\Pirep::class)->create([
'user_id' => $this->user->id,
'state' => PirepState::CANCELLED
]);
$pireps = $this->get('/api/user/pireps')
->assertStatus(200)
->json();
$pirep_ids = collect($pireps['data'])->pluck('id');
$this->assertTrue($pirep_ids->contains($pirep_done->id));
$this->assertTrue($pirep_ids->contains($pirep_in_progress->id));
$this->assertFalse($pirep_ids->contains($pirep_cancelled->id));
// Get only status
$pireps = $this->get('/api/user/pireps?state='.PirepState::IN_PROGRESS)
->assertStatus(200)
->json();
$pirep_ids = collect($pireps['data'])->pluck('id');
$this->assertTrue($pirep_ids->contains($pirep_in_progress->id));
$this->assertFalse($pirep_ids->contains($pirep_done->id));
$this->assertFalse($pirep_ids->contains($pirep_cancelled->id));
}
/**
* check the stats/ranks, etc have incremented properly
*/