#35 rest api additions for pireps

This commit is contained in:
Nabeel Shahzad 2017-08-14 23:36:49 -05:00
parent 6a584b4d36
commit c45d0541ef
8 changed files with 122 additions and 22 deletions

View File

@ -7,7 +7,6 @@ use Illuminate\Http\Request;
use App\Models\Airline;
use App\Models\Airport;
use App\Models\Flight;
use App\Models\Transformers\FlightTransformer;
use App\Http\Controllers\AppBaseController;
@ -15,6 +14,13 @@ use App\Http\Controllers\AppBaseController;
class FlightController extends AppBaseController
{
public function get($id)
{
$flight = Flight::find($id);
return fractal($flight, new FlightTransformer())->respond();
}
public function search(Request $request)
{
$where = [];

View File

@ -0,0 +1,17 @@
<?php
namespace App\Http\Controllers\Api;
use App\Models\Pirep;
use App\Models\Transformers\PirepTransformer;
use App\Http\Controllers\AppBaseController;
class PirepController extends AppBaseController
{
public function get($id)
{
$pirep = Pirep::find($id);
return fractal($pirep, new PirepTransformer())->respond();
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Models\Transformers;
use App\Models\Airport;
use League\Fractal\TransformerAbstract;
class AirportTransform extends TransformerAbstract
{
public function transform(Airport $ap)
{
return [
'id' => $ap->id,
'icao' => $ap->icao,
'name' => $ap->name,
'location' => $ap->location,
'country' => $ap->country,
'fuel_100ll_cost' => $ap->fuel_100ll_cost,
'fuel_jeta_cost' => $ap->fuel_jeta_cost,
'fuel_mogas_cost' => $ap->fuel_mogas_cost,
'lat' => $ap->lat,
'lon' => $ap->lon,
];
}
}

View File

@ -2,13 +2,19 @@
namespace App\Models\Transformers;
use League\Fractal\TransformerAbstract;
use App\Models\Flight;
use League\Fractal\TransformerAbstract;
class FlightTransformer extends TransformerAbstract
{
public static $aptXform = null;
public function __construct()
{
FlightTransformer::$aptXform = new AirportTransform();
}
public function transform(Flight $flight)
{
$ret = [
@ -18,25 +24,13 @@ class FlightTransformer extends TransformerAbstract
'code' => $flight->airline->code,
'name' => $flight->airline->name,
],
'dpt' => [
'id' => $flight->dpt_airport->id,
'icao' => $flight->dpt_airport->icao,
'name' => $flight->dpt_airport->name,
],
'arr' => [
'id' => $flight->arr_airport->id,
'icao' => $flight->arr_airport->icao,
'name' => $flight->arr_airport->name,
],
'dpt' => FlightTransformer::$aptXform->transform($flight->dpt_airport),
'arr' => FlightTransformer::$aptXform->transform($flight->arr_airport),
'alt' => [],
];
if($flight->alt_airport_id) {
$flight['alt'] = [
'id' => $flight->alt_airport->id,
'icao' => $flight->alt_airport->icao,
'name' => $flight->alt_airport->name,
];
$flight['alt'] = FlightTransformer::$aptXform->transform($flight->alt_airport);
}
return $ret;

View File

@ -0,0 +1,46 @@
<?php
namespace App\Models\Transformers;
use App\Models\Pirep;
use League\Fractal\TransformerAbstract;
class PirepTransformer extends TransformerAbstract
{
public static $aptXform = null;
public static $flightXform = null;
public function __construct()
{
PirepTransformer::$aptXform = new AirportTransform();
PirepTransformer::$flightXform = new FlightTransformer();
}
public function transform(Pirep $pirep)
{
$p = [
'id' => $pirep->id,
'route_code' => $pirep->route_code,
'route_leg' => $pirep->route_leg,
'level' => $pirep->level,
'route' => $pirep->route,
'source' => $pirep->source,
'status' => $pirep->status,
'raw_data' => $pirep->raw_data,
'flight_time' => $pirep->flight_time,
'aircraft' => [],
'dpt' => PirepTransformer::$aptXform->transform($pirep->dpt_airport),
'arr' => PirepTransformer::$aptXform->transform($pirep->arr_airport),
'user' => [
'id' => $pirep->user->id,
'pilot_id' => $pirep->user->pilot_id(),
],
];
if ($pirep->flight_id) {
$p['flight'] = PirepTransformer::$flightXform->transform($pirep->flight);
}
return $p;
}
}

View File

@ -68,7 +68,7 @@ class RouteServiceProvider extends ServiceProvider
Route::group([
'middleware' => [
'api',
\App\Http\Middleware\MeasureExecutionTime::class
//\App\Http\Middleware\MeasureExecutionTime::class
],
'namespace' => $this->namespace."\\API",
'prefix' => 'api',

View File

@ -16,5 +16,9 @@ use Illuminate\Http\Request;
Route::group([], function () {
Route::match(['get'], 'status', 'BaseController@status');
Route::match(['get'], 'flight/{id}', 'FlightController@get');
Route::match(['get'], 'flights/search', 'FlightController@search');
Route::match(['get'], 'pirep/{id}', 'PirepController@get');
});

View File

@ -7,18 +7,26 @@ class FlightTest extends TestCase
{
parent::setUp();
$this->addData('airline');
$this->addData('airports');
$this->addData('base');
}
public function addFlight()
{
$flight = new App\Models\Flight;
$flight->airline_id = 1;
$flight->flight_number = 100;
$flight->flight_number = 10;
$flight->dpt_airport_id = 1;
$flight->arr_airport_id = 2;
$flight->save();
return $flight->id;
}
public function XtestGetFlight()
{
$flight_id = $this->addFlight();
$response = $this->json('GET', '/api/flight/'.$flight_id);
$response->assertStatus(200);
$response->assertJson(['data' => true]);
}
/**