From 082c33ae5926786c8105b2d32c3eafa8b719df03 Mon Sep 17 00:00:00 2001 From: Nabeel Shahzad Date: Sat, 6 Jan 2018 15:21:21 -0600 Subject: [PATCH] Add /api/flights to retrieve all flights paginated #120 --- app/Database/factories/FlightFactory.php | 2 +- app/Http/Controllers/Api/FlightController.php | 12 ++++++++++++ app/Routes/api.php | 1 + tests/FlightTest.php | 17 ++++++++++++++++- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app/Database/factories/FlightFactory.php b/app/Database/factories/FlightFactory.php index e996b49d..36d8e74a 100644 --- a/app/Database/factories/FlightFactory.php +++ b/app/Database/factories/FlightFactory.php @@ -10,7 +10,7 @@ $factory->define(App\Models\Flight::class, function (Faker $faker) use ($airline return [ 'id' => substr($faker->unique()->sha1, 28, 12), 'airline_id' => $faker->randomElement($airlinesAvailable), - 'flight_number' => $faker->unique()->text(10), + 'flight_number' => $faker->unique()->numberBetween(10, 1000000), 'route_code' => $faker->randomElement(['', $faker->text(5)]), 'route_leg' => $faker->randomElement(['', $faker->text(5)]), 'dpt_airport_id' => function() { diff --git a/app/Http/Controllers/Api/FlightController.php b/app/Http/Controllers/Api/FlightController.php index e7dbb774..0d7a5d62 100644 --- a/app/Http/Controllers/Api/FlightController.php +++ b/app/Http/Controllers/Api/FlightController.php @@ -18,6 +18,18 @@ class FlightController extends RestController $this->flightRepo = $flightRepo; } + /** + * Return all the flights, paginated + */ + public function index(Request $request) + { + $flights = $this->flightRepo + ->orderBy('flight_number', 'asc') + ->paginate(50); + + return FlightResource::collection($flights); + } + public function get($id) { $flight = $this->flightRepo->find($id); diff --git a/app/Routes/api.php b/app/Routes/api.php index ac6b263a..8724125e 100755 --- a/app/Routes/api.php +++ b/app/Routes/api.php @@ -18,6 +18,7 @@ Route::group([], function() Route::get('fleet', 'FleetController@index'); Route::get('fleet/aircraft/{id}', 'FleetController@get_aircraft'); + Route::get('flights', 'FlightController@index'); Route::get('flights/search', 'FlightController@search'); Route::get('flights/{id}', 'FlightController@get'); diff --git a/tests/FlightTest.php b/tests/FlightTest.php index a4d8f445..20a03e50 100644 --- a/tests/FlightTest.php +++ b/tests/FlightTest.php @@ -54,12 +54,27 @@ class FlightTest extends TestCase $req->assertStatus(200); } + /** + * Find all of the flights + */ + public function testFindAllFlights() + { + factory(App\Models\Flight::class, 120)->create(); + $res = $this->get('/api/flights'); + + $body = $res->json(); + $this->assertEquals(3, $body['meta']['last_page']); + + $res = $this->get('/api/flights?page=3'); + $res->assertJsonCount(20, 'data'); + } + public function testFlightSearchApi() { $flights = factory(App\Models\Flight::class, 100)->create(); $flight = $flights->random(); - $query = 'dep_icao=' . $flight->dep_icao; + $query = 'flight_number=' . $flight->flight_number; $req = $this->get('/api/flights/search?' . $query); $body = $req->json();