Add /api/flights to retrieve all flights paginated #120

This commit is contained in:
Nabeel Shahzad 2018-01-06 15:21:21 -06:00
parent 2de8d9ed64
commit 082c33ae59
4 changed files with 30 additions and 2 deletions

View File

@ -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() {

View File

@ -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);

View File

@ -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');

View File

@ -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();