Skip to content

Commit

Permalink
Add /api/airports with pagination to return all the airports #120
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jan 5, 2018
1 parent e04fa2e commit e931310
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 3 deletions.
10 changes: 8 additions & 2 deletions app/Database/factories/AirportFactory.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
<?php

use Hashids\Hashids;
use Faker\Generator as Faker;

/**
* Add any number of airports. Don't really care if they're real or not
*/
$factory->define(App\Models\Airport::class, function (Faker $faker) {

return [
'id' => strtoupper($faker->unique()->text(5)),
'icao' => function(array $apt) { return substr($apt['id'],0, 4); },
'id' => function(array $apt) use ($faker) {
$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']; },
'name' => $faker->sentence(3),
'country' => $faker->country,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function up()
Schema::create('airports', function (Blueprint $table) {
$table->string('id', 5)->primary();
$table->string('iata', 5)->nullable();
$table->string('icao', 4);
$table->string('icao', 5);
$table->string('name', 100);
$table->string('location', 100)->nullable();
$table->string('country', 64)->nullable();
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Controllers/Api/AirportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ public function __construct(
$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
* @param $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 @@ -7,6 +7,7 @@
{
Route::get('acars', 'AcarsController@index');

Route::get('airports', 'AirportController@index');
Route::get('airports/{id}', 'AirportController@get');
Route::get('airports/{id}/lookup', 'AirportController@lookup');

Expand Down
22 changes: 22 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ public function testAirportRequest()
->get('/api/airports/UNK')
->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');
}
}
11 changes: 11 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public function assertHasKeys($obj, $keys=[])
$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);
}
}

0 comments on commit e931310

Please sign in to comment.