make sure resources return the data object #186

This commit is contained in:
Nabeel Shahzad 2018-02-10 17:34:46 -06:00
parent e57fb5a7c6
commit 21e13b6b92
13 changed files with 45 additions and 58 deletions

View File

@ -35,5 +35,4 @@ class AcarsController extends RestController
'Content-type' => 'application/json'
]);
}
}

View File

@ -35,7 +35,6 @@ class AirlineController extends RestController
public function get($id)
{
$id = strtoupper($id);
AirlineResource::withoutWrapping();
return new AirlineResource($this->airlineRepo->find($id));
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
@ -58,7 +59,6 @@ class AirportController extends RestController
public function get($id)
{
$id = strtoupper($id);
AirportResource::withoutWrapping();
return new AirportResource($this->airportRepo->find($id));
}
@ -73,7 +73,12 @@ class AirportController extends RestController
config('cache.keys.AIRPORT_VACENTRAL_LOOKUP.key') . $id,
config('cache.keys.RANKS_PILOT_LIST.time'),
function () use ($id) {
return AirportLookup::get($id);
try {
return AirportLookup::get($id);
} catch (\VaCentral\HttpException $e) {
Log::error($e);
return [];
}
}
);

View File

@ -30,7 +30,7 @@ class FleetController extends RestController
{
$subfleets = $this->subfleetRepo
->with(['aircraft', 'airline', 'fares', 'ranks'])
->paginate(50);
->paginate();
return SubfleetResource::collection($subfleets);
}
@ -51,13 +51,11 @@ class FleetController extends RestController
$where['id'] = $id;
}
#$all_aircraft = $this->aircraftRepo->all();
$aircraft = $this->aircraftRepo
->with(['subfleet', 'subfleet.fares'])
->findWhere($where)
->first();
AircraftResource::withoutWrapping();
return new AircraftResource($aircraft);
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api;
use App\Http\Resources\FlightCollection;
use App\Repositories\Criteria\WhereCriteria;
use App\Services\FlightService;
use Auth;
@ -33,6 +34,8 @@ class FlightController extends RestController
/**
* Return all the flights, paginated
* @param Request $request
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function index(Request $request)
{
@ -63,7 +66,6 @@ class FlightController extends RestController
$flight = $this->flightRepo->find($id);
$this->flightSvc->filterSubfleets(Auth::user(), $flight);
FlightResource::withoutWrapping();
return new FlightResource($flight);
}

View File

@ -79,7 +79,6 @@ class PirepController extends RestController
*/
public function get($id)
{
PirepResource::withoutWrapping();
return new PirepResource($this->pirepRepo->find($id));
}
@ -138,7 +137,6 @@ class PirepController extends RestController
$this->updateFields($pirep, $request);
PirepResource::withoutWrapping();
return new PirepResource($pirep);
}
@ -166,7 +164,6 @@ class PirepController extends RestController
$pirep = $this->pirepRepo->update($attrs, $id);
$this->updateFields($pirep, $request);
PirepResource::withoutWrapping();
return new PirepResource($pirep);
}
@ -208,7 +205,6 @@ class PirepController extends RestController
$this->pirepSvc->saveRoute($pirep);
}
PirepResource::withoutWrapping();
return new PirepResource($pirep);
}
@ -227,7 +223,6 @@ class PirepController extends RestController
'state' => PirepState::CANCELLED,
], $id);
PirepResource::withoutWrapping();
return new PirepResource($pirep);
}
@ -248,16 +243,15 @@ class PirepController extends RestController
}
/**
* Return the GeoJSON for the ACARS line
* Return the routes for the ACARS line
* @param $id
* @param Request $request
* @return AcarsRouteResource
*/
public function acars_get($id, Request $request)
{
$pirep = $this->pirepRepo->find($id);
$this->pirepRepo->find($id);
AcarsRouteResource::withoutWrapping();
return new AcarsRouteResource(Acars::where([
'pirep_id' => $id,
'type' => AcarsType::FLIGHT_PATH

View File

@ -37,7 +37,6 @@ class UserController extends RestController
*/
public function index(Request $request)
{
UserResource::withoutWrapping();
return new UserResource(Auth::user());
}
@ -46,7 +45,6 @@ class UserController extends RestController
*/
public function get($id)
{
UserResource::withoutWrapping();
return new UserResource($this->userRepo->find($id));
}
@ -79,7 +77,6 @@ class UserController extends RestController
$user = $this->userRepo->find($id);
$subfleets = $this->userSvc->getAllowableSubfleets($user);
SubfleetResource::withoutWrapping();
return SubfleetResource::collection($subfleets);
}

View File

@ -10,6 +10,7 @@ class Flight extends Resource
{
$flight = parent::toArray($request);
$flight['field'] = true;
$flight['airline'] = new Airline($this->airline);
$flight['subfleets'] = Subfleet::collection($this->subfleets);

View File

@ -8,17 +8,9 @@ class Subfleet extends Resource
{
public function toArray($request)
{
return [
'id' => $this->id,
'airline_id' => $this->airline_id,
'name' => $this->name,
'type' => $this->type,
'fuel_type' => $this->fuel_type,
'cargo_capacity' => $this->cargo_capacity,
'fuel_capacity' => $this->fuel_capacity,
'gross_weight' => $this->gross_weight,
$arr = parent::toArray($request);
$arr['aircraft'] = Aircraft::collection($this->aircraft);
'aircraft' => Aircraft::collection($this->aircraft),
];
return $arr;
}
}

View File

@ -249,7 +249,6 @@ const phpvms = (function() {
const onFlightClick = (feature, layer) => {
const uri = opts.pirep_uri.replace('{id}', feature.properties.pirep_id);
console.log('flight check uri', uri);
const flight_route = $.ajax({
url: uri,
@ -326,8 +325,6 @@ const phpvms = (function() {
if (layerSelFlight !== null) {
onFlightClick(layerSelFlightFeature, layerSelFlightLayer);
}
//map.fitBounds(layerFlights.getBounds());
//map.fitBounds('39.8283° N, 98.5795° W', 40);
});
};

View File

@ -49,7 +49,7 @@ class AcarsTest extends TestCase
{
$resp = $this ->get('/api/pireps/' . $pirep_id);
$resp->assertStatus(200);
return $resp->json();
return $resp->json()['data'];
}
/**
@ -116,11 +116,11 @@ class AcarsTest extends TestCase
# Get the PIREP ID
$body = $response->json();
$pirep_id = $body['id'];
$pirep_id = $body['data']['id'];
$this->assertHasKeys($body, ['airline', 'arr_airport', 'dpt_airport', 'position']);
$this->assertHasKeys($body['data'], ['airline', 'arr_airport', 'dpt_airport', 'position']);
$this->assertNotNull($pirep_id);
$this->assertEquals($body['user_id'], $this->user->id);
$this->assertEquals($body['data']['user_id'], $this->user->id);
# Check the PIREP state and status
$pirep = $this->getPirep($pirep_id);
@ -167,7 +167,7 @@ class AcarsTest extends TestCase
$response->assertStatus(200)->assertJson(['count' => 1]);
# Read that if the ACARS record posted
$acars_data = $this->get($uri)->json()[0];
$acars_data = $this->get($uri)->json()['data'][0];
$this->assertEquals(round($acars['lat'], 2), round($acars_data['lat'], 2));
$this->assertEquals(round($acars['lon'], 2), round($acars_data['lon'], 2));
$this->assertEquals($acars['log'], $acars_data['log']);
@ -179,7 +179,7 @@ class AcarsTest extends TestCase
$response = $this->get($uri);
$response->assertStatus(200);
$body = $response->json();
$body = $response->json()['data'];
$this->assertNotNull($body);
$this->assertCount(1, $body);
@ -201,6 +201,7 @@ class AcarsTest extends TestCase
]);
$response->assertStatus(200);
$body = $response->json();
# Add a comment
$uri = '/api/pireps/'.$pirep_id.'/comments';
@ -226,7 +227,7 @@ class AcarsTest extends TestCase
$response = $this->post($uri, $pirep);
$response->assertStatus(201);
$pirep_id = $response->json()['id'];
$pirep_id = $response->json()['data']['id'];
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
@ -239,7 +240,7 @@ class AcarsTest extends TestCase
$response->assertStatus(200)->assertJson(['count' => $acars_count]);
$response = $this->get($uri);
$response->assertStatus(200)->assertJsonCount($acars_count);
$response->assertStatus(200)->assertJsonCount($acars_count, 'data');
}
/**
@ -277,7 +278,7 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
$pirep_id = $response->json()['id'];
$pirep_id = $response->json()['data']['id'];
$dt = date('c');
$uri = '/api/pireps/' . $pirep_id . '/acars/position';
@ -300,7 +301,7 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
$pirep_id = $response->json()['id'];
$pirep_id = $response->json()['data']['id'];
$post_route = ['order' => 1, 'name' => 'NAVPOINT'];
$uri = '/api/pireps/' . $pirep_id . '/route';
@ -323,7 +324,7 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
$pirep_id = $response->json()['id'];
$pirep_id = $response->json()['data']['id'];
$acars = factory(App\Models\Acars::class)->make();
$post_log = [
@ -364,7 +365,7 @@ class AcarsTest extends TestCase
$uri = '/api/pireps/prefile';
$response = $this->post($uri, $pirep);
$pirep_id = $response->json()['id'];
$pirep_id = $response->json()['data']['id'];
$order = 1;
$post_route = [];

View File

@ -36,16 +36,17 @@ class ApiTest extends TestCase
// Test upper/lower case of Authorization header, etc
$response = $this->get($uri, $this->headers($user));
$response->assertStatus(200)->assertJson(['id' => $user->id], true);
$body = $response->json();
$response->assertStatus(200)->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['x-api-key' => $user->api_key])->get($uri)
->assertJson(['id' => $user->id], true);
->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['x-API-key' => $user->api_key])->get($uri)
->assertJson(['id' => $user->id], true);
->assertJson(['data' => ['id' => $user->id]]);
$this->withHeaders(['X-API-KEY' => $user->api_key])->get($uri)
->assertJson(['id' => $user->id], true);
->assertJson(['data' => ['id' => $user->id]]);
}
/**
@ -79,7 +80,8 @@ class ApiTest extends TestCase
$this->assertCount($size, $body['data']);
$airline = $airlines->random();
$this->get('/api/airlines/'.$airline->id)->assertJson(['name' => $airline->name]);
$this->get('/api/airlines/'.$airline->id)
->assertJson(['data' => ['name' => $airline->name]]);
}
/**
@ -93,7 +95,7 @@ class ApiTest extends TestCase
$response = $this->get('/api/airports/' . $airport->icao);
$response->assertStatus(200);
$response->assertJson(['icao' => $airport->icao], true);
$response->assertJson(['data' => ['icao' => $airport->icao]]);
$this->get('/api/airports/UNK')->assertStatus(404);
}
@ -158,9 +160,9 @@ class ApiTest extends TestCase
$response = $this->get('/api/fleet');
$response->assertStatus(200);
$body = $response->json();
$body = $response->json()['data'];
foreach($body['data'] as $subfleet) {
foreach($body as $subfleet) {
if($subfleet['id'] === $subfleetA->id) {
$size = $subfleetA_size;
} else {
@ -195,19 +197,19 @@ class ApiTest extends TestCase
* Just try retrieving by ID
*/
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id);
$body = $resp->json();
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?registration=' . $aircraft->registration);
$body = $resp->json();
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration);
$body = $resp->json();
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
$resp = $this->get('/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao);
$body = $resp->json();
$body = $resp->json()['data'];
$this->assertEquals($body['id'], $aircraft->id);
}

View File

@ -42,7 +42,7 @@ class FlightTest extends TestCase
$req = $this->get('/api/flights/' . $flight->id);
$req->assertStatus(200);
$body = $req->json();
$body = $req->json()['data'];
$this->assertEquals($flight->id, $body['id']);
$this->assertEquals($flight->dpt_airport_id, $body['dpt_airport_id']);
$this->assertEquals($flight->arr_airport_id, $body['arr_airport_id']);