diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index f0b068d6..0909e30f 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -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 = []; diff --git a/app/Http/Controllers/Api/PirepController.php b/app/Http/Controllers/Api/PirepController.php new file mode 100644 index 00000000..bff8882f --- /dev/null +++ b/app/Http/Controllers/Api/PirepController.php @@ -0,0 +1,17 @@ +respond(); + } +} diff --git a/app/Models/Transformers/AirportTransform.php b/app/Models/Transformers/AirportTransform.php new file mode 100644 index 00000000..f9093141 --- /dev/null +++ b/app/Models/Transformers/AirportTransform.php @@ -0,0 +1,25 @@ + $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, + ]; + } +} diff --git a/app/Models/Transformers/FlightTransformer.php b/app/Models/Transformers/FlightTransformer.php index cdc9f423..807c6a44 100644 --- a/app/Models/Transformers/FlightTransformer.php +++ b/app/Models/Transformers/FlightTransformer.php @@ -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; diff --git a/app/Models/Transformers/PirepTransformer.php b/app/Models/Transformers/PirepTransformer.php new file mode 100644 index 00000000..894c7a0e --- /dev/null +++ b/app/Models/Transformers/PirepTransformer.php @@ -0,0 +1,46 @@ + $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; + } +} diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index d867a2a3..c77e47e8 100755 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -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', diff --git a/routes/api.php b/routes/api.php index 409d0085..a4ed7b90 100755 --- a/routes/api.php +++ b/routes/api.php @@ -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'); }); diff --git a/tests/FlightTest.php b/tests/FlightTest.php index a4c026ec..6a07045a 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -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]); } /**