Skip to content

Commit

Permalink
Add /api/flights to retrieve all flights paginated #120
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jan 6, 2018
1 parent 2de8d9e commit 082c33a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/Database/factories/FlightFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
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() {
Expand Down
12 changes: 12 additions & 0 deletions app/Http/Controllers/Api/FlightController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public function __construct(FlightRepository $flightRepo) {
$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);
Expand Down
1 change: 1 addition & 0 deletions app/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
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');

Expand Down
17 changes: 16 additions & 1 deletion tests/FlightTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,27 @@ public function testSearchFlight()
$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();

Expand Down

0 comments on commit 082c33a

Please sign in to comment.