From 8393ae2851881483f319674fb624190caca4230d Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Tue, 20 Feb 2018 12:06:52 -0600 Subject: [PATCH] Add /flight/:id/route resource, returns all route info that's in the navaid database closes #183 --- app/Database/factories/FlightFactory.php | 2 +- app/Database/factories/NavdataFactory.php | 2 +- app/Database/factories/PirepFactory.php | 3 -- app/Http/Controllers/Api/FlightController.php | 17 +++++- app/Http/Resources/Navdata.php | 24 +++++++++ app/Models/GeoJson.php | 3 -- app/Routes/api.php | 1 + app/Services/FlightService.php | 52 ++++++++++++++----- tests/AcarsTest.php | 3 +- tests/FlightTest.php | 31 +++++++++++ 10 files changed, 116 insertions(+), 22 deletions(-) create mode 100644 app/Http/Resources/Navdata.php diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index d7eebbb9c..ccdc5c038 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -25,7 +25,7 @@ return factory(App\Models\Airport::class)->create()->id; }, 'distance' => $faker->numberBetween(0, 3000), - 'route' => $faker->randomElement(['', $faker->text(5)]), + 'route' => null, 'dpt_time' => $faker->time(), 'arr_time' => $faker->time(), 'flight_time' => $faker->numberBetween(60, 360), diff --git a/app/Database/factories/NavdataFactory.php b/app/Database/factories/NavdataFactory.php index be3191d53..929f21891 100644 --- a/app/Database/factories/NavdataFactory.php +++ b/app/Database/factories/NavdataFactory.php @@ -6,7 +6,7 @@ $factory->define(App\Models\Navdata::class, function (Faker $faker) { return [ 'id' => str_replace(' ', '', str_replace('.', '', $faker->unique()->text(5))), - 'name' => $faker->unique()->text(10), + 'name' => str_replace('.', '', $faker->unique()->word), 'type' => $faker->randomElement([NavaidType::VOR, NavaidType::NDB]), 'lat' => $faker->latitude, 'lon' => $faker->longitude, diff --git a/app/Database/factories/PirepFactory.php b/app/Database/factories/PirepFactory.php index 323b8d936..bb08c17bd 100644 --- a/app/Database/factories/PirepFactory.php +++ b/app/Database/factories/PirepFactory.php @@ -8,8 +8,6 @@ */ $factory->define(App\Models\Pirep::class, function (Faker $faker) { - static $raw_data; - return [ 'id' => null, 'airline_id' => function () { @@ -48,7 +46,6 @@ 'source_name' => 'Test Factory', 'state' => PirepState::PENDING, 'status' => PirepStatus::SCHEDULED, - 'raw_data' => $raw_data ?: $raw_data = json_encode(['key' => 'value']), 'created_at' => Carbon::now()->toDateTimeString(), 'updated_at' => function(array $pirep) { return $pirep['created_at']; diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index b41f9c0ea..7bd86285f 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers\Api; -use App\Http\Resources\FlightCollection; use App\Repositories\Criteria\WhereCriteria; use App\Services\FlightService; use Auth; @@ -12,7 +11,9 @@ use App\Services\UserService; use App\Repositories\FlightRepository; + use App\Http\Resources\Flight as FlightResource; +use App\Http\Resources\Navdata as NavdataResource; /** * Class FlightController @@ -97,4 +98,18 @@ public function search(Request $request) return FlightResource::collection($flights); } + + /** + * Get a flight's route + * @param $id + * @param Request $request + * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection + */ + public function route($id, Request $request) + { + $flight = $this->flightRepo->find($id); + $route = $this->flightSvc->getRoute($flight); + + return NavdataResource::collection($route); + } } diff --git a/app/Http/Resources/Navdata.php b/app/Http/Resources/Navdata.php new file mode 100644 index 000000000..b74fd911b --- /dev/null +++ b/app/Http/Resources/Navdata.php @@ -0,0 +1,24 @@ + $point['type'], + 'name' => NavaidType::label($point['type']), + ]; + + $point['type'] = $type; + + return $point; + } +} diff --git a/app/Models/GeoJson.php b/app/Models/GeoJson.php index 7cb543aa9..88398b71d 100644 --- a/app/Models/GeoJson.php +++ b/app/Models/GeoJson.php @@ -11,9 +11,6 @@ * Return different points/features in GeoJSON format * https://tools.ietf.org/html/rfc7946 * - * Once a PIREP is accepted, save this returned structure as a - * JSON-encoded string into the raw_data field of the PIREP row - * * @package App\Models */ class GeoJson diff --git a/app/Routes/api.php b/app/Routes/api.php index 143aab81d..a35f81ca2 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -31,6 +31,7 @@ Route::get('flights', 'FlightController@index'); Route::get('flights/search', 'FlightController@search'); Route::get('flights/{id}', 'FlightController@get'); + Route::get('flights/{id}/route', 'FlightController@route'); Route::get('pireps/{pirep_id}', 'PirepController@get'); Route::put('pireps/{pirep_id}', 'PirepController@update'); diff --git a/app/Services/FlightService.php b/app/Services/FlightService.php index 65c2b5151..d8219dcc7 100644 --- a/app/Services/FlightService.php +++ b/app/Services/FlightService.php @@ -1,29 +1,31 @@ flightRepo = $flightRepo; + $this->navDataRepo = $navdataRepo; $this->userSvc = $userSvc; } @@ -98,6 +100,32 @@ public function deleteFlight(Flight $flight) $flight->delete(); } + /** + * Return all of the navaid points as a collection + * @param Flight $flight + * @return \Illuminate\Support\Collection + */ + public function getRoute(Flight $flight) + { + if(!$flight->route) { + return collect(); + } + + $route_points = array_map(function($point) { + return strtoupper($point); + }, explode(' ', $flight->route)); + + $route = $this->navDataRepo->findWhereIn('id', $route_points); + + // Put it back into the original order the route is in + $return_points = []; + foreach($route_points as $rp) { + $return_points[] = $route->where('id', $rp)->first(); + } + + return collect($return_points); + } + /** * Allow a user to bid on a flight. Check settings and all that good stuff * @param Flight $flight diff --git a/tests/AcarsTest.php b/tests/AcarsTest.php index 3de29f853..599cc715c 100644 --- a/tests/AcarsTest.php +++ b/tests/AcarsTest.php @@ -377,7 +377,8 @@ public function testAcarsRoutePost() foreach($route as $position) { $post_route[] = [ 'order' => $order, - 'name' => $position->name, + 'id' => $position->id, + 'name' => $position->id, 'lat' => $position->lat, 'lon' => $position->lon, ]; diff --git a/tests/FlightTest.php b/tests/FlightTest.php index d860d8fa0..021189e29 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -68,6 +68,37 @@ public function testSearchFlight() $req->assertStatus(200); } + /** + * Get the flight's route + */ + public function testFlightRoute() + { + $this->user = factory(App\Models\User::class)->create(); + $flight = $this->addFlight($this->user); + + $route_count = random_int(4, 6); + $route = factory(App\Models\Navdata::class, $route_count)->create(); + $route_text = implode(' ', $route->pluck('id')->toArray()); + + $flight->route = $route_text; + $flight->save(); + + $res = $this->get('/api/flights/'.$flight->id.'/route'); + $res->assertStatus(200); + $body = $res->json(); + + $this->assertCount($route_count, $body['data']); + + $first_point = $body['data'][0]; + $this->assertEquals($first_point['id'], $route[0]->id); + $this->assertEquals($first_point['name'], $route[0]->name); + $this->assertEquals($first_point['type']['type'], $route[0]->type); + $this->assertEquals( + $first_point['type']['name'], + \App\Models\Enums\NavaidType::label($route[0]->type) + ); + } + /** * Find all of the flights */