Add /api/airports with pagination to return all the airports #120

This commit is contained in:
Nabeel Shahzad
2018-01-05 10:23:26 -06:00
parent e04fa2e056
commit e931310dee
6 changed files with 52 additions and 3 deletions

View File

@@ -1,14 +1,20 @@
<?php <?php
use Hashids\Hashids;
use Faker\Generator as Faker; use Faker\Generator as Faker;
/** /**
* Add any number of airports. Don't really care if they're real or not * Add any number of airports. Don't really care if they're real or not
*/ */
$factory->define(App\Models\Airport::class, function (Faker $faker) { $factory->define(App\Models\Airport::class, function (Faker $faker) {
return [ return [
'id' => strtoupper($faker->unique()->text(5)), 'id' => function(array $apt) use ($faker) {
'icao' => function(array $apt) { return substr($apt['id'],0, 4); }, $hashids = new Hashids(microtime(), 5);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
},
'icao' => function(array $apt) { return $apt['id']; },
'iata' => function (array $apt) { return $apt['id']; }, 'iata' => function (array $apt) { return $apt['id']; },
'name' => $faker->sentence(3), 'name' => $faker->sentence(3),
'country' => $faker->country, 'country' => $faker->country,

View File

@@ -10,7 +10,7 @@ class CreateAirportsTable extends Migration
Schema::create('airports', function (Blueprint $table) { Schema::create('airports', function (Blueprint $table) {
$table->string('id', 5)->primary(); $table->string('id', 5)->primary();
$table->string('iata', 5)->nullable(); $table->string('iata', 5)->nullable();
$table->string('icao', 4); $table->string('icao', 5);
$table->string('name', 100); $table->string('name', 100);
$table->string('location', 100)->nullable(); $table->string('location', 100)->nullable();
$table->string('country', 64)->nullable(); $table->string('country', 64)->nullable();

View File

@@ -19,6 +19,15 @@ class AirportController extends AppBaseController
$this->airportRepo = $airportRepo; $this->airportRepo = $airportRepo;
} }
/**
* Return all the airports, paginated
*/
public function index()
{
$airports = $this->airportRepo->orderBy('icao', 'asc')->paginate(50);
return AirportResource::collection($airports);
}
/** /**
* Do a lookup, via vaCentral, for the airport information * Do a lookup, via vaCentral, for the airport information
* @param $id * @param $id

View File

@@ -7,6 +7,7 @@ Route::group([], function()
{ {
Route::get('acars', 'AcarsController@index'); Route::get('acars', 'AcarsController@index');
Route::get('airports', 'AirportController@index');
Route::get('airports/{id}', 'AirportController@get'); Route::get('airports/{id}', 'AirportController@get');
Route::get('airports/{id}/lookup', 'AirportController@lookup'); Route::get('airports/{id}/lookup', 'AirportController@lookup');

View File

@@ -83,4 +83,26 @@ class ApiTest extends TestCase
->get('/api/airports/UNK') ->get('/api/airports/UNK')
->assertStatus(404); ->assertStatus(404);
} }
/**
* Get all the airports, test the pagination
*/
public function testGetAllAirports()
{
$user = factory(App\Models\User::class)->create();
factory(App\Models\Airport::class, 120)->create();
$response = $this->user_get($user, '/api/airports/')
->assertStatus(200)
->assertJsonCount(50, 'data');
$body = $response->json();
$this->assertHasKeys($body, ['data', 'links', 'meta']);
$last_page = $body['meta']['last_page'];
$this->user_get($user, '/api/airports?page='.$last_page)
->assertStatus(200)
->assertJsonCount(20, 'data');
}
} }

View File

@@ -95,4 +95,15 @@ abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
$this->assertArrayHasKey($key, $obj); $this->assertArrayHasKey($key, $obj);
} }
} }
/**
* Shortcut for a get call with a user
* @param \App\Models\User $user
* @param string $uri
* @return \Illuminate\Foundation\Testing\TestResponse
*/
public function user_get($user, $uri)
{
return $this->withHeaders($this->headers($user))->get($uri);
}
} }