Skip to content

Commit

Permalink
Add lookup for aircraft as /fleet/aircraft/{id} #120
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeelio committed Jan 5, 2018
1 parent 1ee9041 commit 500cbb8
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 15 deletions.
3 changes: 2 additions & 1 deletion app/Database/factories/AircraftFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

$factory->define(App\Models\Aircraft::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 100000),
#'id' => $faker->unique()->numberBetween(10, 100000),
'subfleet_id' => function() {
return factory(App\Models\Subfleet::class)->create()->id;
},
'airport_id' => function () {
return factory(App\Models\Airport::class)->create()->id;
},
'icao' => $faker->unique()->text(5),
'name' => $faker->unique()->text(50),
'registration' => $faker->unique()->text(10),
'tail_number' => $faker->unique()->text(10),
Expand Down
11 changes: 8 additions & 3 deletions app/Database/factories/AirlineFactory.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
<?php

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

/**
* Add any number of airports. Don't really care if they're real or not
*/
$factory->define(App\Models\Airline::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
'icao' => function(array $apt) { return substr($apt['id'],0, 4); },
'iata' => function (array $apt) { return $apt['id']; },
#'id' => $faker->unique()->numberBetween(10, 10000),
'icao' => function (array $apt) use ($faker) {
$hashids = new Hashids(microtime(), 5);
$mt = str_replace('.', '', microtime(true));
return $hashids->encode($mt);
},
'iata' => function (array $apt) { return $apt['icao']; },
'name' => $faker->sentence(3),
'country' => $faker->country,
'active' => 1
Expand Down
2 changes: 1 addition & 1 deletion app/Database/factories/FareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$factory->define(App\Models\Fare::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
#'id' => $faker->unique()->numberBetween(10, 10000),
'code' => $faker->text(5),
'name' => $faker->text(20),
'price' => $faker->randomFloat(2, 100, 1000),
Expand Down
2 changes: 1 addition & 1 deletion app/Database/factories/RankFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
$factory->define(App\Models\Rank::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
#'id' => $faker->unique()->numberBetween(10, 10000),
'name' => $faker->unique()->text(50),
'hours' => $faker->numberBetween(10, 50),
'auto_approve_acars' => 0,
Expand Down
2 changes: 1 addition & 1 deletion app/Database/factories/SubfleetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

$factory->define(App\Models\Subfleet::class, function (Faker $faker) {
return [
'id' => $faker->unique()->numberBetween(10, 10000),
#'id' => $faker->unique()->numberBetween(10, 10000),
'airline_id' => 1,
'name' => $faker->unique()->text(50),
'type' => $faker->unique()->text(7),
Expand Down
2 changes: 1 addition & 1 deletion app/Database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
static $password;

return [
'id' => $faker->unique()->numberBetween(10, 10000),
#'id' => $faker->unique()->numberBetween(10, 10000),
'name' => $faker->name,
'email' => $faker->safeEmail,
'password' => $password ?: $password = Hash::make('secret'),
Expand Down
43 changes: 36 additions & 7 deletions app/Http/Controllers/Api/FleetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;

use App\Repositories\AircraftRepository;
use App\Repositories\SubfleetRepository;

use App\Http\Resources\Aircraft as AircraftResource;
use App\Http\Resources\Subfleet as SubfleetResource;

class FleetController extends RestController
Expand All @@ -13,10 +16,10 @@ class FleetController extends RestController

public function __construct(
AircraftRepository $aircraftRepo,
SubfleetRepository $airportRepo
SubfleetRepository $subfleetRepo
) {
$this->aircraftRepo = $airportRepo;
$this->subfleetRepo = $airportRepo;
$this->aircraftRepo = $aircraftRepo;
$this->subfleetRepo = $subfleetRepo;
}

/**
Expand All @@ -25,10 +28,36 @@ public function __construct(
*/
public function index()
{
$airports = $this->subfleetRepo
->with(['aircraft', 'airline', 'fares', 'ranks'])
->paginate(50);
$subfleets = $this->subfleetRepo
->with(['aircraft', 'airline', 'fares', 'ranks'])
->paginate(50);

return SubfleetResource::collection($subfleets);
}

/**
* Get a specific aircraft. Query string required to specify the tail
* /api/aircraft/XYZ?type=registration
* @param $id
* @param Request $request
* @return AircraftResource
*/
public function get_aircraft($id, Request $request)
{
$where = [];
if($request->filled('type')) {
$where[$request->get('type')] = $id;
} else {
$where['id'] = $id;
}

$all_aircraft = $this->aircraftRepo->all();
$aircraft = $this->aircraftRepo
->with(['subfleet'])
->findWhere($where)
->first();

return SubfleetResource::collection($airports);
AircraftResource::withoutWrapping();
return new AircraftResource($aircraft);
}
}
1 change: 1 addition & 0 deletions app/Routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
Route::get('airports/{id}/lookup', 'AirportController@lookup');

Route::get('fleet', 'FleetController@index');
Route::get('fleet/aircraft/{id}', 'FleetController@get_aircraft');

Route::get('flights/search', 'FlightController@search');
Route::get('flights/{id}', 'FlightController@get');
Expand Down
34 changes: 34 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,38 @@ public function testGetSubfleets()
$this->assertCount($size, $subfleet['aircraft']);
}
}

/**
* Test getting an aircraft
*/
public function testGetAircraft()
{
$user = factory(App\Models\User::class)->create();
$subfleet = factory(App\Models\Subfleet::class)->create();
$aircraft = factory(App\Models\Aircraft::class)->create([
'subfleet_id' => $subfleet->id
]);

/**
* Just try retrieving by ID
*/
$resp = $this->user_get($user, '/api/fleet/aircraft/'. $aircraft->id);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);

$resp = $this->user_get($user,
'/api/fleet/aircraft/'.$aircraft->id.'?registration='.$aircraft->registration);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);

$resp = $this->user_get($user,
'/api/fleet/aircraft/' . $aircraft->id . '?tail_number=' . $aircraft->registration);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);

$resp = $this->user_get($user,
'/api/fleet/aircraft/' . $aircraft->id . '?icao=' . $aircraft->icao);
$body = $resp->json();
$this->assertEquals($body['id'], $aircraft->id);
}
}

0 comments on commit 500cbb8

Please sign in to comment.